feat: improve code block parsing

This commit is contained in:
Anthony Fu 2022-11-27 14:16:02 +08:00
parent 2bf4f8913a
commit 8dd91002d7
8 changed files with 39 additions and 17 deletions

View file

@ -60,9 +60,10 @@ export function contentToVNode(
return `:${name}:`
})
// handle code frames
.replace(/<p>(```|~~~)([\w]*)([\s\S]+?)\1/g, (_1, _2, lang, raw) => {
const code = htmlToText(`<p>${raw}</p>`)
return `<custom-code lang="${lang?.trim().toLowerCase() || ''}" code="${encodeURIComponent(code)}"></custom-code>`
.replace(/>(```|~~~)([\s\S]+?)\1/g, (_1, _2, raw) => {
const plain = htmlToText(raw)
const [lang, ...code] = plain.split('\n')
return `><custom-code lang="${lang?.trim().toLowerCase() || ''}" code="${encodeURIComponent(code.join('\n'))}" />`
})
const tree = parseFragment(content)