Update MD051/link-fragments to support GitHub-style line/column range syntax in URLs (refs #1117).

This commit is contained in:
Théo LUDWIG 2024-02-07 20:45:56 +01:00 committed by David Anson
parent ece0ccf269
commit 242b35f98f
8 changed files with 329 additions and 26 deletions

View file

@ -24,6 +24,16 @@ generated name (see below):
[Link](#heading-name)
```
Furthermore, this rule mandates the use of lowercase for link fragments.
Thus, even though the link points to the correct fragment, the following example
will still trigger this rule:
```markdown
# Heading Name
[Link](#Heading-Name)
```
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 `_`):
@ -46,6 +56,28 @@ attribute can be used to define a fragment:
An `a` tag can be useful in scenarios where a heading is not appropriate or for
control over the text of the fragment identifier.
GitHub supports links to [specific lines][github-lines-links].
For example, to link to line 12 of current Markdown file:
```markdown
[Go to line 12](#L12)
```
GitHub also allows to specify a column number:
```markdown
[Go to line 12, column 34](#L12-L34)
```
Or a range of lines/columns:
```markdown
[Go to lines 12-34](#L12-L34)
```
So this rule will not report violations when this syntax is used.
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.
@ -59,3 +91,4 @@ append an incrementing integer as needed for uniqueness.
[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
[github-heading-algorithm]: https://github.com/gjtorikian/html-pipeline/blob/f13a1534cb650ba17af400d1acd3a22c28004c09/lib/html/pipeline/toc_filter.rb
[github-lines-links]: 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