Skip to content
Merged
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
9 changes: 8 additions & 1 deletion packages/mdx/md-ast-to-mdx-ast.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
const visit = require('unist-util-visit')

const commentOpen = '<!--'
const commentClose = '-->'

module.exports = options => tree => {
visit(tree, 'html', node => {
if (node.value.startsWith('<!--') && node.value.endsWith('-->')) {
if (
node.value.startsWith(commentOpen) &&
node.value.endsWith(commentClose)
) {
node.type = 'comment'
node.value = node.value.slice(commentOpen.length, -commentClose.length)
} else {
node.type = node.mdxType || 'jsx'
}
Expand Down
2 changes: 1 addition & 1 deletion packages/mdx/mdx-hast-to-jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function toJSX(node, parentNode = {}, options = {}) {
}

if (node.type === 'comment') {
return node.value.replace('<!--', '{/*').replace('-->', '*/}')
return `{/*${node.value}*/}`
}

if (node.type === 'import' || node.type === 'export' || node.type === 'jsx') {
Expand Down
12 changes: 12 additions & 0 deletions packages/mdx/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ it('Should render blockquote correctly', async () => {
parse(result)
})

it('Should properly expose comments', async () => {
const result = await mdx('<!--foo-->', {
hastPlugins: [
() => tree => {
tree.children[0].value = 'bar'
}
]
})

expect(result).toContain('{/*bar*/}')
})

it('Should render HTML inside inlineCode correctly', async () => {
const result = await mdx('`<div>`')

Expand Down