Skip to content

Commit aad9b27

Browse files
authored
Add/fix important kinds of GitHub highlighting (#104)
2 parents 93a30a9 + 68e60ff commit aad9b27

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

packages/overtype

src/lib/enhancers/github/github-common.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,28 @@ export function prepareGitHubHighlighter() {
3636
})
3737
}
3838

39+
function escapeHtml(text: string): string {
40+
const map: Record<string, string> = {
41+
"&": "&amp;",
42+
"<": "&lt;",
43+
">": "&gt;",
44+
'"': "&quot;",
45+
"'": "&#39;",
46+
}
47+
return text.replace(/[&<>"']/g, (m) => map[m]!)
48+
}
49+
3950
function githubHighlighter(code: string, language?: string) {
4051
try {
4152
if (language && hljs.getLanguage(language)) {
4253
const result = hljs.highlight(code, { language })
4354
return result.value
4455
} else {
45-
return code
56+
// No language specified - escape HTML to prevent tags from being interpreted
57+
return escapeHtml(code)
4658
}
4759
} catch (error) {
4860
console.warn("highlight.js highlighting failed:", error)
49-
return code
61+
return escapeHtml(code)
5062
}
5163
}

0 commit comments

Comments
 (0)