how do I turn the Result object table into mdast or hast? #1218
-
I was in the middle of write some custom code to parse a custom directive that is coming as markdown content and has the following syntax:
See the following example of markdown content: # this h1
## this h2
## this h2
### this h3
#### this h4 The idea behind this was to create a TOC for all the h2 (set by min-leve), h3 (set by max-level). On top of my head I should end up with something like:
Doing some research I found this two: This is my current code: const processPageHeadingList = (
node: ContainerDirective | LeafDirective,
index: number,
parent: any
) => {
const minLevel: number = node.attributes?.['min-level'] !== undefined ? parseInt(node.attributes?.['min-level']) : 0
const maxLevel: number = node.attributes?.['max-level'] !== undefined ? parseInt(node.attributes?.['max-level']) : 0
node.data = node.data ?? {}
node.data.hName = 'nav'
node.data.hProperties = {
className: 'page-heading-list',
ariaLabel: 'Page heading list'
}
let headings: any = []
parent.children.map((element: any) => {
if (element.type === 'heading' && element.depth <= maxLevel && element.depth >= minLevel) {
headings.push(element)
}
})
/** @type {import('mdast').Root} */
const tocElement = {
type: 'root',
children: parent.children
}
const table = toc(tocElement)
console.dir(table, { depth: 3 })
}
export default processPageHeadingList and it partially works, however:
Here is a runnable example for the above: https://codesandbox.io/s/remark-directive-forked-tjy5z6 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
I'm not sure what you mean by this.
Reading the documentation is a good starting point https://github.com/syntax-tree/mdast-util-toc#result 🙂 MDAST can be turned into HAST with Or if you are adding additional headings through your remark directives or rehype plugins, you could also consider using |
Beta Was this translation helpful? Give feedback.
The answer remains the same as before
Don't run
toHast
ontable.map
, directly addtable.map
into the MDAST document.