2022-10-30 15:13:19 -07:00
|
|
|
# `MD051` - Link fragments should be valid
|
2022-10-29 23:21:45 -07:00
|
|
|
|
2022-10-30 15:13:19 -07:00
|
|
|
Tags: `links`
|
2022-10-29 23:21:45 -07:00
|
|
|
|
2022-10-30 15:13:19 -07:00
|
|
|
Aliases: `link-fragments`
|
2022-10-29 23:21:45 -07:00
|
|
|
|
|
|
|
This rule is triggered when a link fragment does not correspond to a heading
|
|
|
|
in the document:
|
|
|
|
|
|
|
|
```markdown
|
|
|
|
# Title
|
|
|
|
|
|
|
|
[Link](#fragment)
|
|
|
|
```
|
|
|
|
|
|
|
|
To fix the issue, change the fragment to reference an existing heading:
|
|
|
|
|
|
|
|
```markdown
|
|
|
|
[Link](#title)
|
|
|
|
```
|
|
|
|
|
|
|
|
Alternatively, an HTML `a` tag with an `id` (or a `name`) attribute defines a
|
|
|
|
valid anchor:
|
|
|
|
|
|
|
|
```markdown
|
|
|
|
<a id="fragment"></a>
|
|
|
|
```
|
|
|
|
|
|
|
|
Some platforms (e.g., [GitHub][github-section-links]) automatically create HTML
|
|
|
|
anchors for every heading. This makes it easy to link to different sections in
|
|
|
|
a document. These internal links can break over time as headings are renamed.
|
|
|
|
|
|
|
|
Note: Creating anchors for headings is not part of the CommonMark specification.
|
|
|
|
|
|
|
|
[github-section-links]: https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#section-links
|