Skip to content
This repository was archived by the owner on Nov 3, 2019. It is now read-only.

Include JSX headings in table of contents field #140

Closed
chasemccoy opened this issue Sep 17, 2018 · 10 comments
Closed

Include JSX headings in table of contents field #140

chasemccoy opened this issue Sep 17, 2018 · 10 comments

Comments

@chasemccoy
Copy link

Right now, you can query an MDX node to get a table of contents that is built using the markdown headers in the MDX file.

It would be really nice if this would also include headers rendered inside of JSX. I have scenarios where I want to put a header inside of a React component, but right now that will break my table of contents because gatsby-mdx only picks up Markdown headers.

@ChristopherBiscardi
Copy link
Owner

hmm, I suppose we could accomplish this by rendering out the MDX content in full, converting back to mdast and then running get-table-of-content on it. Probably better than what we do today.

@avigoldman
Copy link
Contributor

If we are adding the html field described in #139, this should be a relatively straightforward change.

@chasemccoy
Copy link
Author

This is awesome. I know I keep saying this, but this really is the last thing keeping me from shipping mdx integration with my company's new, Gatsby-built design system. Would love to see this added!

@ChristopherBiscardi
Copy link
Owner

Master now has a working html field so we can move on this

@chasemccoy
Copy link
Author

@ChristopherBiscardi Trying to dig in and see if I can make this happen now that the html field exists. Looking at this:

tableOfContents: {
type: GraphQLJSON,
args: {
maxDepth: {
type: GraphQLInt,
default: 6
}
},
async resolve(mdxNode, { maxDepth }) {
const { mdast } = await processMDX({ node: mdxNode });
const toc = generateTOC(mdast, maxDepth);
return getTableOfContents(toc.map, {});
}
},

The generateTOC function expects an mdast object. For the sake of simplicity, is there a way to take the contents of the html field and convert it back into an mdast object so we can use that here?

@chasemccoy
Copy link
Author

Looks like there is a utility for going from HAST to MDAST but I am not sure how to get the HTML string into HAST...

@ChristopherBiscardi
Copy link
Owner

You can use rehype-parse to get from HTML to hast

@chasemccoy
Copy link
Author

Looks like that requires adding unified as a dependency as well as rehype-parse, that cool?

@ChristopherBiscardi
Copy link
Owner

yeah, it's already a dependency of mdx and we're not moving away from unified anytime soon (or ever really).

@ChristopherBiscardi
Copy link
Owner

This might be possible in the future due to gatsbyjs/gatsby#13061

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

Successfully merging a pull request may close this issue.

3 participants