Replies: 4 comments 3 replies
-
Hey!
This is impossible.
There is not one particular recommendation that I have for everybody, no.
There are several steps missing between that abbreviated code you showed, and how you are actually using it. I need to know those steps before I can answer this. |
Beta Was this translation helpful? Give feedback.
-
After some investigation, I found out that React.children.map has a critical drawback of not showing the rendered children of children elements AlternativeCreating a React Context with a |
Beta Was this translation helpful? Give feedback.
-
@wooorm , thx for the quick answer
The main goal here is reusability across different MDX files. I have certain pieces of content that I want to use in different places, so I’ve encapsulated this content in a component (MDX file) for easier maintenance and reuse. Example Structure//parent1.mdx
import { MyComponent } from './my-component.mdx'
# Parent 1
lorem ipsum
<MyComponent /> //parent2.mdx
import { MyComponent } from './my-component.mdx'
# Parent 2
lorem ipsum
<MyComponent /> //my-component.mdx
# My Component
lorem ipsum In our case, the content of the child component includes a header and a paragraph. The challenge for me is how I can get the headers of the child component to be displayed in the TOC of the parent component.
I think I already found out that this is not possible in my comment above. React is not showing the rendered children of children elements. |
Beta Was this translation helpful? Give feedback.
-
@ProchaLu created a top-level table of contents PR based on React Context in Allows for imports of other MDX files, without resorting to use of Even better would be a server-computed version (eg. React Server Components or similar), maybe another time... |
Beta Was this translation helpful? Give feedback.
-
I’m trying to build a top-level Table of Contents (TOC) in a parent MDX file that imports other MDX files as components. I’m using
rehype-toc
to generate the TOC. However, I’m encountering two problems:Expected Behavior:
The parent MDX file should generate a single, unified TOC that includes all headers, including those from nested MDX components
Actual Behavior:
Separate TOCs are generated for each MDX file and the parent TOC does not include headers from imported MDX components
Assumption:
It seems that MDX might be processing each
.mdx
file independently and is unaware of the larger page structure when components are imported.Code Example
Parent MDX File:
Sub MDX File:
Resulting TOC:
Expected TOC:
Workaround attempt
To address these issues, I am experimenting with a recursive function to gather the full MDX content, including nested MDX files:
I attempt to extract headers from this compiled content to create a unified TOC. However, I am encountering issues where the content type in nested MDX files is always
undefined
:Questions:
rehype-toc
or a similar plugin to generate a unified TOC that spans across nested MDX files?undefined
in the recursive function and correctly extract headers from nested MDX files?Beta Was this translation helpful? Give feedback.
All reactions