You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TOC does not work when importing one MDX into another #3915
bugAn error in the Docusaurus core causing instability or issues with its executiondifficulty: advancedIssues that are complex, e.g. large scoping for long-term maintainability.domain: markdownRelated to Markdown parsing or syntax
On our site we have a couple of duplicate doc pages. To avoid repeating content we have one doc as the source of truth then import the content like so:
import Content from './doc1.md';
<Content />
The issue is, the table of contents is not picked up on the pages that import the content. Please let me know if there is a better way to handle duplicate pages and if this usage is just conceptually wrong
It's worth noting that this also happens on my 6 month old version, so I don't think that it's due to anything recent
Reacted by John Knox, François Chalifour, Huan Li, Kushl Agaarwal, Sébastien Lorber, Marika Marszalkowski, Archie Lamb, zhoulf1006, Valentin Laurin, Anonymitaet and 29 more
Reacted by Marika Marszalkowski, Anonymitaet, Leonardo Graboski Veiga, Gabriel T. Nardy (Syed), Bryan Hoang, Ragina Jeon, Grant Colestock, Jeremy Gayed, Ben Perlmutter, Arthur and 14 more
This usage should be supported, but it's a current limitation of the existing system.
Unfortunately, I'm not sure how to implement this technically, and still busy on i18n.
In the meantime I'd recommend to only put raw text in mdx and duplicate the headings. That's not ideal but I don't have any other solution for now 😓
You could also add a build script that copy a file to multiple locations, or use a remark plugin that substitute a placeholder with a content string in multiple docs
@oriooctopus unfortunately I suspect this to be quite complex to implement, and can't even give you much help to do so because I'm not sure how this should be done.
You can look at the code in this folder: packages/docusaurus-mdx-loader/src/remark/toc
If you are comfortable with ASTs and visitors you can give it a try
@slorber I think we can address this by getting docusaurus-mdx-loader to follow jsx tags into their imported sources when traversing the AST.
I did something similar to this before, using the fs api to read the file and getting remark to parse this in-place of the jsx node (so you can continue to recurse into the imported file and visit its nodes).
The only downside in my eyes is that we'd be using the node fs api, which means this package wouldn't work at runtime in the browser anymore (if it does now).
Yes @jknoxville this is what I have in mind: a recursive visitor that is able to "flatten" the TOC structure of multiple MDX docs (and preserve the correct TOC ordering).
This code already runs in node (it's a Webpack loader)
It's scrappy, and not production-ready (currently uses eval for one thing - I'd recommend using babel to parse the imports instead), but it works. Maybe there's a more elegant way to do it.
What I imagine, is modifying search.js to do what that prototype does before it starts visiting headings to assemble its TOC.
I haven't tried this, but don't see why it wouldn't work.
I submitted a PR for Docusaurus v2, that I'll be willing to work on, but in the meantime, until it is accepted, I made a quick drop in replacement remark plugin that fixes this behaviour: @akopyl/docusaurus-toc-patcher.
Nice feature, do you plan to also support headings from custom JSX/TSX components to appear in ToC ? I have a React component that reads a JSON file and create headings and paragraphs based on it (the JSON file is loaded as a module but I guess it means the ToC should be generated dynamically at runtime and not at compile time?)
@jbltx we only support importing other MDX files atm
However, it could be technically possible to also support React imported components, and try to get the compiler to output something looking like this:
@felipecrs this is unlikely we will ever support this. The TOC is computed statically at MDX compilation time. We are not running the JS to see what the output is and if it contains any heading. Hence we don't support such loop, nor using props inside partial headings.
If you want that, you'd have to use the TOC function form described just above, a possible enhancement that we haven't implemented yet.
@felipecrs we don't support a toc function yet, only a toc array export.
You should rather do that instead of a loop
importReleaseV1from"./_releaseV1.mdx"importReleaseV2from"./_releaseV2.mdx"importReleaseV3from"./_releaseV3.mdx"# Releases## Top heading
<ReleaseV1/>
<ReleaseV2/>
<ReleaseV3/>
Note, we don't support "dynamic headings" such as ## Release {props.version}, so you will need to provide a TOC export if you want dynamic headings (you can provide the toc export either on the root doc or the partial). There's also a non-implemented proposal to solve that problem that you can track here: #9773
Unfortunately, this issue still isn't fixed, and I can add a note that <h1>..<h6> tags aren't added either. I know this behavior is described in the docs (that only MD headings go to the ToC), but I have a proposal.
What if the ToC were fully a client-side JS component?
This right ToC sidebar has nothing to do with either SEO or the main content, so we can safely remove it from static generation.
Then, a JS script added to the page could scan all the headings on the page and build the ToC on the fly!
The ToC would contain headings:
included from any .tsx or .mdx components
defined in MD or HTML formats
inside or outside React code
No complicated server-side parsing, no struggling with preprocessing sequences.
It would be better to allow components to exclude certain H1..H6 elements from the ToC if needed using an attribute like data-include-to-toc="false", but include every existing heading written in all possible forms in the ToC by default.
The issue as originally described is fixed. If something is not, that must be something slightly different, and you'd rather provide a repro so that we can understand your exact setup.
I can add a note that <h1>..<h6> tags aren't added either.
We only add to TOC Markdown headings, not JSX nodes that produce headings.
If you think that could be useful, feel free to open a separate proposal. It isn't really related to this issue which is about importing one MDX into another.
I know this behavior is described in the docs (that only MD headings go to the ToC), but I have a proposal.
What if the ToC were fully a client-side JS component? This right ToC sidebar has nothing to do with either SEO or the main content, so we can safely remove it from static generation.
Then, a JS script added to the page could scan all the headings on the page and build the ToC on the fly!
The ToC would contain headings:
included from any .tsx or .mdx components
defined in MD or HTML formats
inside or outside React code
No complicated server-side parsing, no struggling with preprocessing sequences.
It would be better to allow components to exclude certain H1..H6 elements from the ToC if needed using an attribute like data-include-to-toc="false", but include every existing heading written in all possible forms in the ToC by default.
Any pitfalls? What do you think?
We purposefully don't want that.
Having the TOC magically appear after the page loads and React hydrates wouldn't give a very good UX.
Note that if you believe this tradeoff is acceptable for your site, you can already swizzle the TOC component and build this yourself in a React useEffect instead of using the TOC items provided as props.
Since this is possible to achieve in userland today, I'd suggest that you code this on your site, deploy it to production, and send us the link for the community to see how it behaves.
If there's traction for your proposed solution after seeing it in action, we could eventually design a "toc mode" feature that allows you to opt in for client-side generation. I doubt we'll make it the default mode, though.
bugAn error in the Docusaurus core causing instability or issues with its executiondifficulty: advancedIssues that are complex, e.g. large scoping for long-term maintainability.domain: markdownRelated to Markdown parsing or syntax
Activity
slorber commentedon Dec 14, 2020
This usage should be supported, but it's a current limitation of the existing system.
Unfortunately, I'm not sure how to implement this technically, and still busy on i18n.
In the meantime I'd recommend to only put raw text in mdx and duplicate the headings. That's not ideal but I don't have any other solution for now 😓
You could also add a build script that copy a file to multiple locations, or use a remark plugin that substitute a placeholder with a content string in multiple docs
oriooctopus commentedon Dec 15, 2020
How difficult would it be to fix? I could possibly put up a PR
slorber commentedon Dec 15, 2020
@oriooctopus unfortunately I suspect this to be quite complex to implement, and can't even give you much help to do so because I'm not sure how this should be done.
You can look at the code in this folder: packages/docusaurus-mdx-loader/src/remark/toc
If you are comfortable with ASTs and visitors you can give it a try
jknoxville commentedon Dec 16, 2020
@slorber I think we can address this by getting docusaurus-mdx-loader to follow jsx tags into their imported sources when traversing the AST.
I did something similar to this before, using the
fsapi to read the file and getting remark to parse this in-place of the jsx node (so you can continue to recurse into the imported file and visit its nodes).The only downside in my eyes is that we'd be using the node fs api, which means this package wouldn't work at runtime in the browser anymore (if it does now).
Do you think this would be an acceptable change?
slorber commentedon Dec 16, 2020
Yes @jknoxville this is what I have in mind: a recursive visitor that is able to "flatten" the TOC structure of multiple MDX docs (and preserve the correct TOC ordering).
This code already runs in node (it's a Webpack loader)
oriooctopus commentedon Jan 19, 2021
@jknoxville do you still have any interest in fixing this? If not, maybe we could sync on what you had in mind and I might be able to implement it?
jknoxville commentedon Jan 20, 2021
@oriooctopus I'm interested, but it's not top of my queue right now, so feel free to take a stab!
Here's a prototype I made a while ago, for a remark plugin that effectively re-implements mdx transclusions by parsing the target of any imported markdown files, and then replacing any JSX nodes that use it, with the nodes of the target AST.
https://gist.github.com/jknoxville/8ed4cb8bcef348362a99dd70bb8006ae
It's scrappy, and not production-ready (currently uses eval for one thing - I'd recommend using babel to parse the imports instead), but it works. Maybe there's a more elegant way to do it.
What I imagine, is modifying search.js to do what that prototype does before it starts visiting headings to assemble its TOC.
I haven't tried this, but don't see why it wouldn't work.
slorber commentedon Jan 20, 2021
if that helps I remembered that Gatsby has a way to extract all imports/exports from MDX.
https://github.com/gatsbyjs/gatsby/blob/2e42197025e2e1bac06c721c3cc44135bf8ef526/packages/gatsby-plugin-mdx/utils/gen-mdx.js#L199
Laptopmini commentedon Apr 30, 2021
slorber commentedon Apr 30, 2021
Laptopmini commentedon Apr 30, 2021
Laptopmini commentedon May 4, 2021
sweeneyskirt-sl commentedon May 5, 2021
41 remaining items
anatolykopyl commentedon Dec 29, 2023
I submitted a PR for Docusaurus v2, that I'll be willing to work on, but in the meantime, until it is accepted, I made a quick drop in replacement remark plugin that fixes this behaviour:
@akopyl/docusaurus-toc-patcher.It implements the logic from shuding/nextra#2199.
slorber commentedon Jan 19, 2024
This has been fixed (main/canary) as part of #9684, and will be released in Docusaurus v3.2
Thanks for the inspiration @dimaMachina and the initial port @anatolykopyl 🤗
jbltx commentedon Jan 29, 2024
Nice feature, do you plan to also support headings from custom JSX/TSX components to appear in ToC ? I have a React component that reads a JSON file and create headings and paragraphs based on it (the JSON file is loaded as a module but I guess it means the ToC should be generated dynamically at runtime and not at compile time?)
slorber commentedon Feb 1, 2024
@jbltx we only support importing other MDX files atm
However, it could be technically possible to also support React imported components, and try to get the compiler to output something looking like this:
Note that in any case, it would be your responsibility to implement the
tocfunction attribute:We can't execute the React code and infer the TOC from that execution.
Would that be a helpful addition, or is this too limited?
jbltx commentedon Feb 2, 2024
Thank you for the suggestion @slorber, I think your example is enough for my use case indeed! 👍
felipecrs commentedon Apr 12, 2024
I don't know if I'm doing something wrong, but this is still not working for me, even after the fix:
_release.mdx:## Release {props.release}releases.mdx:And the result:
slorber commentedon Apr 12, 2024
@felipecrs this is unlikely we will ever support this. The TOC is computed statically at MDX compilation time. We are not running the JS to see what the output is and if it contains any heading. Hence we don't support such loop, nor using props inside partial headings.
If you want that, you'd have to use the TOC function form described just above, a possible enhancement that we haven't implemented yet.
felipecrs commentedon Apr 12, 2024
@slorber I see.
Implementing my own
tocfunction is 100% acceptable for my use case, I would appreciate this capability.felipecrs commentedon Apr 12, 2024
Ops, sorry, did you mean that I could already implement my
tocfunction?I tried it with:
And then the toc disappeared entirely. (Also tried
toc = []like in one of the examples)slorber commentedon Apr 13, 2024
@felipecrs we don't support a toc function yet, only a toc array export.
You should rather do that instead of a loop
Note, we don't support "dynamic headings" such as
## Release {props.version}, so you will need to provide a TOC export if you want dynamic headings (you can provide the toc export either on the root doc or the partial). There's also a non-implemented proposal to solve that problem that you can track here: #9773felipecrs commentedon Apr 13, 2024
Great. Thank you, I'll watch the pull request, because my data is very dynamic so I can't define them statically.
remove warning from partial docs
todo0123 commentedon Oct 16, 2025
Hi guys,
Unfortunately, this issue still isn't fixed, and I can add a note that
<h1>..<h6>tags aren't added either. I know this behavior is described in the docs (that only MD headings go to the ToC), but I have a proposal.What if the ToC were fully a client-side JS component?
This right ToC sidebar has nothing to do with either SEO or the main content, so we can safely remove it from static generation.
Then, a JS script added to the page could scan all the headings on the page and build the ToC on the fly!
The ToC would contain headings:
No complicated server-side parsing, no struggling with preprocessing sequences.
It would be better to allow components to exclude certain H1..H6 elements from the ToC if needed using an attribute like
data-include-to-toc="false", but include every existing heading written in all possible forms in the ToC by default.Any pitfalls?
What do you think?
slorber commentedon Oct 17, 2025
The issue as originally described is fixed. If something is not, that must be something slightly different, and you'd rather provide a repro so that we can understand your exact setup.
We only add to TOC Markdown headings, not JSX nodes that produce headings.
If you think that could be useful, feel free to open a separate proposal. It isn't really related to this issue which is about importing one MDX into another.
We purposefully don't want that.
Having the TOC magically appear after the page loads and React hydrates wouldn't give a very good UX.
Note that if you believe this tradeoff is acceptable for your site, you can already swizzle the TOC component and build this yourself in a React
useEffectinstead of using the TOC items provided as props.Since this is possible to achieve in userland today, I'd suggest that you code this on your site, deploy it to production, and send us the link for the community to see how it behaves.
If there's traction for your proposed solution after seeing it in action, we could eventually design a "toc mode" feature that allows you to opt in for client-side generation. I doubt we'll make it the default mode, though.