Closed
Description
Hello!
I'm running into issue with mdast-util-toc
flattening html value into string:
import u from 'unist-builder'
import mdastToToc from 'mdast-util-toc'
const mdast = u('root', [
u('heading', { depth: 1 }, [
u('text', { value: 'Hello' }),
u('html', { value: '<code>World<code>' })
])
])
const toc = mdastToToc(mdast).map
console.dir(toc, { depth: null })
yeild this result:
{ type: 'list',
...
[ { type: 'link',
title: null,
url: '#hellocodeworldcode',
children: [ { type: 'text', value: 'Hello<code>World<code>' } ]
}]
...
}
Ideally, I would like to receive something like this:
[ { type: 'link',
title: null,
url: '#helloworld',
children: [
{ type: 'text', value: 'Hello' },
{ type: 'html', value: '<code>World<code>' },
]
}]
I found this issue when working on gatsbyjs/gatsby#13608. After extracting a table of contents, it will be transformed into a hast tree via mdast-util-to-hast
and finally, html via hast-util-to-html
. There are plugins in the ecosystem that might modify markdown heading & inject html in them, thus leaving html artifact in final output:
<ul>
<li>
<a href="/demo/#generating-tocs-with-code-classlanguage-textgatsby-transformer-remarkcode\">
Generating TOCs with <code class="language-text">gatsby-transformer-remark</code>
</a>
</li>
</ul>
Would mdast-util-toc
be open to support this behavior?