fix: escape html (#506)
All checks were successful
Build and Deploy / build-and-deploy (push) Has been skipped
Build and Push Docker Images / build (push) Has been skipped

This commit is contained in:
Libin YANG 2025-01-06 09:28:52 +08:00 committed by GitHub
parent 4f73811f0e
commit 8f09acb06d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -40,6 +40,16 @@ function buildTheme({ theme: _theme, fonts, size, isUseIndent }: IOpts): ThemeSt
} as ThemeStyles } as ThemeStyles
} }
function escapeHtml(text: string): string {
return text
.replace(/&/g, `&`) // 转义 &
.replace(/</g, `&lt;`) // 转义 <
.replace(/>/g, `&gt;`) // 转义 >
.replace(/"/g, `&quot;`) // 转义 "
.replace(/'/g, `&#39;`) // 转义 '
.replace(/`/g, `&#96;`) // 转义 `
}
function buildAddition(): string { function buildAddition(): string {
return ` return `
<style> <style>
@ -203,7 +213,7 @@ export function initRenderer(opts: IOpts) {
}, },
codespan({ text }: Tokens.Codespan): string { codespan({ text }: Tokens.Codespan): string {
const escapedText = text.replace(/</g, `&lt;`).replace(/>/g, `&gt;`) const escapedText = escapeHtml(text)
return styledContent(`codespan`, escapedText, `code`) return styledContent(`codespan`, escapedText, `code`)
}, },