How to link to a section or heading in the same Markdown file
Codemia Knowledge Hub ·
Use standard link syntax with a # anchor built from the heading text: [link text](#heading-slug). The slug is the heading lowercased, with spaces replaced by hyphens and special characters removed. This is how every Markdown table of contents works, and it is supported on GitHub, GitLab, VS Code previews and most static site generators.
Linking to a heading
Every heading automatically becomes an anchor target:
#configuration-options comes from ## Configuration Options: lowercased, with the space replaced by a hyphen.
How heading slugs are generated
The rules vary slightly between processors, but the common convention (GitHub, GitLab and most static site generators) is:
- Convert the heading text to lowercase
- Remove all punctuation except hyphens
- Replace each space with a hyphen (
-)
GitHub does not collapse repeated hyphens, so ## Setup - Linux becomes #setup---linux (the hyphen is kept and each space around it adds one more). GitLab documents an extra step that merges consecutive hyphens into one, so the same heading there is #setup-linux.
Differences between processors
| Platform | Punctuation handling | Duplicate headings | Emoji in headings |
| GitHub Flavored Markdown | Removes most punctuation, keeps hyphens | Appends -1, -2, etc. | Strips emoji |
| GitLab | Same as GitHub | Appends -1, -2, etc. | Strips emoji |
| Hugo (Goldmark) | Removes punctuation, keeps hyphens | Appends -1, -2, etc. | Strips emoji |
| Docusaurus | Removes punctuation | Appends -1, -2, etc. | Strips emoji |
| Pandoc | Removes punctuation, prefix with section- optional | Appends -1, -2, etc. | Keeps emoji in ID |
| VS Code preview | Follows CommonMark extensions | Does not deduplicate | Strips emoji |
When in doubt, inspect the id attribute on the heading in the rendered HTML.
Custom anchors with HTML or {#id}
When the generated slug is inconvenient, or you need a target that is not a heading, insert a raw HTML anchor:
This lets you:
- Link to a specific paragraph, not just a heading
- Use short, stable anchor names that survive heading renames
- Link to positions inside code blocks or tables
Some processors also support a heading ID in curly braces:
This works in Pandoc, Hugo, Docusaurus and several others, but not in GitHub Flavored Markdown.
Building a table of contents
A manual table of contents is a list of internal links at the top of the document:
Tools can generate a TOC for you (for example the Markdown All in One extension for VS Code, or TOC plugins in many static site generators), but writing it by hand gives full control over ordering and nesting.
Linking to a section in another file
Combine a file path with an anchor:
These work when browsing a repository on GitHub. In static site generators, the file extension is usually replaced with .html or removed, depending on the URL scheme.
A full document with internal links
The links between sections let readers jump straight to what they need.
Why an internal link does not work
- Uppercase in the anchor. Anchors are case-insensitive in some processors and case-sensitive in others. Use lowercase:
[Link](#my-heading), not[Link](#My-Heading). - Punctuation in the anchor.
## What's New?becomes#whats-new, not#what's-new?. Apostrophes, question marks, periods and colons are stripped. - Duplicate headings. A second
## Overviewbecomes#overview-1, and inserting a new heading can shift the numbering. Use unique headings or manual anchors. - Spaces in anchors. Anchors use hyphens, not spaces or
%20.[Link](#my heading)will not work. - Curly-brace IDs on GitHub.
{#custom-id}is not supported there. Use<a id="custom-id"></a>instead. - Renamed headings. Renaming a heading silently breaks every link to it. Search the document for the old anchor after any heading change.
Related reading
- How to list npm user-installed packages?
- How to list the properties of a JavaScript object?
- How to listen for a WebView finishing loading a URL?
- How to load an image asynchronously?
- How to load CSS Asynchronously
- How to load local html file into UIWebView
- How to load npm modules in AWS Lambda?
- How to make a function wait until a callback has been called using node.js
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.