-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (30 loc) · 887 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const toHTML = require('hast-util-to-html')
module.exports = () => tree => {
function dfs(node) {
const jsxChildren = node.children ? node.children.filter(dfs) : []
if (jsxChildren.length > 0 || node.type === 'root') {
for (const child of node.children) {
if (
child.type === 'element' &&
child.children.length > 0 &&
!jsxChildren.includes(child)
) {
// Use dangerouslySetInnerHTML to replace MDXTag
const html = child.children.map(toHTML).join('')
child.properties.dangerouslySetInnerHTML = { __html: html }
child.children.splice(0, child.children.length)
}
}
return true
} else if (
node.type === 'jsx' ||
node.type === 'import' ||
node.type === 'export'
) {
return true
}
return false
}
dfs(tree)
return tree
}