fix: escape/textify the contents of inline and block code (#954)

This commit is contained in:
Daniel Roe 2023-01-11 16:24:13 +00:00 committed by GitHub
parent 24d43699bb
commit f9509f8987
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 1 deletions

View file

@ -53,10 +53,13 @@ export function parseMastodonHTML(
// Handle code blocks
html = html
.replace(/>(```|~~~)(\w*)([\s\S]+?)\1/g, (_1, _2, lang: string, raw: string) => {
const code = htmlToText(raw)
const code = htmlToText(raw).replace(/</g, '&lt;').replace(/>/g, '&gt;')
const classes = lang ? ` class="language-${lang}"` : ''
return `><pre><code${classes}>${code}</code></pre>`
})
.replace(/`([^`\n]*)`/g, (_1, raw) => {
return raw ? `<code>${htmlToText(raw).replace(/</g, '&lt;').replace(/>/g, '&gt;')}</code>` : ''
})
}
// Always sanitize the raw HTML data *after* it has been modified