Skip to content

Commit 20dc6d0

Browse files
committed
added a test
1 parent 7123270 commit 20dc6d0

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/html/html-to-mdx-ast.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,4 +752,52 @@ describe('parseHtmlToMdxAst with markdown processor', () => {
752752
])
753753
})
754754
})
755+
756+
test('block-level HTML elements as direct children of root should be flow elements', () => {
757+
// Test that wrapper outer HTML elements that are direct children of root
758+
// should be flow elements, not text elements
759+
const htmlWithNestedBlocks = htmlToMdxAst({
760+
html: `<div class="wrapper">
761+
<h1>Title</h1>
762+
<p>Some content</p>
763+
<section>
764+
<h2>Subtitle</h2>
765+
</section>
766+
</div>
767+
<article>
768+
<header>Header</header>
769+
<main>Content</main>
770+
</article>`,
771+
parentType: 'root'
772+
})
773+
774+
// All block-level elements at root should be flow elements
775+
expect(htmlWithNestedBlocks).toHaveLength(2)
776+
777+
// First element: div wrapper
778+
expect(htmlWithNestedBlocks[0]).toMatchObject({
779+
type: 'mdxJsxFlowElement',
780+
name: 'div'
781+
})
782+
783+
// Second element: article
784+
expect(htmlWithNestedBlocks[1]).toMatchObject({
785+
type: 'mdxJsxFlowElement',
786+
name: 'article'
787+
})
788+
789+
// Test with various block-level tags
790+
const blockTags = ['div', 'article', 'section', 'header', 'main', 'footer', 'aside', 'nav']
791+
blockTags.forEach(tag => {
792+
const result = htmlToMdxAst({
793+
html: `<${tag}>Content</${tag}>`,
794+
parentType: 'root'
795+
})
796+
expect(result).toHaveLength(1)
797+
expect(result[0]).toMatchObject({
798+
type: 'mdxJsxFlowElement',
799+
name: tag
800+
})
801+
})
802+
})
755803
})

src/safe-mdx.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ export class MdastToJsx {
287287
)
288288
}
289289
default: {
290-
return this.mdastTransformer(node, 'mdxJsxElement')
290+
return this.mdastTransformer(node, 'mdxJsxFlowElement')
291291
}
292292
}
293293
}
@@ -571,7 +571,7 @@ export class MdastToJsx {
571571
return res
572572
}
573573

574-
mdastTransformer(node: MyRootContent, parentType?: string): ReactNode {
574+
mdastTransformer(node: MyRootContent, parentType: string): ReactNode {
575575
if (!node) {
576576
return []
577577
}
@@ -944,7 +944,7 @@ export class MdastToJsx {
944944
})
945945

946946
// Process the MDX AST nodes
947-
return mdxAst.map(child => this.mdastTransformer(child, parentType))
947+
return mdxAst.map(child => this.mdastTransformer(child, 'html'))
948948
}
949949
case 'imageReference': {
950950
return []

0 commit comments

Comments
 (0)