A MkDocs plugin that resolves absolute URLs relative to a configurable root url, with support for multi-version and multi-locale documentation sites based on the configurable url versioning scheme. Currently this only supports websites hosted through Read the Docs.
Mkdocs doesn't natively support absolute links (reference). In addition, when using documentation with multiple versions or locales (for example on ReadtheDocs), absolute URLs like /docs/guide.html become problematic. This plugin:
- Automatically prepends the absolute URLs in your Markdown files with the correct root URL
- Falls back to the
READTHEDOCS_CANONICAL_URLenvironment variable when noroot_urlis configured - Supports linking to specific locales or versions using a simple syntax, keeping the current locale/version (if present) when they are not inlcuded in the absolute link
The plugin resolves absolute URLs by prepending a root URL (either from your config or from the Read the Docs environment):
| Absolute URL | Root URL | Result |
|---|---|---|
/bar/foo.png |
https://mywebsite.com |
https://mywebsite.com/bar/foo.png |
/bar/foo.png |
https://mywebsite.com/subpage/ |
https://mywebsite.com/subpage/bar/foo.png |
If the root URL is not configured, it is taken from the READTHEDOCS_CANONICAL_URL environment variable.
For sites that enable multiple locales and/or multiple versions, you can add the url_versioning_scheme option and use an extended URL syntax to link to specific versions or locales:
URL Format: /!<locale>/@<version>/<link>
!<locale>: Optional locale override to switch to a specific locale. Must be used withurl_versioning_schemeset tomultiple_versions_with_translations@<version>: Optional version override to switch to a specific version. Must be used withurl_versioning_schemeset to eithermultiple_versions_with_translationsormultiple_versions_without_translations- Omit both to use the current locale/version (if present)
Given a root URL https://mywebsite.com, url versioning scheme multiple_versions_with_translations, current locale en, and current version latest:
| URL | Resolves to |
|---|---|
/my/page |
https://mywebsite.com/en/latest/my/page (current locale & version) |
/!fr/my/page |
https://mywebsite.com/fr/latest/my/page (locale override) |
/@v2.0/my/page |
https://mywebsite.com/en/v2.0/my/page (version override) |
/!fr/@v2.0/my/page |
https://mywebsite.com/fr/v2.0/my/page (locale and version override) |
| Option | Description | Default |
|---|---|---|
root_url |
Root URL to prepend resolved URLs to | READTHEDOCS_CANONICAL_URL environment variable |
attributes |
HTML attributes containing the URLs to process | ["href", "src", "data"] |
prefix |
URL prefix to identify absolute URLs for processing | / |
plugins:
- resolve-absolute-urlsplugins:
- resolve-absolute-urls:
root_url: https://mywebsite.io/
attributes:
- href
- data-url
prefix: /abs/
url_versioning_scheme: multiple_versions_without_translationsThis plugin is licensed under the CC-BY 4.0 license.
This project builds on work from OctoPrint/mkdocs-site-urls. We thank the original authors for their contributions and for publishing their work under an open license.