plugin ¤
This module contains the "mkdocs-autorefs" plugin.
After each page is processed by the Markdown converter, this plugin stores absolute URLs of every HTML anchors it finds to later be able to fix unresolved references. It stores them during the on_page_content
event hook.
Just before writing the final HTML to the disc, during the on_post_page
event hook, this plugin searches for references of the form [identifier][]
or [title][identifier]
that were not resolved, and fixes them using the previously stored identifier-URL mapping.
Classes:
-
AutorefsConfig
–Configuration options for the
autorefs
plugin. -
AutorefsPlugin
–The
autorefs
plugin formkdocs
.
AutorefsConfig ¤
Bases: Config
Configuration options for the autorefs
plugin.
Attributes:
-
resolve_closest
–Whether to resolve an autoref to the closest URL when multiple URLs are found for an identifier.
resolve_closest class-attribute
instance-attribute
¤
resolve_closest = Type(bool, default=False)
Whether to resolve an autoref to the closest URL when multiple URLs are found for an identifier.
By closest, we mean a combination of "relative to the current page" and "shortest distance from the current page".
For example, if you link to identifier hello
from page foo/bar/
, and the identifier is found in foo/
, foo/baz/
and foo/bar/baz/qux/
pages, autorefs will resolve to foo/bar/baz/qux
, which is the only URL relative to foo/bar/
.
If multiple URLs are equally close, autorefs will resolve to the first of these equally close URLs. If autorefs cannot find any URL that is close to the current page, it will log a warning and resolve to the first URL found.
When false and multiple URLs are found for an identifier, autorefs will log a warning and resolve to the first URL.
AutorefsPlugin ¤
AutorefsPlugin()
Bases: BasePlugin[AutorefsConfig]
The autorefs
plugin for mkdocs
.
This plugin defines the following event hooks:
on_config
on_page_content
on_post_page
Check the Developing Plugins page of mkdocs
for more information about its plugin system.
Methods:
-
get_item_url
–Return a site-relative URL with anchor to the identifier, if it's present anywhere.
-
map_urls
–Recurse on every anchor to map its ID to its absolute URL.
-
on_config
–Instantiate our Markdown extension.
-
on_page_content
–Map anchors to URLs.
-
on_page_markdown
–Remember which page is the current one.
-
on_post_page
–Fix cross-references.
-
register_anchor
–Register that an anchor corresponding to an identifier was encountered when rendering the page.
-
register_url
–Register that the identifier should be turned into a link to this URL.
Source code in src/mkdocs_autorefs/plugin.py
94 95 96 97 98 99 |
|
get_item_url ¤
get_item_url(
identifier: str,
from_url: str | None = None,
fallback: Callable[[str], Sequence[str]] | None = None,
) -> str
Return a site-relative URL with anchor to the identifier, if it's present anywhere.
Parameters:
-
identifier
(str
) –The anchor (without '#').
-
from_url
(str | None
, default:None
) –The URL of the base page, from which we link towards the targeted pages.
-
fallback
(Callable[[str], Sequence[str]] | None
, default:None
) –An optional function to suggest alternative anchors to try on failure.
Returns:
-
str
–A site-relative URL.
Source code in src/mkdocs_autorefs/plugin.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
map_urls ¤
map_urls(base_url: str, anchor: AnchorLink) -> None
Recurse on every anchor to map its ID to its absolute URL.
This method populates self.url_map
by side-effect.
Parameters:
-
base_url
(str
) –The base URL to use as a prefix for each anchor's relative URL.
-
anchor
(AnchorLink
) –The anchor to process and to recurse on.
Source code in src/mkdocs_autorefs/plugin.py
265 266 267 268 269 270 271 272 273 274 275 276 |
|
on_config ¤
on_config(config: MkDocsConfig) -> MkDocsConfig | None
Instantiate our Markdown extension.
Hook for the on_config
event. In this hook, we instantiate our AutorefsExtension
and add it to the list of Markdown extensions used by mkdocs
.
Parameters:
-
config
(MkDocsConfig
) –The MkDocs config object.
Returns:
-
MkDocsConfig | None
–The modified config.
Source code in src/mkdocs_autorefs/plugin.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
on_page_content ¤
Map anchors to URLs.
Hook for the on_page_content
event. In this hook, we map the IDs of every anchor found in the table of contents to the anchors absolute URLs. This mapping will be used later to fix unresolved reference of the form [title][identifier]
or [identifier][]
.
Parameters:
-
html
(str
) –HTML converted from Markdown.
-
page
(Page
) –The related MkDocs page instance.
-
kwargs
(Any
, default:{}
) –Additional arguments passed by MkDocs.
Returns:
-
str
–The same HTML. We only use this hook to map anchors to URLs.
Source code in src/mkdocs_autorefs/plugin.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
on_page_markdown ¤
Remember which page is the current one.
Parameters:
-
markdown
(str
) –Input Markdown.
-
page
(Page
) –The related MkDocs page instance.
-
kwargs
(Any
, default:{}
) –Additional arguments passed by MkDocs.
Returns:
-
str
–The same Markdown. We only use this hook to keep a reference to the current page URL, used during Markdown conversion by the anchor scanner tree processor.
Source code in src/mkdocs_autorefs/plugin.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
on_post_page ¤
Fix cross-references.
Hook for the on_post_page
event. In this hook, we try to fix unresolved references of the form [title][identifier]
or [identifier][]
. Doing that allows the user of autorefs
to cross-reference objects in their documentation strings. It uses the native Markdown syntax so it's easy to remember and use.
We log a warning for each reference that we couldn't map to an URL, but try to be smart and ignore identifiers that do not look legitimate (sometimes documentation can contain strings matching our AUTO_REF_RE
regular expression that did not intend to reference anything). We currently ignore references when their identifier contains a space or a slash.
Parameters:
-
output
(str
) –HTML converted from Markdown.
-
page
(Page
) –The related MkDocs page instance.
-
kwargs
(Any
, default:{}
) –Additional arguments passed by MkDocs.
Returns:
-
str
–Modified HTML.
Source code in src/mkdocs_autorefs/plugin.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
|
register_anchor ¤
Register that an anchor corresponding to an identifier was encountered when rendering the page.
Parameters:
-
page
(str
) –The relative URL of the current page. Examples:
'foo/bar/'
,'foo/index.html'
-
identifier
(str
) –The HTML anchor (without '#') as a string.
Source code in src/mkdocs_autorefs/plugin.py
101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
register_url ¤
Register that the identifier should be turned into a link to this URL.
Parameters:
-
identifier
(str
) –The new identifier.
-
url
(str
) –The absolute URL (including anchor, if needed) where this item can be found.
Source code in src/mkdocs_autorefs/plugin.py
115 116 117 118 119 120 121 122 |
|