typedoc-plugin-markdown
Version:
A TypeDoc plugin that generates TypeScript API documentation as Markdown.
22 lines (21 loc) • 889 B
JavaScript
export function sanitizeComments(str) {
const codeBlocks = [];
const placeholder = '___CODEBLOCKPLACEHOLDER___';
// Replace code blocks (backtick and tilde fences) and inline code with
// placeholders. Indented lines are not protected: MDX has no indented code
// blocks, so braces there must still be escaped.
str = str.replace(/(```[\s\S]*?```|~~~[\s\S]*?~~~|`[^`]*?`)/g, (match) => {
codeBlocks.push(match);
return placeholder;
});
// If line starts with a > treat it as a blockquote
// Otherwise escape all <, >, {, and } characters
str = str
.replace(/(?!^)>/gm, '\\>')
.replace(/</g, '\\<')
.replace(/\{/g, '\\{')
.replace(/\}/g, '\\}');
// Replace placeholders with original code blocks
str = str.replace(new RegExp(placeholder, 'g'), () => codeBlocks.shift() || '');
return str;
}