Skip to content

Commit bcb0c0a

Browse files
authored
fix: skip wrapping heading in callouts for non pragraph (#1230)
[![PR App][icn]][demo] | Fix RM-XYZ :-------------------:|:----------: ## 🧰 Changes If a callout starts with an html element, the content gets dropped when migrated. I found that this is because in migrate-callouts.ts, we are wrapping the first children in a heading, assuming the node has a children attribute, and putting the children under the heading. However, the node might be an HTML node (or could be others) that don't have children, as a result the value gets dropped. Current approach to fix is to avoid wrapping the node entirely if it's not a paragraph since the others won't have children. ## 🧬 QA & Testing ``` // Use the migrate() function const markdown = '> ⚠️ <div>hello</div>' const mdx = migrate(markdown); ``` The mdx should retain the <div>hello</div>' - [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 615fe1b commit bcb0c0a

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { migrate } from '../helpers';
2+
3+
describe('migrating callouts', () => {
4+
it('retains HTML content that starts a callout body', () => {
5+
const md = `> ⚠️ <div><b>hello</b></div>
6+
`;
7+
8+
const mdx = migrate(md);
9+
expect(mdx).toMatchInlineSnapshot(`
10+
"<Callout icon="⚠️" theme="warn">
11+
<div><b>hello</b></div>
12+
</Callout>
13+
"
14+
`);
15+
});
16+
});

processor/transform/migrate-callouts.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import { wrapHeading } from './callouts';
77

88
const migrateCallouts: Plugin<[], Root> = (): Transformer<Root> => (tree: Root) => {
99
visit(tree, 'rdme-callout', callout => {
10-
callout.children[0] = wrapHeading(callout);
10+
const firstChild = callout.children?.[0];
11+
// This will retain the value of the node if it is not a paragraph, e.g. an HTML node
12+
if (firstChild && firstChild.type === 'paragraph') {
13+
callout.children[0] = wrapHeading(callout);
14+
}
1115
});
1216

1317
return tree;

0 commit comments

Comments
 (0)