Skip to content

Commit 91978aa

Browse files
committed
fix(core): escape all angle brackets with "santizeComments" (#612)
1 parent 5b412fb commit 91978aa

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { sanitizeComments } from './sanitize-comments';
2+
3+
describe('sanitizeComments', () => {
4+
it('should escape special characters correctly', () => {
5+
const input = 'Comments <tag></tag>, {braces}, >= 5, < 5, `code`';
6+
const output =
7+
'Comments \\<tag\\>\\</tag\\>, \\{braces\\}, \\>= 5, \\< 5, `code`';
8+
const result = sanitizeComments(input);
9+
expect(result).toEqual(output);
10+
});
11+
12+
it('should not escape blockquotes', () => {
13+
const input = '> Blockquote with <tag>';
14+
const output = '> Blockquote with \\<tag\\>';
15+
const result = sanitizeComments(input);
16+
expect(result).toEqual(output);
17+
});
18+
19+
it('should not escape inline code', () => {
20+
const input = 'Comment with <tag> `code with <tag> and {braces}`';
21+
const output = 'Comment with \\<tag\\> `code with <tag> and {braces}`';
22+
const result = sanitizeComments(input);
23+
expect(result).toEqual(output);
24+
});
25+
26+
it('should not escape code block', () => {
27+
const input = `
28+
<tag>
29+
\`\`\`html
30+
<div>x</div>
31+
\`\`\``;
32+
const expectedOutput = `
33+
\\<tag\\>
34+
\`\`\`html
35+
<div>x</div>
36+
\`\`\``;
37+
const result = sanitizeComments(input);
38+
expect(result).toEqual(expectedOutput);
39+
});
40+
});

packages/typedoc-plugin-markdown/src/libs/utils/sanitize-comments.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export function sanitizeComments(str: string) {
2-
const re = /<(?=(?:[^`]*`[^`]*`)*[^`]*$)[^<]+?>/gi;
32
const codeBlocks: string[] = [];
43
const placeholder = '___CODEBLOCKPLACEHOLDER___';
54

@@ -9,9 +8,11 @@ export function sanitizeComments(str: string) {
98
return placeholder;
109
});
1110

12-
// Perform escaping outside of code blocks
11+
// If line starts with a > treat it as a blockquote
12+
// Otherwise escape all <, >, {, and } characters
1313
str = str
14-
.replace(re, (tags) => tags.replace(/>/g, '\\>').replace(/</g, '\\<'))
14+
.replace(/(?!^)>/gm, '\\>')
15+
.replace(/</g, '\\<')
1516
.replace(/\{/g, '\\{')
1617
.replace(/\}/g, '\\}');
1718

0 commit comments

Comments
 (0)