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
|
|
|
|
2022-11-05 16:49:38 -07:00
|
|
|
This rule is triggered when a link fragment does not match any of the fragments
|
|
|
|
that are automatically generated for headings in a document:
|
2022-10-29 23:21:45 -07:00
|
|
|
|
|
|
|
```markdown
|
|
|
|
# Title
|
|
|
|
|
|
|
|
[Link](#fragment)
|
|
|
|
```
|
|
|
|
|
2022-11-05 16:49:38 -07:00
|
|
|
To fix this issue, change the link fragment to reference an existing heading:
|
2022-10-29 23:21:45 -07:00
|
|
|
|
|
|
|
```markdown
|
2022-11-05 16:49:38 -07:00
|
|
|
# Title
|
|
|
|
|
2022-10-29 23:21:45 -07:00
|
|
|
[Link](#title)
|
|
|
|
```
|
|
|
|
|
2022-11-05 16:49:38 -07:00
|
|
|
Alternatively, an HTML `a` tag with an `id` or a `name` attribute can be used to
|
|
|
|
define a fragment:
|
2022-10-29 23:21:45 -07:00
|
|
|
|
|
|
|
```markdown
|
2022-11-05 16:49:38 -07:00
|
|
|
<a id="bookmark"></a>
|
|
|
|
|
|
|
|
[Link](#bookmark)
|
2022-10-29 23:21:45 -07:00
|
|
|
```
|
|
|
|
|
2022-11-05 16:49:38 -07:00
|
|
|
An `a` tag can be useful in scenarios where a heading is not appropriate or for
|
|
|
|
control over the text of the fragment identifier.
|
|
|
|
|
2022-11-05 17:34:37 -07:00
|
|
|
Rationale: [GitHub section links][github-section-links] are created
|
|
|
|
automatically for every heading when Markdown content is displayed on GitHub.
|
|
|
|
This makes it easy to link directly to different sections within a document.
|
|
|
|
However, section links change if headings are renamed or removed. This rule
|
|
|
|
helps identify broken section links within a document.
|
2022-10-29 23:21:45 -07:00
|
|
|
|
2022-11-05 16:49:38 -07:00
|
|
|
Section links are **not** part of the CommonMark specification. This rule
|
|
|
|
enforces the [GitHub heading algorithm][github-heading-algorithm] which is:
|
2022-11-05 17:34:37 -07:00
|
|
|
convert heading to lowercase, remove punctuation, convert spaces to dashes,
|
|
|
|
append an incrementing integer as needed for uniqueness.
|
2022-10-29 23:21:45 -07:00
|
|
|
|
|
|
|
[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
|
2022-11-05 16:49:38 -07:00
|
|
|
[github-heading-algorithm]: https://github.com/gjtorikian/html-pipeline/blob/main/lib/html/pipeline/toc_filter.rb
|