Skip to content
Closed
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
24 changes: 24 additions & 0 deletions __tests__/lib/stripComments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,29 @@ end"`);
expect(output).not.toContain('{/* c */}');
expect(output).not.toContain('deprecated');
});

describe.each([
['legacy', undefined],
['mdx', { mdx: true }],
['mdxish', { mdxish: true }],
])('checkbox behavior for %s', (_description, options) => {
it('should not escape checkboxes', async () => {
const input = '- [ ] Checkbox with text';
const output = await stripComments(input, options);
expect(output).toContain('[ ] Checkbox with text');
});

it('should not escape ticked checkboxes', async () => {
const input = '- [x] Ticked checkbox';
const output = await stripComments(input, options);
expect(output).toContain('[x] Ticked checkbox');
});

it('should retain escaped checkboxes', async () => {
const input = '- \\[ ] Checkbox';
const output = await stripComments(input, options);
expect(output).toContain('\\[ ] Checkbox');
});
});
});
});
19 changes: 15 additions & 4 deletions lib/stripComments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { VARIABLE_REGEXP } from '@readme/variable';
import { gfmTaskListItemFromMarkdown, gfmTaskListItemToMarkdown } from 'mdast-util-gfm-task-list-item';
import { mdxExpressionFromMarkdown, mdxExpressionToMarkdown } from 'mdast-util-mdx-expression';
import { gfmTaskListItem } from 'micromark-extension-gfm-task-list-item';
import { mdxExpression } from 'micromark-extension-mdx-expression';
import remarkMdx from 'remark-mdx';
import remarkParse from 'remark-parse';
Expand All @@ -24,16 +26,25 @@ async function stripComments(doc: string, { mdx, mdxish }: Opts = {}): Promise<s

const processor = unified();

// gfmTaskListItem is required to retain checkboxes in task lists & not
const micromarkExtensions = [gfmTaskListItem()];
const fromMarkdownExtensions = [gfmTaskListItemFromMarkdown()];
const toMarkdownExtensions = [gfmTaskListItemToMarkdown()];

// we still require these two extensions because:
// 1. we can rely on remarkMdx to parse MDXish
// 2. we need to parse JSX comments into mdxTextExpression nodes so that the transformers can pick them up
if (mdxish) {
processor
.data('micromarkExtensions', [mdxExpression({ allowEmpty: true })])
.data('fromMarkdownExtensions', [mdxExpressionFromMarkdown()])
.data('toMarkdownExtensions', [mdxExpressionToMarkdown()]);
micromarkExtensions.push(mdxExpression({ allowEmpty: true }));
fromMarkdownExtensions.push(mdxExpressionFromMarkdown());
toMarkdownExtensions.push(mdxExpressionToMarkdown());
}

processor
.data('micromarkExtensions', micromarkExtensions)
.data('fromMarkdownExtensions', fromMarkdownExtensions)
.data('toMarkdownExtensions', toMarkdownExtensions);

processor
.use(remarkParse)
.use(normalizeEmphasisAST)
Expand Down
10 changes: 7 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@
"lodash.escape": "^4.0.1",
"lodash.kebabcase": "^4.1.1",
"mdast-util-find-and-replace": "^3.0.1",
"mdast-util-gfm-task-list-item": "^2.0.0",
"mdast-util-mdx-expression": "2.0.0",
"mdast-util-phrasing": "^4.1.0",
"mdast-util-to-markdown": "^2.1.2",
"micromark-extension-gfm-task-list-item": "^2.1.0",
"micromark-extension-mdx-expression": "^3.0.1",
"micromark-util-character": "^2.1.1",
"micromark-util-html-tag-name": "^2.0.1",
Expand Down