fix: prevent HTML injections to code blocks (#1165)
This commit is contained in:
parent
1a4fd19720
commit
c15df78cbb
5 changed files with 88 additions and 25 deletions
|
@ -48,10 +48,22 @@ export function useShikiTheme() {
|
|||
return useColorMode().value === 'dark' ? 'vitesse-dark' : 'vitesse-light'
|
||||
}
|
||||
|
||||
const HTML_ENTITIES = {
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'&': '&',
|
||||
'\'': ''',
|
||||
'"': '"',
|
||||
} as Record<string, string>
|
||||
|
||||
function escapeHtml(text: string) {
|
||||
return text.replace(/[<>&'"]/g, ch => HTML_ENTITIES[ch])
|
||||
}
|
||||
|
||||
export function highlightCode(code: string, lang: Lang) {
|
||||
const shiki = useHightlighter(lang)
|
||||
if (!shiki)
|
||||
return code
|
||||
return escapeHtml(code)
|
||||
|
||||
return shiki.codeToHtml(code, {
|
||||
lang,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue