mkdocs-autorefs


Namemkdocs-autorefs JSON
Version 1.2.0 PyPI version JSON
download
home_pageNone
SummaryAutomatically link across pages in MkDocs.
upload_time2024-09-01 18:29:18
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseISC
keywords mkdocs mkdocs-plugin docstrings autodoc
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # mkdocs-autorefs

[![ci](https://github.com/mkdocstrings/autorefs/workflows/ci/badge.svg)](https://github.com/mkdocstrings/autorefs/actions?query=workflow%3Aci)
[![documentation](https://img.shields.io/badge/docs-mkdocs-708FCC.svg?style=flat)](https://mkdocstrings.github.io/autorefs/)
[![pypi version](https://img.shields.io/pypi/v/mkdocs-autorefs.svg)](https://pypi.org/project/mkdocs-autorefs/)
[![conda version](https://img.shields.io/conda/vn/conda-forge/mkdocs-autorefs.svg)](https://anaconda.org/conda-forge/mkdocs-autorefs)
[![gitpod](https://img.shields.io/badge/gitpod-workspace-708FCC.svg?style=flat)](https://gitpod.io/#https://github.com/mkdocstrings/autorefs)
[![gitter](https://badges.gitter.im/join%20chat.svg)](https://app.gitter.im/#/room/#mkdocstrings/autorefs:gitter.im)

Automatically link across pages in MkDocs.

## Installation

With `pip`:

```bash
python3 -m pip install mkdocs-autorefs
```

## Usage

```yaml
# mkdocs.yml
plugins:
  - search
  - autorefs
```

In one of your Markdown files (e.g. `doc1.md`) create some headings:

```markdown
## Hello, world!

## Another heading

Link to [Hello, World!](#hello-world) on the same page.
```

This is a [*normal* link to an anchor](https://www.mkdocs.org/user-guide/writing-your-docs/#linking-to-pages). 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`:

```markdown
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.

### Non-unique headings

When linking to a heading that appears several times throughout the site, this plugin will log a warning message stating that multiple URLs were found and that headings should be made unique, and will resolve the link using the first found URL.

To prevent getting warnings, use [Markdown anchors](#markdown-anchors) to add unique aliases to your headings, and use these aliases when referencing the headings.

If you cannot use Markdown anchors, for example because you inject the same generated contents in multiple locations (for example mkdocstrings' API documentation), then you can try to alleviate the warnings by enabling the `resolve_closest` option:

```yaml
plugins:
- autorefs:
    resolve_closest: true
```

When `resolve_closest` is enabled, and multiple URLs are found for the same identifier, the plugin will try to resolve to the one that is "closest" to the current page (the page containing the link). By closest, we mean:

- URLs that are relative to the current page's URL, climbing up parents
- if multiple URLs are relative to it, use the one at the shortest distance if possible.

If multiple relative URLs are at the same distance, the first of these URLs will be used. If no URL is relative to the current page's URL, the first URL of all found URLs will be used.

Examples:

Current page | Candidate URLs | Relative URLs | Winner
------------ | -------------- | ------------- | ------
` ` | `x/#b`, `#b` | `#b` | `#b` (only one relative)
`a/` | `b/c/#d`, `c/#d` | none | `b/c/#d` (no relative, use first one, even if longer distance)
`a/b/` | `x/#e`, `a/c/#e`, `a/d/#e` | `a/c/#e`, `a/d/#e` (relative to parent `a/`) | `a/c/#e` (same distance, use first one)
`a/b/` | `x/#e`, `a/c/d/#e`, `a/c/#e` | `a/c/d/#e`, `a/c/#e` (relative to parent `a/`) | `a/c/#e` (shortest distance)
`a/b/c/` | `x/#e`, `a/#e`, `a/b/#e`, `a/b/c/d/#e`, `a/b/c/#e` | `a/b/c/d/#e`, `a/b/c/#e` | `a/b/c/#e` (shortest distance)

### 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:

```md
[](){#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`](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.

The `attr_list` extension must be enabled for the Markdown anchors feature to work:

```yaml
# mkdocs.yml
plugins:
  - search
  - autorefs

markdown_extensions:
  - attr_list
```

Now, you can add anchors to documents:

```md
Somewhere in a document.

[](){#foobar-paragraph}

Paragraph about foobar.
```

...making it possible to link to this anchor with our automatic links:

```md
In any document.

Check out the [paragraph about foobar][foobar-paragraph].
```

If you add a Markdown anchor right above a heading, this anchor will redirect to the heading itself:

```md
[](){#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:

```md
[](){#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:

```tree
docs/
  install/
    arch.md
    debian.md
    gentoo.md
```

Each page has:

```md
## 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:

```md
[](){#arch-install-pkg}
## Install with package manager
...

[](){#arch-install-src}
## Install from sources
...
```

...changing `arch` by `debian`, `gentoo`, etc. in the other pages.

---

You can also change the actual identifier of a heading, thanks again to the `attr_list` Markdown extension:

```md
## Install from sources { #arch-install-src }
...
```

...though note that this will impact the URL anchor too (and therefore the permalink to the heading).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "mkdocs-autorefs",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "mkdocs, mkdocs-plugin, docstrings, autodoc",
    "author": null,
    "author_email": "Oleh Prypin <oleh@pryp.in>, =?utf-8?q?Timoth=C3=A9e_Mazzucotelli?= <dev@pawamoy.fr>",
    "download_url": "https://files.pythonhosted.org/packages/fb/ae/0f1154c614d6a8b8a36fff084e5b82af3a15f7d2060cf0dcdb1c53297a71/mkdocs_autorefs-1.2.0.tar.gz",
    "platform": null,
    "description": "# mkdocs-autorefs\n\n[![ci](https://github.com/mkdocstrings/autorefs/workflows/ci/badge.svg)](https://github.com/mkdocstrings/autorefs/actions?query=workflow%3Aci)\n[![documentation](https://img.shields.io/badge/docs-mkdocs-708FCC.svg?style=flat)](https://mkdocstrings.github.io/autorefs/)\n[![pypi version](https://img.shields.io/pypi/v/mkdocs-autorefs.svg)](https://pypi.org/project/mkdocs-autorefs/)\n[![conda version](https://img.shields.io/conda/vn/conda-forge/mkdocs-autorefs.svg)](https://anaconda.org/conda-forge/mkdocs-autorefs)\n[![gitpod](https://img.shields.io/badge/gitpod-workspace-708FCC.svg?style=flat)](https://gitpod.io/#https://github.com/mkdocstrings/autorefs)\n[![gitter](https://badges.gitter.im/join%20chat.svg)](https://app.gitter.im/#/room/#mkdocstrings/autorefs:gitter.im)\n\nAutomatically link across pages in MkDocs.\n\n## Installation\n\nWith `pip`:\n\n```bash\npython3 -m pip install mkdocs-autorefs\n```\n\n## Usage\n\n```yaml\n# mkdocs.yml\nplugins:\n  - search\n  - autorefs\n```\n\nIn one of your Markdown files (e.g. `doc1.md`) create some headings:\n\n```markdown\n## Hello, world!\n\n## Another heading\n\nLink to [Hello, World!](#hello-world) on the same page.\n```\n\nThis is a [*normal* link to an anchor](https://www.mkdocs.org/user-guide/writing-your-docs/#linking-to-pages). 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.\n\nBut 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.  \nLet's create another Markdown page to try this, `subdir/doc2.md`:\n\n```markdown\nWe can [link to that heading][hello-world] from another page too.\n\nThis works the same as [a normal link to that heading](../doc1.md#hello-world).\n```\n\nLinking 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.\n\n### Non-unique headings\n\nWhen linking to a heading that appears several times throughout the site, this plugin will log a warning message stating that multiple URLs were found and that headings should be made unique, and will resolve the link using the first found URL.\n\nTo prevent getting warnings, use [Markdown anchors](#markdown-anchors) to add unique aliases to your headings, and use these aliases when referencing the headings.\n\nIf you cannot use Markdown anchors, for example because you inject the same generated contents in multiple locations (for example mkdocstrings' API documentation), then you can try to alleviate the warnings by enabling the `resolve_closest` option:\n\n```yaml\nplugins:\n- autorefs:\n    resolve_closest: true\n```\n\nWhen `resolve_closest` is enabled, and multiple URLs are found for the same identifier, the plugin will try to resolve to the one that is \"closest\" to the current page (the page containing the link). By closest, we mean:\n\n- URLs that are relative to the current page's URL, climbing up parents\n- if multiple URLs are relative to it, use the one at the shortest distance if possible.\n\nIf multiple relative URLs are at the same distance, the first of these URLs will be used. If no URL is relative to the current page's URL, the first URL of all found URLs will be used.\n\nExamples:\n\nCurrent page | Candidate URLs | Relative URLs | Winner\n------------ | -------------- | ------------- | ------\n` ` | `x/#b`, `#b` | `#b` | `#b` (only one relative)\n`a/` | `b/c/#d`, `c/#d` | none | `b/c/#d` (no relative, use first one, even if longer distance)\n`a/b/` | `x/#e`, `a/c/#e`, `a/d/#e` | `a/c/#e`, `a/d/#e` (relative to parent `a/`) | `a/c/#e` (same distance, use first one)\n`a/b/` | `x/#e`, `a/c/d/#e`, `a/c/#e` | `a/c/d/#e`, `a/c/#e` (relative to parent `a/`) | `a/c/#e` (shortest distance)\n`a/b/c/` | `x/#e`, `a/#e`, `a/b/#e`, `a/b/c/d/#e`, `a/b/c/#e` | `a/b/c/d/#e`, `a/b/c/#e` | `a/b/c/#e` (shortest distance)\n\n### Markdown anchors\n\nThe autorefs plugin offers a feature called \"Markdown anchors\". Such anchors can be added anywhere in a document, and linked to from any other place.\n\nThe syntax is:\n\n```md\n[](){#id-of-the-anchor}\n```\n\nIf 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.\n\nThe `attr_list` extension must be enabled for the Markdown anchors feature to work:\n\n```yaml\n# mkdocs.yml\nplugins:\n  - search\n  - autorefs\n\nmarkdown_extensions:\n  - attr_list\n```\n\nNow, you can add anchors to documents:\n\n```md\nSomewhere in a document.\n\n[](){#foobar-paragraph}\n\nParagraph about foobar.\n```\n\n...making it possible to link to this anchor with our automatic links:\n\n```md\nIn any document.\n\nCheck out the [paragraph about foobar][foobar-paragraph].\n```\n\nIf you add a Markdown anchor right above a heading, this anchor will redirect to the heading itself:\n\n```md\n[](){#foobar}\n## A verbose title about foobar\n```\n\nLinking 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:\n\n```md\n[](){#contributing}\n[](){#development-setup}\n## How to contribute to the project?\n```\n\nSuch 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.\n\nFor 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:\n\n```tree\ndocs/\n  install/\n    arch.md\n    debian.md\n    gentoo.md\n```\n\nEach page has:\n\n```md\n## Install with package manager\n...\n\n## Install from sources\n...\n```\n\nYou 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:\n\n```md\n[](){#arch-install-pkg}\n## Install with package manager\n...\n\n[](){#arch-install-src}\n## Install from sources\n...\n```\n\n...changing `arch` by `debian`, `gentoo`, etc. in the other pages.\n\n---\n\nYou can also change the actual identifier of a heading, thanks again to the `attr_list` Markdown extension:\n\n```md\n## Install from sources { #arch-install-src }\n...\n```\n\n...though note that this will impact the URL anchor too (and therefore the permalink to the heading).\n",
    "bugtrack_url": null,
    "license": "ISC",
    "summary": "Automatically link across pages in MkDocs.",
    "version": "1.2.0",
    "project_urls": {
        "Changelog": "https://mkdocstrings.github.io/autorefs/changelog",
        "Discussions": "https://github.com/mkdocstrings/autorefs/discussions",
        "Documentation": "https://mkdocstrings.github.io/autorefs",
        "Gitter": "https://gitter.im/mkdocstrings/autorefs",
        "Homepage": "https://mkdocstrings.github.io/autorefs",
        "Issues": "https://github.com/mkdocstrings/autorefs/issues",
        "Repository": "https://github.com/mkdocstrings/autorefs"
    },
    "split_keywords": [
        "mkdocs",
        " mkdocs-plugin",
        " docstrings",
        " autodoc"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "71264d39d52ea2219604053a4d05b98e90d6a335511cc01806436ec4886b1028",
                "md5": "21d0a64ea321d34f40dd9fbf96a5f532",
                "sha256": "d588754ae89bd0ced0c70c06f58566a4ee43471eeeee5202427da7de9ef85a2f"
            },
            "downloads": -1,
            "filename": "mkdocs_autorefs-1.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "21d0a64ea321d34f40dd9fbf96a5f532",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 16522,
            "upload_time": "2024-09-01T18:29:16",
            "upload_time_iso_8601": "2024-09-01T18:29:16.605544Z",
            "url": "https://files.pythonhosted.org/packages/71/26/4d39d52ea2219604053a4d05b98e90d6a335511cc01806436ec4886b1028/mkdocs_autorefs-1.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fbae0f1154c614d6a8b8a36fff084e5b82af3a15f7d2060cf0dcdb1c53297a71",
                "md5": "da831909e4232d01201f059328248526",
                "sha256": "a86b93abff653521bda71cf3fc5596342b7a23982093915cb74273f67522190f"
            },
            "downloads": -1,
            "filename": "mkdocs_autorefs-1.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "da831909e4232d01201f059328248526",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 40262,
            "upload_time": "2024-09-01T18:29:18",
            "upload_time_iso_8601": "2024-09-01T18:29:18.514525Z",
            "url": "https://files.pythonhosted.org/packages/fb/ae/0f1154c614d6a8b8a36fff084e5b82af3a15f7d2060cf0dcdb1c53297a71/mkdocs_autorefs-1.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-01 18:29:18",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "mkdocstrings",
    "github_project": "autorefs",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "mkdocs-autorefs"
}
        
Elapsed time: 0.30629s