You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+94-1Lines changed: 94 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,4 +49,97 @@ This works the same as [a normal link to that heading](../doc1.md#hello-world).
49
49
50
50
Linking to a heading without needing to know the destination page can be useful if specifying that path is cumbersome, e.g. when the pages have deeply nested paths, are far apart, or are moved around frequently. And the issue is somewhat exacerbated by the fact that [MkDocs supports only *relative* links between pages](https://github.com/mkdocs/mkdocs/issues/1592).
51
51
52
-
Note that this plugin's behavior is undefined when trying to link to a heading title that appears several times throughout the site. Currently it arbitrarily chooses one of the pages.
52
+
Note that this plugin's behavior is undefined when trying to link to a heading title that appears several times throughout the site. Currently it arbitrarily chooses one of the pages. In such cases, use [Markdown anchors](#markdown-anchors) to add unique aliases to your headings.
53
+
54
+
### Markdown anchors
55
+
56
+
The autorefs plugin offers a feature called "Markdown anchors". Such anchors can be added anywhere in a document, and linked to from any other place.
57
+
58
+
The syntax is:
59
+
60
+
```md
61
+
[](){#id-of-the-anchor}
62
+
```
63
+
64
+
If you look closely, it starts with the usual syntax for a link, `[]()`, except both the text value and URL of the link are empty. Then we see `{#id-of-the-anchor}`, which is the syntax supported by the [`attr_list`](https://python-markdown.github.io/extensions/attr_list/) extension. It sets an HTML id to the anchor element. The autorefs plugin simply gives a meaning to such anchors with ids. Note that raw HTML anchors like `<a id="foo"></a>` are not supported.
65
+
66
+
The `attr_list` extension must be enabled for the Markdown anchors feature to work:
67
+
68
+
```yaml
69
+
# mkdocs.yml
70
+
plugins:
71
+
- search
72
+
- autorefs
73
+
74
+
markdown_extensions:
75
+
- attr_list
76
+
```
77
+
78
+
Now, you can add anchors to documents:
79
+
80
+
```md
81
+
Somewhere in a document.
82
+
83
+
[](){#foobar-paragraph}
84
+
85
+
Paragraph about foobar.
86
+
```
87
+
88
+
...making it possible to link to this anchor with our automatic links:
89
+
90
+
```md
91
+
In any document.
92
+
93
+
Check out the [paragraph about foobar][foobar-pararaph].
94
+
```
95
+
96
+
If you add a Markdown anchor right above a heading, this anchor will redirect to the heading itself:
97
+
98
+
```md
99
+
[](){#foobar}
100
+
## A verbose title about foobar
101
+
```
102
+
103
+
Linking to the `foobar` anchor will bring you directly to the heading, not the anchor itself, so the URL will show `#a-verbose-title-about-foobar` instead of `#foobar`. These anchors therefore act as "aliases" for headings. It is possible to define multiple aliases per heading:
104
+
105
+
```md
106
+
[](){#contributing}
107
+
[](){#development-setup}
108
+
## How to contribute to the project?
109
+
```
110
+
111
+
Such aliases are especially useful when the same headings appear in several different pages. Without aliases, linking to the heading is undefined behavior (it could lead to any one of the headings). With unique aliases above headings, you can make sure to link to the right heading.
112
+
113
+
For example, consider the following setup. You have one document per operating system describing how to install a project with the OS package manager or from sources:
114
+
115
+
```tree
116
+
docs/
117
+
install/
118
+
arch.md
119
+
debian.md
120
+
gentoo.md
121
+
```
122
+
123
+
Each page has:
124
+
125
+
```md
126
+
## Install with package manager
127
+
...
128
+
129
+
## Install from sources
130
+
...
131
+
```
132
+
133
+
You don't want to change headings and make them redundant, like `## Arch: Install with package manager` and `## Debian: Install with package manager` just to be able to reference the right one with autorefs. Instead you can do this:
134
+
135
+
```md
136
+
[](){#arch-install-pkg}
137
+
## Install with package manager
138
+
...
139
+
140
+
[](){#arch-install-src}
141
+
## Install from sources
142
+
...
143
+
```
144
+
145
+
...changing `arch` by `debian`, `gentoo`, etc. in the other pages.
0 commit comments