stefanprobst / rehype-extract-toc

MIT License
18 stars 0 forks source link

feat: expose the table of content as AST #2

Closed yordis closed 3 years ago

yordis commented 3 years ago

closes #1

stefanprobst commented 3 years ago

hi, thanks for the pr!

i like the idea of exposing a named export of the toc, only thing i'm unsure about is if this rehype plugin should be adding mdx-specific nodes.

maybe we can separate this out in to its own tiny thing, and make it abailable as rehype-extract-tox/mdx?

mdx users who prefer adding a named export could then use it like this:

import { compile } from "xdm";
import withSlugs from "rehype-slug";
import withExtractedToc from "@stefanprobst/rehype-extract-toc";
import withExportedToc from "@stefanprobst/rehype-extract-toc/mdx";

await compile("some document", {
  rehypePlugins: [
    withSlugs,
    withExtractedToc,
    [withExportedToc, { name: "tableOfContents" }],
  ],
});

that would mean moving what you added into src/mdx.js:

export default function attacher(options) {
  return function transformer(tree, file) {
    if (file.data.toc == null) return;

    /** Add named export exposing table of contents. */
  };
}

what do you think?

yordis commented 3 years ago

Right, right, I love the idea.

yordis commented 3 years ago

Is that what you had in mind?

Side note: wouldn't be easier to use TypeScript compiler instead of maintaining the types separately?

yordis commented 3 years ago

Should I convert the tests to use https://github.com/remcohaszing/rehype-mdx-title/blob/48522de568e88571b587d328b987bac2042dc914/index.test.mjs#L25

stefanprobst commented 3 years ago

hi, i had a quick look at how other mdx plugins do it, and it seems the ecosystem convention is to require non-mdx and mdx plugins to run after one another, e.g. for exporting frontmatter, you have to have both remark-frontmatter and remark-mdx-frontmatter plugins. so i adjusted this here as well and added a section to the readme. added tests as well.

thanks!

stefanprobst commented 3 years ago

published as v2.1.0 :tada: