Skip to content

Commit 60691dc

Browse files
authored
feat: migrate contents inside comments (#1229)
[![PR App][icn]][demo] | Fix RM-XYZ :-------------------:|:----------: ## 🧰 Changes In the migrate function, recursively call the base migrate on contents inside comment blocks ## 🧬 QA & Testing - [Broken on production][prod]. - [Working in this PR app][demo]. [demo]: https://markdown-pr-PR_NUMBER.herokuapp.com [prod]: https://SUBDOMAIN.readme.io [icn]: https://user-images.githubusercontent.com/886627/160426047-1bee9488-305a-4145-bb2b-09d8b757d38a.svg
1 parent bcb0c0a commit 60691dc

File tree

3 files changed

+76
-18
lines changed

3 files changed

+76
-18
lines changed

__tests__/migration/html-comments.test.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ describe('migrating html comments', () => {
55
const md = `
66
<!--
77
8-
8+
99
1010
## Walkthrough
1111
1212
[block:html]
1313
{
14-
"html": "<div style="position: relative; padding-bottom: 56.25%; height: 0;"><iframe src="https://www.loom.com/embed/53dd194717bb4965a8e838b95715ff18" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div>"
14+
"html": "<div style=\\"position: relative; padding-bottom: 56.25%; height: 0;\\"><iframe src=\\"https://www.loom.com/embed/53dd194717bb4965a8e838b95715ff18\\" frameborder=\\"0\\" webkitallowfullscreen mozallowfullscreen allowfullscreen style=\\"position: absolute; top: 0; left: 0; width: 100%; height: 100%;\\"></iframe></div>"
1515
}
1616
[/block]
1717
@@ -25,20 +25,41 @@ describe('migrating html comments', () => {
2525
expect(mdx).toMatchInlineSnapshot(`
2626
"{/*
2727
28-
28+
2929
3030
## Walkthrough
3131
32+
<HTMLBlock>{\`
33+
<div style="position: relative; padding-bottom: 56.25%; height: 0;"><iframe src="https://www.loom.com/embed/53dd194717bb4965a8e838b95715ff18" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div>
34+
\`}</HTMLBlock>
3235
33-
[block:html]
34-
{
35-
"html": "<div style="position: relative; padding-bottom: 56.25%; height: 0;"><iframe src="https://www.loom.com/embed/53dd194717bb4965a8e838b95715ff18" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div>"
36-
}
37-
[/block]
36+
<br />
3837
3938
39+
*/}
40+
"
41+
`);
42+
});
43+
44+
it('converts markdown within html comments', () => {
45+
const md = `
46+
<!--
47+
48+
### Heading inside comment
49+
50+
- a **list** item
51+
52+
-->
53+
`;
54+
55+
const mdx = migrate(md);
56+
expect(mdx).toMatchInlineSnapshot(`
57+
"{/*
58+
59+
### Heading inside comment
60+
61+
* a **list** item
4062
41-
<br />
4263
4364
*/}
4465
"

lib/migrate.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@ import migrateLinkReferences from '../processor/transform/migrate-link-reference
44

55
import mdastV6 from './mdastV6';
66
import mdx from './mdx';
7+
import migrateComments from './utils/migrateComments';
78

8-
const migrate = (doc: string, { rdmd }): string => {
9+
const migrateDoc = (doc: string, { rdmd }): string => {
910
const ast = mdastV6(doc, { rdmd });
1011

11-
return (
12-
mdx(ast, { remarkTransformers: [migrateCallouts, [migrateLinkReferences, { rdmd }], migrateHtmlTags], file: doc })
13-
.replaceAll(/&#x20;/g, ' ')
14-
// @note: I'm not sure what's happening, but I think mdx is converting an
15-
// 'a' to '&#x61;' as a means of escaping it. I think this helps with
16-
// parsing weird cases.
17-
.replaceAll(/&#x61;/g, 'a')
18-
);
12+
return mdx(ast, { remarkTransformers: [migrateCallouts, [migrateLinkReferences, { rdmd }], migrateHtmlTags], file: doc })
13+
.replaceAll(/&#x20;/g, ' ')
14+
// @note: I'm not sure what's happening, but I think mdx is converting an
15+
// 'a' to '&#x61;' as a means of escaping it. I think this helps with
16+
// parsing weird cases.
17+
.replaceAll(/&#x61;/g, 'a');
18+
};
19+
20+
const migrate = (doc: string, opts): string => {
21+
const migratedDoc = migrateDoc(doc, opts);
22+
const migratedDocAndComments = migrateComments(migratedDoc, migrateDoc, opts);
23+
return migratedDocAndComments;
1924
};
2025

2126
export default migrate;

lib/utils/migrateComments.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const COMMENT_BLOCK_REGEX = /{\/\*([\s\S]*?)\*\/}/g;
2+
3+
const migrateComments = (doc: string, migrateDoc: (doc: string, opts) => string, opts): string => {
4+
return doc.replace(COMMENT_BLOCK_REGEX, (match, contents: string) => {
5+
// Preserve leading and trailing whitespace
6+
const leadingWhitespace = contents.match(/^\s*/)?.[0] ?? '';
7+
const trailingWhitespace = contents.match(/\s*$/)?.[0] ?? '';
8+
const inner = contents.slice(leadingWhitespace.length, contents.length - trailingWhitespace.length);
9+
10+
// Skip empty comments
11+
if (!inner.trim()) return match;
12+
13+
// Compile the inner content through the migration pipeline
14+
let compiled: string;
15+
try {
16+
compiled = migrateDoc(inner, opts);
17+
// Trim trailing whitespace only if inner content had no newlines
18+
if (!/\r|\n/.test(inner)) {
19+
compiled = compiled.trimEnd();
20+
}
21+
} catch {
22+
return match;
23+
}
24+
25+
// Recursively process any nested comments
26+
const processed = migrateComments(compiled, migrateDoc, opts);
27+
28+
return `{/*${leadingWhitespace}${processed}${trailingWhitespace}*/}`;
29+
});
30+
};
31+
32+
export default migrateComments;

0 commit comments

Comments
 (0)