-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[MDX] headings
prop not being properly populated with nested <Content />
#10447
Comments
That solution doesn't work with how Astro works. Astro components compile to a function similar to this: function App() {
const props = Astro.props;
return html`<h1>Title</h1>
<OtherComponent />
`
} I hope that illustrates things. We don't render the component and then hand you props, the props are used to determine what to render. So I don't think we could implement what you're hoping for here. |
Hey @matthewp, first of all, thanks for the quick response! If this is something that cannot be done due to technical limitations on Astro's side then that's unfortunate, I really don't know enough about Astro's internals either to think of any ways to solve this. I did however learn than in the meanwhile I can do: ---
import * as Foo from "./foo.mdx";
import * as Bar from "./bar.mdx";
const merged = Foo.getHeadings().concat(Bar.getHeadings());
console.log(merged);
---
<Foo.Content />
<Bar.Content /> Which results in: [
{ depth: 2, slug: 'foo', text: 'Foo' },
{ depth: 2, slug: 'bar', text: 'Bar' }
] It isn't the best solution because you need to manually retrieve and merge headings but it's atleast something that I can use for now. I see you've labeled it as requiring discussion so I'll just wait for that. |
We decided this is a documentation issue and will be updating the docs on MDX. |
PR is here: withastro/docs#7409 Closing as this is not an Astro bug. Glad you found another way! |
Astro Info
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
It's easier to explain the issue with a quick example, let's say we have the following:
Layout.astro
foo.mdx
## Foo
bar.mdx
When rendering the
bar.mdx
file we get the following:This is great, we now have a reusable markdown file:
foo.mdx
that we can import and use in multiple places.Now we want to generate a Table Of Contents for
bar.mdx
.Astro's MDX integration has a built in feature that passes a
headings
prop into the layout that the MDX files are using, this means thatLayout.astro
will have aheadings
prop available.This is what we get back when doing
console.log(Astro.props.headings)
:This is weird, we are only getting back 1 heading while the page actually has 2 headings.
This happens because the
headings
prop from Astro is populated through arehype/remark
plugin that only checks for headings in the.mdx
/.md
file that is being processed, but it does not check for any imported.mdx/md
content that could also have headings, for example:Docusaurus, a documentation website builder similar to Starlight, had the same issue which can be viewed here: facebook/docusaurus#3915
They ended up solving it by recursively importing and scanning markdown inside their plugin to grab any nested headings, the same solution could be applied here.
I've actually created a proof of concept plugin here which is in a working state but fails to cover some edge cases:
https://github.com/Hugos68/remark-astro-headings
What's the expected result?
When importing
.mdx
/.md
I expect theheadings
prop to be populated correctly accounting for headings that might be inside imported<Content />
.Link to Minimal Reproducible Example
https://github.com/Hugos68/astro-headings-issue-repro
Participation
The text was updated successfully, but these errors were encountered: