diff --git a/src/markdown.js b/src/markdown.js new file mode 100644 index 0000000..5e43017 --- /dev/null +++ b/src/markdown.js @@ -0,0 +1,30 @@ +import mathjax3 from 'https://esm.sh/markdown-it-mathjax3'; + +export const md = window.md = window.markdownit({ html: false, linkify: true, typographer: true, breaks: true }).use(mathjax3); + +export function enhanceCodeBlocks(root, doHL = true) { + window.$(root).find('pre>code').each((i, code) => { + if (code.textContent.length > 200000) return; + const $pre = window.$(code).parent().addClass('relative rounded-xl border border-gray-200'); + if (!$pre.find('.code-actions').length) { + const len = code.textContent.length, countText = len >= 1e3 ? (len / 1e3).toFixed(1) + 'K' : len; + const $btn = window.$('').on('click', async e => { + e.stopPropagation(); + try { + await navigator.clipboard.writeText(code.innerText); + $btn.text('Copied'); + setTimeout(() => $btn.text('Copy'), 1200); + } catch { } + }); + const $container = window.$('
'); + $container.append(window.$(`${countText} chars`), $btn); + $pre.append($container); + } + if (doHL && window.hljs && code.textContent.length < 100000) window.hljs.highlightElement(code); + }); +} + +export const renderMarkdown = window.renderMarkdown = function (node, text, opt = { enhance: true, highlight: true }) { + node.innerHTML = md.render(text); + if (opt.enhance) enhanceCodeBlocks(node, opt.highlight); +};