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
|
|
|
|
2024-10-08 22:40:11 -07:00
|
|
|
Parameters:
|
|
|
|
|
|
|
|
- `ignore_case`: Ignore case of fragments (`boolean`, default `false`)
|
|
|
|
|
2022-12-16 13:53:03 -08:00
|
|
|
Fixable: Some violations can be fixed by tooling
|
2022-12-16 13:50:38 -08: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
|
2023-07-08 22:14:00 -07:00
|
|
|
# Heading Name
|
2022-10-29 23:21:45 -07:00
|
|
|
|
|
|
|
[Link](#fragment)
|
|
|
|
```
|
|
|
|
|
2023-07-08 22:14:00 -07:00
|
|
|
To fix this issue, change the link fragment to reference an existing heading's
|
|
|
|
generated name (see below):
|
2022-10-29 23:21:45 -07:00
|
|
|
|
|
|
|
```markdown
|
2023-07-08 22:14:00 -07:00
|
|
|
# Heading Name
|
2022-11-05 16:49:38 -07:00
|
|
|
|
2023-07-08 22:14:00 -07:00
|
|
|
[Link](#heading-name)
|
|
|
|
```
|
|
|
|
|
2024-10-08 22:40:11 -07:00
|
|
|
For consistency, this rule requires fragments to exactly match the [GitHub
|
|
|
|
heading algorithm][github-heading-algorithm] which converts letters to
|
|
|
|
lowercase. Therefore, the following example is reported as a violation:
|
2024-02-07 20:45:56 +01:00
|
|
|
|
|
|
|
```markdown
|
|
|
|
# Heading Name
|
|
|
|
|
|
|
|
[Link](#Heading-Name)
|
|
|
|
```
|
|
|
|
|
2024-10-08 22:40:11 -07:00
|
|
|
To ignore case when comparing fragments with heading names, the `ignore_case`
|
|
|
|
parameter can be set to `true`. In this configuration, the previous example is
|
|
|
|
not reported as a violation.
|
|
|
|
|
2023-07-08 22:14:00 -07:00
|
|
|
Alternatively, some platforms allow the syntax `{#named-anchor}` to be used
|
|
|
|
within a heading to provide a specific name (consisting of only lower-case
|
|
|
|
letters, numbers, `-`, and `_`):
|
|
|
|
|
|
|
|
```markdown
|
|
|
|
# Heading Name {#custom-name}
|
|
|
|
|
|
|
|
[Link](#custom-name)
|
2022-10-29 23:21:45 -07:00
|
|
|
```
|
|
|
|
|
2023-08-04 20:53:38 -07:00
|
|
|
Alternatively, any HTML tag with an `id` attribute or an `a` tag with 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.
|
|
|
|
|
2024-02-10 15:36:12 -08:00
|
|
|
This rule also recognizes the custom fragment syntax used by GitHub to highlight
|
|
|
|
[specific content in a document][github-linking-to-content].
|
2024-02-07 20:45:56 +01:00
|
|
|
|
2024-02-10 15:36:12 -08:00
|
|
|
For example, this link to line 20:
|
2024-02-07 20:45:56 +01:00
|
|
|
|
|
|
|
```markdown
|
2024-02-10 15:36:12 -08:00
|
|
|
[Link](#L20)
|
2024-02-07 20:45:56 +01:00
|
|
|
```
|
|
|
|
|
2024-02-10 15:36:12 -08:00
|
|
|
And this link to content starting within line 19 running into line 21:
|
2024-02-07 20:45:56 +01:00
|
|
|
|
|
|
|
```markdown
|
2024-02-10 15:36:12 -08:00
|
|
|
[Link](#L19C5-L21C11)
|
2024-02-07 20:45:56 +01:00
|
|
|
```
|
|
|
|
|
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-12-31 21:06:48 +00:00
|
|
|
[github-heading-algorithm]: https://github.com/gjtorikian/html-pipeline/blob/f13a1534cb650ba17af400d1acd3a22c28004c09/lib/html/pipeline/toc_filter.rb
|
2024-02-10 15:36:12 -08:00
|
|
|
[github-linking-to-content]: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-a-permanent-link-to-a-code-snippet#linking-to-markdown#linking-to-markdown
|