Headings and Table of contents
Markdown headingsβ
You can use regular Markdown headings.
## Level 2 title
### Level 3 title
#### Level 4 title
Each Markdown heading will appear as a table of contents entry.
Heading IDsβ
Each heading has an ID that can be automatically generated or explicitly specified. Heading IDs allow you to link to a specific document heading in Markdown or JSX:
[link](#heading-id)
<Link to="#heading-id">link</Link>
By default, Docusaurus will generate heading IDs for you, based on the heading text. For example, ### Hello World
will have id hello-world
.
Generated IDs have some limitations:
- The ID might not look good
- You might want to change or translate the text without updating the existing ID
A special Markdown syntax lets you set an explicit heading id:
### Hello World {#my-explicit-id}
tip
Use the write-heading-ids CLI command to add explicit ids to all your Markdown documents.
Avoid colliding IDs
Generated heading IDs will be guaranteed to be unique on each page, but if you use custom IDs, make sure each one appears exactly once on each page, or there will be two DOM elements with the same ID, which is invalid HTML semantics, and will lead to one heading being unlinkable.
Inline table of contentsβ
Each Markdown document displays a table of contents on the top-right corner. But it is also possible to display an inline table of contents directly inside a markdown document, thanks to MDX.
The toc
variable is available in any MDX document and contains all the headings of an MDX document. By default, only h2
and h3
headings are displayed in the TOC. You can change which heading levels are visible by setting minHeadingLevel
or maxHeadingLevel
for individual TOCInline
components.
import TOCInline from '@theme/TOCInline';
<TOCInline toc={toc} />
The toc
global is just a list of heading items:
declare const toc: Array<{
value: string;
id: string;
level: number;
}>;
Note that the toc
global is a flat array, so you can easily cut out unwanted nodes or insert extra nodes, and create a new TOC tree.
import TOCInline from '@theme/TOCInline';
<TOCInline
// Only show h2 and h4 headings
toc={toc.filter((node) => node.level === 2 || node.level === 4)}
minHeadingLevel={2}
// Show h4 headings in addition to the default h2 and h3 headings
maxHeadingLevel={4}
/>
Customizing table of contents generationβ
The table-of-contents is generated by parsing the Markdown source with a Remark plugin. There are known edge-cases where it generates false-positives and false-negatives.
Markdown headings within hideable areas will still show up in the TOC. For example, headings within Tabs
and details
will not be excluded.
Non-markdown headings will not show up in the TOC. This can be used to your advantage to tackle the aforementioned issue.
<details>
<summary>Some details containing headings</summary>
<h2 id="#heading-id">I'm a heading that will not show up in the TOC</h2>
Some content...
</details>
The ability to ergonomically insert extra headings or ignore certain headings is a work-in-progress. If this feature is important to you, please report your use-case in this issue.
caution
Below is just some dummy content to have more table of contents items available on the current page.
Example Section 1β
Lorem ipsum
Example Subsection 1 aβ
Lorem ipsum
Example subsubsection 1 a Iβ
Example subsubsection 1 a IIβ
Example subsubsection 1 a IIIβ
Example Subsection 1 bβ
Lorem ipsum
Example subsubsection 1 b Iβ
Example subsubsection 1 b IIβ
Example subsubsection 1 b IIIβ
Example Subsection 1 cβ
Lorem ipsum
Example subsubsection 1 c Iβ
Example subsubsection 1 c IIβ
Example subsubsection 1 c IIIβ
Example Section 2β
Lorem ipsum
Example Subsection 2 aβ
Lorem ipsum
Example subsubsection 2 a Iβ
Example subsubsection 2 a IIβ
Example subsubsection 2 a IIIβ
Example Subsection 2 bβ
Lorem ipsum
Example subsubsection 2 b Iβ
Example subsubsection 2 b IIβ
Example subsubsection 2 b IIIβ
Example Subsection 2 cβ
Lorem ipsum
Example subsubsection 2 c Iβ
Example subsubsection 2 c IIβ
Example subsubsection 2 c IIIβ
Example Section 3β
Lorem ipsum
Example Subsection 3 aβ
Lorem ipsum
Example subsubsection 3 a Iβ
Example subsubsection 3 a IIβ
Example subsubsection 3 a IIIβ
Example Subsection 3 bβ
Lorem ipsum
Example subsubsection 3 b Iβ
Example subsubsection 3 b IIβ
Example subsubsection 3 b IIIβ
Example Subsection 3 cβ
Lorem ipsum