fix: render code block without language (#276)

This commit is contained in:
三咲智子 Kevin Deng 2022-12-02 10:19:31 +08:00 committed by GitHub
parent 18d5fd4804
commit 0f06653636
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 7 deletions

View file

@ -38,7 +38,7 @@ function handleCodeBlock(el: Element) {
const codeEl = el.childNodes[0] as Element
const classes = codeEl.attrs.find(i => i.name === 'class')?.value
const lang = classes?.split(/\s/g).find(i => i.startsWith('language-'))?.replace('language-', '')
const code = treeToText(codeEl.childNodes[0])
const code = codeEl.childNodes[0] ? treeToText(codeEl.childNodes[0]) : ''
return h(ContentCode, { lang, code: encodeURIComponent(code) })
}
}
@ -61,11 +61,10 @@ export function parseMastodonHTML(html: string, customEmojis: Record<string, Emo
return `:${name}:`
})
// handle code blocks
.replace(/>(```|~~~)([\s\S]+?)\1/g, (_1, _2, raw) => {
const plain = htmlToText(raw)
const [lang, ...code] = plain.split('\n')
.replace(/>(```|~~~)(\w*)([\s\S]+?)\1/g, (_1, _2, lang, raw) => {
const code = htmlToText(raw)
const classes = lang ? ` class="language-${lang}"` : ''
return `><pre><code${classes}>${code.join('\n')}</code></pre>`
return `><pre><code${classes}>${code}</code></pre>`
})
const tree = parseFragment(processed)