Skip to content

Commit e740bfc

Browse files
fix: empty callouts erroring out (#1232)
[![PR App][icn]][demo] | Fix RM-XYZ :-------------------:|:----------: ## 🧰 Changes - In readme-to-mdx.ts, do more type checking when checking if a node is an h3 when visiting rdme-callout - Part of the fix is also included in #1230 where we skip wrapping callout children in heading if it's empty ## 🧬 QA & Testing - [Broken on production][prod]. - [Working in this PR app][demo]. --------- Co-authored-by: Kelly Joseph Price <[email protected]>
1 parent 60691dc commit e740bfc

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

__tests__/compilers/callout.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ describe('callouts compiler', () => {
2121
expect(mdx(mdast(markdown))).toBe(markdown);
2222
});
2323

24-
it('compiles callouts with no heading or body!?', () => {
24+
it('compiles callouts with no heading or body', () => {
2525
const markdown = `> 🚧
2626
`;
2727

2828
expect(mdx(mdast(markdown))).toBe(markdown);
2929
});
3030

31+
it('compiles callouts with no heading or body and no new line at the end', () => {
32+
const markdown = '> ℹ️';
33+
34+
expect(mdx(mdast(markdown))).toBe(`${markdown}\n`);
35+
});
36+
3137
it('compiles callouts with markdown in the heading', () => {
3238
const markdown = `> 🚧 It **works**!
3339
>

__tests__/migration/callouts.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { migrate } from '../helpers';
22

33
describe('migrating callouts', () => {
4+
it('does not error on callouts with no heading', () => {
5+
const md = '> ℹ️';
6+
const mdx = migrate(md);
7+
expect(mdx).toMatchInlineSnapshot(`
8+
"<Callout icon="ℹ️" theme="info" />
9+
"
10+
`);
11+
});
12+
413
it('retains HTML content that starts a callout body', () => {
514
const md = `> ⚠️ <div><b>hello</b></div>
615
`;

processor/transform/readme-to-mdx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const readmeToMdx = (): Transform => tree => {
2222

2323
visit(tree, 'rdme-callout', (node, index, parent) => {
2424
const isEmpty = node.data.hProperties?.empty;
25-
const isH3 = node.children[0].type === 'heading' && node.children[0].depth === 3;
25+
const isH3 = node.children?.[0] && 'type' in node.children[0] && node.children[0].type === 'heading' && node.children[0].depth === 3;
2626
let { icon, theme } = node.data.hProperties;
2727
if (!icon) icon = defaultIcons[theme];
2828
if (!theme) theme = themes[icon] || 'default';

0 commit comments

Comments
 (0)