Skip to content

html value being turned into string #56

Closed
@d4rekanguok

Description

@d4rekanguok

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 &#x3C;code class="language-text">gatsby-transformer-remark&#x3C;/code>
    </a>
  </li>
</ul>

Would mdast-util-toc be open to support this behavior?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions