diff --git a/packages/typedoc-plugin-markdown/src/libs/utils/sanitize-comments.spec.ts b/packages/typedoc-plugin-markdown/src/libs/utils/sanitize-comments.spec.ts new file mode 100644 index 000000000..b905c516c --- /dev/null +++ b/packages/typedoc-plugin-markdown/src/libs/utils/sanitize-comments.spec.ts @@ -0,0 +1,40 @@ +import { sanitizeComments } from './sanitize-comments'; + +describe('sanitizeComments', () => { + it('should escape special characters correctly', () => { + const input = 'Comments , {braces}, >= 5, < 5, `code`'; + const output = + 'Comments \\\\, \\{braces\\}, \\>= 5, \\< 5, `code`'; + const result = sanitizeComments(input); + expect(result).toEqual(output); + }); + + it('should not escape blockquotes', () => { + const input = '> Blockquote with '; + const output = '> Blockquote with \\'; + const result = sanitizeComments(input); + expect(result).toEqual(output); + }); + + it('should not escape inline code', () => { + const input = 'Comment with `code with and {braces}`'; + const output = 'Comment with \\ `code with and {braces}`'; + const result = sanitizeComments(input); + expect(result).toEqual(output); + }); + + it('should not escape code block', () => { + const input = ` + +\`\`\`html +
x
+\`\`\``; + const expectedOutput = ` +\\ +\`\`\`html +
x
+\`\`\``; + const result = sanitizeComments(input); + expect(result).toEqual(expectedOutput); + }); +}); diff --git a/packages/typedoc-plugin-markdown/src/libs/utils/sanitize-comments.ts b/packages/typedoc-plugin-markdown/src/libs/utils/sanitize-comments.ts index 692f36aa0..7a37049e6 100644 --- a/packages/typedoc-plugin-markdown/src/libs/utils/sanitize-comments.ts +++ b/packages/typedoc-plugin-markdown/src/libs/utils/sanitize-comments.ts @@ -1,5 +1,4 @@ export function sanitizeComments(str: string) { - const re = /<(?=(?:[^`]*`[^`]*`)*[^`]*$)[^<]+?>/gi; const codeBlocks: string[] = []; const placeholder = '___CODEBLOCKPLACEHOLDER___'; @@ -9,9 +8,11 @@ export function sanitizeComments(str: string) { return placeholder; }); - // Perform escaping outside of code blocks + // If line starts with a > treat it as a blockquote + // Otherwise escape all <, >, {, and } characters str = str - .replace(re, (tags) => tags.replace(/>/g, '\\>').replace(//gm, '\\>') + .replace(/