Skip to content

Commit

Permalink
fix: escape html (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme authored Jan 6, 2025
1 parent 4f73811 commit 8f09acb
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/utils/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ function buildTheme({ theme: _theme, fonts, size, isUseIndent }: IOpts): ThemeSt
} 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 {
return `
<style>
Expand Down Expand Up @@ -203,7 +213,7 @@ export function initRenderer(opts: IOpts) {
},

codespan({ text }: Tokens.Codespan): string {
const escapedText = text.replace(/</g, `&lt;`).replace(/>/g, `&gt;`)
const escapedText = escapeHtml(text)
return styledContent(`codespan`, escapedText, `code`)
},

Expand Down

0 comments on commit 8f09acb

Please sign in to comment.