Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions __tests__/lib/stripComments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
`);
});
});
10 changes: 9 additions & 1 deletion lib/stripComments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface Opts {
mdx?: boolean;
}

const surroundedSyntax = /^([*_]{1,2})[^*]+\1$/;

/**
* Removes Markdown and MDX comments.
*/
Expand All @@ -30,9 +32,15 @@ async function stripComments(doc: string, { mdx }: Opts = {}): Promise<string> {
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);
},
Expand All @@ -42,7 +50,7 @@ async function stripComments(doc: string, { mdx }: Opts = {}): Promise<string> {
// 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;
},
],
},
Expand Down
Loading