Automatically link across pages in MkDocs.
Project description
mkdocs-autorefs
Automatically link across pages in MkDocs.
Installation
With pip
:
python3 -m pip install mkdocs-autorefs
Usage
# mkdocs.yml
plugins:
- search
- autorefs
In one of your Markdown files (e.g. doc1.md
) create some headings:
## Hello, world!
## Another heading
Link to [Hello, World!](#hello-world) on the same page.
This is a normal link to an anchor. MkDocs generates anchors for each heading, and they can always be used to link to something, either within the same page (as shown here) or by specifying the path of the other page.
But with this plugin, you can link to a heading from any other page on the site without needing to know the path of either of the pages, just the heading title itself.
Let's create another Markdown page to try this, subdir/doc2.md
:
We can [link to that heading][hello-world] from another page too.
This works the same as [a normal link to that heading](../doc1.md#hello-world).
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.
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 to add unique aliases to your headings.
Markdown anchors
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.
The syntax is:
[](){#id-of-the-anchor}
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
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.
The attr_list
extension must be enabled for the Markdown anchors feature to work:
# mkdocs.yml
plugins:
- search
- autorefs
markdown_extensions:
- attr_list
Now, you can add anchors to documents:
Somewhere in a document.
[](){#foobar-paragraph}
Paragraph about foobar.
...making it possible to link to this anchor with our automatic links:
In any document.
Check out the [paragraph about foobar][foobar-pararaph].
If you add a Markdown anchor right above a heading, this anchor will redirect to the heading itself:
[](){#foobar}
## A verbose title about foobar
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:
[](){#contributing}
[](){#development-setup}
## How to contribute to the project?
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.
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:
docs/
install/
arch.md
debian.md
gentoo.md
Each page has:
## Install with package manager
...
## Install from sources
...
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:
[](){#arch-install-pkg}
## Install with package manager
...
[](){#arch-install-src}
## Install from sources
...
...changing arch
by debian
, gentoo
, etc. in the other pages.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mkdocs_autorefs-1.0.1.tar.gz
.
File metadata
- Download URL: mkdocs_autorefs-1.0.1.tar.gz
- Upload date:
- Size: 17.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f684edf847eced40b570b57846b15f0bf57fb93ac2c510450775dcf16accb971 |
|
MD5 | 6a91f692e94f040a8a27b94eef17184e |
|
BLAKE2b-256 | ce750ced93354fd9df40531c548f07d6462912eea9519e8cd78a8e6b42d73c4a |
File details
Details for the file mkdocs_autorefs-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: mkdocs_autorefs-1.0.1-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aacdfae1ab197780fb7a2dac92ad8a3d8f7ca8049a9cbe56a4218cd52e8da570 |
|
MD5 | 1665bfa337c1803de27f61d557e1f8cd |
|
BLAKE2b-256 | f601d413c98335ed75d8c211afb91320811366d55fb0ef7f4b01b9ab19630eac |