Enable with .UseAutoIdentifiers() (included in UseAdvancedExtensions()).
This extension automatically generates id attributes for all headings, similar to Pandoc auto identifiers. This is essential for linking to specific sections.
Every heading gets an id attribute derived from its text content:
## Getting Started
Renders as:
<h2 id="getting-started">Getting Started</h2>
You can then link to it:
See [Getting Started](#getting-started).
With the default AutoLink option, [Getting Started] also links to the heading.
These implicit heading references do not resolve inside another still-open link or
image bracket. For example, [Read [Getting Started]](/guide) links the entire
label Read [Getting Started] to /guide. Explicit reference definitions such as
[Getting Started]: /guide retain normal CommonMark reference-link behavior.
Examples:
| Heading | Generated ID |
|---|---|
# Hello World |
hello-world |
## C# Tips & Tricks |
c-tips--tricks |
### 2. Installation |
2-installation |
If two headings produce the same ID, a numeric suffix is appended:
## Section
## Section
## Section
Produces: section, section-1, section-2.
UseAutoIdentifiers accepts an AutoIdentifierOptions flags enum:
using Markdig.Extensions.AutoIdentifiers;
var pipeline = new MarkdownPipelineBuilder()
.UseAutoIdentifiers(AutoIdentifierOptions.GitHub)
.Build();
Available options:
| Option | Description |
|---|---|
AutoIdentifierOptions.Default |
Standard auto-identifier behavior |
AutoIdentifierOptions.GitHub |
GitHub-compatible ID generation |
AutoIdentifierOptions.AutoLink |
Also create a self-link anchor |
AutoIdentifierOptions.AllowOnlyAscii |
Strip non-ASCII characters from IDs |
You can override the auto-generated ID using Generic attributes:
## My Heading {#custom-id}
The explicit #custom-id takes precedence over the auto-generated one.