Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node Shape Passed to customizeTOC Not unified Standard #9

Open
shellscape opened this issue May 10, 2022 · 1 comment
Open

Node Shape Passed to customizeTOC Not unified Standard #9

shellscape opened this issue May 10, 2022 · 1 comment

Comments

@shellscape
Copy link

The shape of the node object passed to customizeTOC doesn't match the Node interface as specified by the latest version of unified. This is what the plugin is passing:

{
    type: 'element',
    tagName: 'ol',
    properties: { className: 'toc toc-level toc-level-1' },
    children: [ [Object] ]
  }

This is what unified expects:

interface Node {
  type: string
  data: Data?
  position: Position?
}

As a result, we've no way to export the TOC using that method to stringify it, store the AST separately, etc.

@ChristianMurphy
Copy link

rehype and hast have official typings for the HTML nodes.
You can find them at https://www.npmjs.com/package/@types/hast

The overall plugin can leverage the Plugin type, for example import('unified').Plugin<[Options] | Array<void>, Root>} (for example: https://github.com/rehypejs/rehype-meta/blob/e5eae88704c300393641c71b9e3f59a780fc3ed7/lib/index.js#L145)

Everything in src/types.ts could probably be replaced with @types/hast.
And the guard in src/type-guards.ts could be simplified after @types/hast is in use, there are a few ways to narrow nodes https://unifiedjs.com/learn/recipe/narrow-node-typescript/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants