Skip to content

Dynamic headings in tableofContents? #2280

Closed Answered by delucis
spokospace asked this question in Q&A
Discussion options

You must be logged in to vote

You can’t do this directly in MDX unfortunately — we use Astro’s headings extraction which doesn’t take into account dynamic/component headings.

There are a few alternatives, so it depends a bit exactly where the data object is coming from:

Alternative 1: Use a custom page.

Using a custom page gives you full control over the table of contents, so you can do something like this:

---
// src/pages/example.astro
import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';

const sections = [
  { id: 'section-one', heading: 'Section One', content: '...' },
  { id: 'section-two', heading: 'Section Two', content: '...' },
  // ...
];
---

<StarlightPage
  frontmatter={{ title: 'M…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by spokospace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants