Skip to content

Commit 6df3204

Browse files
authored
bug: do not escape html entities in code spans/blocks (#29)
1 parent 5b6e81f commit 6df3204

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/lib/outline.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { $ } from 'dom';
99

1010
marked.use({
1111
renderer: {
12-
link: markdownParsers.link
12+
link: markdownParsers.link,
13+
codespan: markdownParsers.codespan as any
1314
}
1415
});
1516

src/lib/parsers/md-parser.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,33 @@ import { Tokens } from 'marked';
33
export function link(token: Tokens.Link): string {
44
return `<a href="${token.href}" ${token.title ? `title="${token.title}"` : ''} target="_blank">${token.text}</a>`;
55
}
6+
7+
function decodeHtmlEntities(input: string): string {
8+
if (!input) {
9+
return '';
10+
}
11+
return input
12+
.replace(/&amp;/g, '&')
13+
.replace(/&lt;/g, '<')
14+
.replace(/&gt;/g, '>')
15+
.replace(/&quot;/g, '"')
16+
.replace(/&#39;/g, "'");
17+
}
18+
19+
function escapeForCode(input: string): string {
20+
if (!input) {
21+
return '';
22+
}
23+
// For code content, escape only what is necessary for safe HTML text rendering.
24+
// We intentionally do NOT escape '>' so sequences like `g>g` render literally.
25+
return input
26+
.replace(/&/g, '&amp;')
27+
.replace(/</g, '&lt;');
28+
}
29+
30+
export function codespan(token: Tokens.Codespan): string {
31+
const text = token.text;
32+
const decoded = decodeHtmlEntities(text);
33+
const escaped = escapeForCode(decoded);
34+
return `<code>${escaped}</code>`;
35+
}

0 commit comments

Comments
 (0)