@@ -16,6 +16,7 @@ Use Markdoc defaults out of the box or configure Markdoc schema to your needs.
1616 - [ Relative imports] ( #relative-imports )
1717- [ Preprocessor Options] ( #preprocessor-options )
1818 - [ Functions] ( #functions )
19+ - [ Heading IDs] ( #heading-ids )
1920 - [ Nodes] ( #nodes )
2021 - [ Partials] ( #partials )
2122 - [ Tags] ( #tags )
@@ -270,6 +271,7 @@ const config = {
270271| ` components ` | string | ` "$lib/components" ` | Svelte components directory for custom nodes and tags |
271272| ` extensions ` | string[ ] | ` [".mdoc", ".md"] ` | Files to process with Markdoc |
272273| ` functions ` | Config[ 'functions'] | - | [ Functions config] ( #functions ) |
274+ | ` headingIds ` | boolean | ` false ` | Add IDs to headings without them and include them in export |
273275| ` layout ` | string | - | Default layout for all processed Markdown files |
274276| ` linkify ` | boolean | ` false ` | Auto-convert bare URLs to links |
275277| ` nodes ` | Config[ 'nodes'] | - | [ Nodes config] ( #nodes ) |
@@ -313,6 +315,16 @@ title: Hello World
313315This is a {% uppercase(markdown) %} file that is processed by `markdoc-svelte`.
314316```
315317
318+ ### Heading IDs
319+
320+ If you want to build a table of contents for a page or just have links to specific headings, set ` headingIds ` to `true.
321+ You can add your own IDs in the original file with [ annotations] ( https://markdoc.dev/docs/syntax#annotations )
322+ or they are generated automatically.
323+ Each heading element in the generated HTML has an ` id ` attribute you can use to link to directly.
324+
325+ Each page then also exports a ` headings ` property: a list of all headings with their text, level, and ID.
326+ Use the list to generate a [ table of contents] ( #page-table-of-contents ) .
327+
316328### Nodes
317329
318330[ Nodes] ( https://markdoc.dev/docs/nodes ) are elements built into Markdown from the CommonMark specification.
@@ -525,16 +537,16 @@ See the example [custom node](#nodes).
525537
526538### Page table of contents
527539
528- Each proccessed page automatically exports a ` headings ` property with all headings on the page and IDs for each.
529- Add IDs with [ annotations ] ( https://markdoc.dev/docs/syntax#annotations ) or they are generated automatically .
540+ When you have the [ ` headingIds ` option ] ( #heading-ids ) set to ` true ` ,
541+ each proccessed page automatically exports a ` headings ` property with all headings on the page and IDs for each .
530542Use this list to generate a table of contents for the page, as in the following example:
531543
532544``` svelte
533545<script lang="ts">
534546 let { data } = $props();
535547 const { frontmatter, headings } = data.page;
536548
537- // Filter only h1 and h2 headings
549+ // Include only h1 and h2 headings
538550 const filteredHeadings = headings?.filter((heading) => heading.level <= 2) ?? [];
539551</script>
540552
0 commit comments