diff --git a/__tests__/lib/stripComments.test.ts b/__tests__/lib/stripComments.test.ts index 108a3070b..227c9c2e2 100644 --- a/__tests__/lib/stripComments.test.ts +++ b/__tests__/lib/stripComments.test.ts @@ -118,4 +118,49 @@ Second code block const output = await stripComments(input); expect(output).toBe(input.trim()); }); + + it.only('allows compact headings with no whitespace delimiter', async () => { + const input = ` +#Blue +\\# Literal +# Black`; + + await expect(stripComments(input)).resolves.toMatchInlineSnapshot(` + "# Blue + \\# Literal + # Black" + `); + }); + + it.only('allows leading/trailing spaces between bold/italic markers', async () => { + const input = ` +single line with **bold ** text and \\*literal\\* asterisks. + +**bold** +** leading** +**trailing ** + +__emphasis__ +__ leading__ +__trailing __ + +\\*literal\\* +end" +`; + + await expect(stripComments(input)).resolves.toMatchInlineSnapshot(` + "single line with **bold** and \\*literal\\* asterisks. + + **bold** + ** leading** + **trailing ** + + **emphasis** + __ leading__ + __trailing __ + + \\*literal\\* + end" + `); + }); }); diff --git a/lib/stripComments.ts b/lib/stripComments.ts index bf5585b8d..d021f410d 100644 --- a/lib/stripComments.ts +++ b/lib/stripComments.ts @@ -12,6 +12,8 @@ interface Opts { mdx?: boolean; } +const surroundedSyntax = /^([*_]{1,2})[^*]+\1$/; + /** * Removes Markdown and MDX comments. */ @@ -30,9 +32,15 @@ async function stripComments(doc: string, { mdx }: Opts = {}): Promise { handlers: { // Preserve <<...>> variables without escaping any angle brackets. text(node, _, state, info) { + const testValue = node.value.trim(); + console.log(node, `"${testValue}"`); + // If text contains <<...>> pattern, return as is. if (new RegExp(VARIABLE_REGEXP).test(node.value)) return node.value; + // Don't escape special markdown characters like #, *, or _. + if (surroundedSyntax.test(testValue)) return node.value; + // Otherwise, handle each text node normally. return state.safe(node.value, info); }, @@ -42,7 +50,7 @@ async function stripComments(doc: string, { mdx }: Opts = {}): Promise { // Our markdown renderer uses this to group these code blocks into a tabbed interface. (left, right) => { // 0 = no newline between blocks - return (left.type === 'code' && right.type === 'code') ? 0 : undefined; + return left.type === 'code' && right.type === 'code' ? 0 : undefined; }, ], },