mkdocs_autorefs ¤
mkdocs-autorefs package.
Automatically link across pages in MkDocs.
Classes:
-
AnchorScannerTreeProcessor
–Tree processor to scan and register HTML anchors.
-
AutorefsConfig
–Configuration options for the
autorefs
plugin. -
AutorefsExtension
–Markdown extension that transforms unresolved references into auto-references.
-
AutorefsHookInterface
–An interface for hooking into how AutoRef handles inline references.
-
AutorefsInlineProcessor
–A Markdown extension to handle inline references.
-
AutorefsPlugin
–The
autorefs
plugin formkdocs
. -
Backlink
–A backlink (list of breadcrumbs).
-
BacklinkCrumb
–A navigation breadcrumb for a backlink.
-
BacklinksTreeProcessor
–Enhance autorefs with
backlink-type
andbacklink-anchor
attributes.
Functions:
-
fix_ref
–Return a
repl
function forre.sub
. -
fix_refs
–Fix all references in the given HTML text.
-
relative_url
–Compute the relative path from URL A to URL B.
Attributes:
-
AUTOREF_RE
–The autoref HTML tag regular expression.
-
AUTO_REF_RE
–Deprecated. Use
AUTOREF_RE
instead.
AUTOREF_RE module-attribute
¤
The autoref HTML tag regular expression.
A regular expression to match mkdocs-autorefs' special reference markers in the on_env
hook.
AUTO_REF_RE module-attribute
¤
AUTO_REF_RE = compile(
f"<span data-(?P<kind>autorefs-(?:identifier|optional|optional-hover))=(?P<identifier>{_ATTR_VALUE})(?: class=(?P<class>{_ATTR_VALUE}))?(?P<attrs> [^<>]+)?>(?P<title>.*?)</span>",
flags=DOTALL,
)
Deprecated. Use AUTOREF_RE
instead.
AnchorScannerTreeProcessor ¤
AnchorScannerTreeProcessor(
plugin: AutorefsPlugin, md: Markdown | None = None
)
Bases: Treeprocessor
Tree processor to scan and register HTML anchors.
Parameters:
-
plugin
(AutorefsPlugin
) –A reference to the autorefs plugin, to use its
register_anchor
method.
Methods:
-
run
–Run the tree processor.
Attributes:
Source code in src/mkdocs_autorefs/_internal/references.py
234 235 236 237 238 239 240 241 |
|
name class-attribute
instance-attribute
¤
name: str = 'mkdocs-autorefs-anchors-scanner'
The name of the tree processor.
run ¤
run(root: Element) -> None
Run the tree processor.
Parameters:
-
root
(Element
) –The root element of the tree.
Source code in src/mkdocs_autorefs/_internal/references.py
243 244 245 246 247 248 249 250 251 252 |
|
AutorefsConfig ¤
Bases: Config
Configuration options for the autorefs
plugin.
Attributes:
-
link_titles
(bool | Literal['auto', 'external']
) –Whether to set titles on links.
-
resolve_closest
(bool
) –Whether to resolve an autoref to the closest URL when multiple URLs are found for an identifier.
-
strip_title_tags
(bool | Literal['auto']
) –Whether to strip HTML tags from link titles.
link_titles class-attribute
instance-attribute
¤
link_titles: bool | Literal["auto", "external"] = Choice(
(True, False, "auto", "external"), default="auto"
)
Whether to set titles on links.
Such title attributes are displayed as tooltips when hovering over the links.
"auto"
: autorefs will detect whether the instant preview feature of Material for MkDocs is enabled, and set titles on external links when it is, all links if it is not."external"
: autorefs will set titles on external links only.True
: autorefs will set titles on all links.False
: autorefs will not set any title attributes on links.
Titles are only set when they are different from the link's text. Titles are constructed from the linked heading's original title, optionally appending the identifier for API objects.
resolve_closest class-attribute
instance-attribute
¤
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.
strip_title_tags class-attribute
instance-attribute
¤
Whether to strip HTML tags from link titles.
Some themes support HTML in link titles, but others do not.
"auto"
: strip tags unless the Material for MkDocs theme is detected.
AutorefsExtension ¤
AutorefsExtension(
plugin: AutorefsPlugin | None = None, **kwargs: Any
)
Bases: Extension
Markdown extension that transforms unresolved references into auto-references.
Auto-references are then resolved later by the MkDocs plugin.
This extension also scans Markdown anchors ([](){#some-id}
) to register them with the MkDocs plugin.
Parameters:
-
plugin
(AutorefsPlugin | None
, default:None
) –An optional reference to the autorefs plugin (to pass it to the anchor scanner tree processor).
-
**kwargs
(Any
, default:{}
) –Keyword arguments passed to the base constructor.
Methods:
-
extendMarkdown
–Register the extension.
Attributes:
-
plugin
–A reference to the autorefs plugin.
Source code in src/mkdocs_autorefs/_internal/references.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
|
extendMarkdown ¤
extendMarkdown(md: Markdown) -> None
Register the extension.
Add an instance of our AutorefsInlineProcessor
to the Markdown parser. Also optionally add an instance of our AnchorScannerTreeProcessor
and BacklinksTreeProcessor
to the Markdown parser if a reference to the autorefs plugin was passed to this extension.
Parameters:
-
md
(Markdown
) –A
markdown.Markdown
instance.
Source code in src/mkdocs_autorefs/_internal/references.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
|
AutorefsHookInterface ¤
Bases: ABC
An interface for hooking into how AutoRef handles inline references.
Classes:
-
Context
–The context around an auto-reference.
Methods:
-
expand_identifier
–Expand an identifier in a given context.
-
get_context
–Get the current context.
Context dataclass
¤
The context around an auto-reference.
Methods:
-
as_dict
–Convert the context to a dictionary of HTML attributes.
Attributes:
-
domain
(str
) –A domain like
py
orjs
. -
filepath
(str | Path
) –The path to the file containing the autoref.
-
lineno
(int
) –The line number in the file containing the autoref.
-
origin
(str
) –The origin of an autoref (an object identifier).
-
role
(str
) –A role like
class
orfunction
.
as_dict ¤
Convert the context to a dictionary of HTML attributes.
Source code in src/mkdocs_autorefs/_internal/references.py
72 73 74 75 76 77 78 79 80 |
|
expand_identifier abstractmethod
¤
Expand an identifier in a given context.
Parameters:
-
identifier
(str
) –The identifier to expand.
Returns:
-
str
–The expanded identifier.
Source code in src/mkdocs_autorefs/_internal/references.py
82 83 84 85 86 87 88 89 90 91 92 |
|
get_context abstractmethod
¤
get_context() -> Context
Get the current context.
Returns:
-
Context
–The current context.
Source code in src/mkdocs_autorefs/_internal/references.py
94 95 96 97 98 99 100 101 |
|
AutorefsInlineProcessor ¤
Bases: ReferenceInlineProcessor
A Markdown extension to handle inline references.
Methods:
-
handleMatch
–Handle an element that matched.
Attributes:
-
hook
(AutorefsHookInterface | None
) –The hook to use for expanding identifiers or adding context to autorefs.
-
name
(str
) –The name of the inline processor.
Source code in src/mkdocs_autorefs/_internal/references.py
113 114 |
|
hook class-attribute
instance-attribute
¤
hook: AutorefsHookInterface | None = None
The hook to use for expanding identifiers or adding context to autorefs.
name class-attribute
instance-attribute
¤
name: str = 'mkdocs-autorefs'
The name of the inline processor.
handleMatch ¤
Handle an element that matched.
Parameters:
Returns:
Source code in src/mkdocs_autorefs/_internal/references.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
AutorefsPlugin ¤
AutorefsPlugin()
Bases: BasePlugin[AutorefsConfig]
The autorefs
plugin for mkdocs
.
This plugin defines the following event hooks:
on_config
, to configure itselfon_page_markdown
, to set the current page in order for Markdown extension to use iton_env
, to apply cross-references once all pages have been rendered
Check the Developing Plugins page of mkdocs
for more information about its plugin system.
Methods:
-
get_backlinks
–Return the backlinks to an identifier relative to the given URL.
-
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_env
–Apply cross-references and collect backlinks.
-
on_page_content
–Map anchors to URLs.
-
on_page_markdown
–Remember which page is the current one.
-
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.
Attributes:
-
current_page
(Page | None
) –The current page being processed.
-
get_fallback_anchor
(Callable[[str], tuple[str, ...]] | None
) –Fallback anchors getter.
-
legacy_refs
(bool
) –Whether to support legacy references.
-
record_backlinks
(bool
) –Whether to record backlinks.
-
scan_toc
(bool
) –Whether to scan the table of contents for identifiers to map to URLs.
Source code in src/mkdocs_autorefs/_internal/plugin.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
current_page class-attribute
instance-attribute
¤
current_page: Page | None = None
The current page being processed.
get_fallback_anchor property
writable
¤
Fallback anchors getter.
legacy_refs class-attribute
instance-attribute
¤
legacy_refs: bool = True
Whether to support legacy references.
record_backlinks class-attribute
instance-attribute
¤
record_backlinks: bool = False
Whether to record backlinks.
scan_toc class-attribute
instance-attribute
¤
scan_toc: bool = True
Whether to scan the table of contents for identifiers to map to URLs.
get_backlinks ¤
Return the backlinks to an identifier relative to the given URL.
Parameters:
-
*identifiers
(str
, default:()
) –The identifiers to get backlinks for.
-
from_url
(str
) –The URL of the page where backlinks are rendered.
Returns:
-
dict[str, set[Backlink]]
–A dictionary of backlinks, with the type of reference as key and a set of backlinks as value.
-
dict[str, set[Backlink]]
–Each backlink is a tuple of (URL, title) tuples forming navigation breadcrumbs.
Source code in src/mkdocs_autorefs/_internal/plugin.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
|
get_item_url ¤
get_item_url(
identifier: str,
from_url: str | None = None,
fallback: Callable[[str], Sequence[str]] | None = None,
) -> tuple[str, str | None]
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.
Returns:
Source code in src/mkdocs_autorefs/_internal/plugin.py
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
|
map_urls ¤
map_urls(page: Page, anchor: AnchorLink) -> None
Recurse on every anchor to map its ID to its absolute URL.
This method populates self._primary_url_map
by side-effect.
Parameters:
-
page
(Page
) –The page containing the anchors.
-
anchor
(AnchorLink
) –The anchor to process and to recurse on.
Source code in src/mkdocs_autorefs/_internal/plugin.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
|
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/_internal/plugin.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
on_env ¤
on_env(
env: Environment,
/,
*,
config: MkDocsConfig,
files: Files,
) -> Environment
Apply cross-references and collect backlinks.
Hook for the on_env
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.
We also collect backlinks at the same time. We fix cross-refs and collect backlinks in a single pass for performance reasons (we don't want to run the regular expression on each page twice).
Parameters:
-
env
(Environment
) –The MkDocs environment.
-
config
(MkDocsConfig
) –The MkDocs config object.
-
files
(Files
) –The list of files in the MkDocs project.
Returns:
-
Environment
–The unmodified environment.
Source code in src/mkdocs_autorefs/_internal/plugin.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|
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/_internal/plugin.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
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/_internal/plugin.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
register_anchor ¤
register_anchor(
page: Page,
identifier: str,
anchor: str | None = None,
*,
title: str | None = None,
primary: bool = True
) -> None
Register that an anchor corresponding to an identifier was encountered when rendering the page.
Parameters:
-
page
(Page
) –The page where the anchor was found.
-
identifier
(str
) –The identifier to register.
-
anchor
(str | None
, default:None
) –The anchor on the page, without
#
. If not provided, defaults to the identifier. -
title
(str | None
, default:None
) –The title of the anchor (optional).
-
primary
(bool
, default:True
) –Whether this anchor is the primary one for the identifier.
Source code in src/mkdocs_autorefs/_internal/plugin.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
|
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/_internal/plugin.py
409 410 411 412 413 414 415 416 |
|
Backlink dataclass
¤
Backlink(crumbs: tuple[BacklinkCrumb, ...])
A backlink (list of breadcrumbs).
Attributes:
-
crumbs
(tuple[BacklinkCrumb, ...]
) –The list of breadcrumbs.
BacklinkCrumb dataclass
¤
BacklinksTreeProcessor ¤
BacklinksTreeProcessor(
plugin: AutorefsPlugin, md: Markdown | None = None
)
Bases: Treeprocessor
Enhance autorefs with backlink-type
and backlink-anchor
attributes.
These attributes are then used later to register backlinks.
Parameters:
-
plugin
(AutorefsPlugin
) –A reference to the autorefs plugin, to use its
register_anchor
method.
Methods:
-
run
–Run the tree processor.
Attributes:
-
initial_id
(str | None
) –The initial heading ID.
-
name
(str
) –The name of the tree processor.
Source code in src/mkdocs_autorefs/_internal/backlinks.py
59 60 61 62 63 64 65 66 67 |
|
initial_id class-attribute
instance-attribute
¤
initial_id: str | None = None
The initial heading ID.
name class-attribute
instance-attribute
¤
name: str = 'mkdocs-autorefs-backlinks'
The name of the tree processor.
run ¤
run(root: Element) -> None
Run the tree processor.
Parameters:
-
root
(Element
) –The root element of the document.
Source code in src/mkdocs_autorefs/_internal/backlinks.py
69 70 71 72 73 74 75 76 77 |
|
fix_ref ¤
fix_ref(
url_mapper: Callable[[str], tuple[str, str | None]],
unmapped: list[tuple[str, Context | None]],
record_backlink: (
Callable[[str, str, str], None] | None
) = None,
*,
link_titles: bool | Literal["external"] = True,
strip_title_tags: bool = False
) -> Callable
Return a repl
function for re.sub
.
In our context, we match Markdown references and replace them with HTML links.
When the matched reference's identifier was not mapped to an URL, we append the identifier to the outer unmapped
list. It generally means the user is trying to cross-reference an object that was not collected and rendered, making it impossible to link to it. We catch this exception in the caller to issue a warning.
Parameters:
-
url_mapper
(Callable[[str], tuple[str, str | None]]
) –A callable that gets an object's site URL by its identifier, such as mkdocs_autorefs.AutorefsPlugin.get_item_url.
-
unmapped
(list[tuple[str, Context | None]]
) –A list to store unmapped identifiers.
-
record_backlink
(Callable[[str, str, str], None] | None
, default:None
) –A callable to record backlinks.
-
link_titles
(bool | Literal['external']
, default:True
) –How to set HTML titles on links. Always (
True
), never (False
), or external-only ("external"
). -
strip_title_tags
(bool
, default:False
) –Whether to strip HTML tags from link titles.
Returns:
-
Callable
–The actual function accepting a
Match
object -
Callable
–and returning the replacement strings.
Source code in src/mkdocs_autorefs/_internal/references.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
|
fix_refs ¤
fix_refs(
html: str,
url_mapper: Callable[[str], tuple[str, str | None]],
*,
record_backlink: (
Callable[[str, str, str], None] | None
) = None,
link_titles: bool | Literal["external"] = True,
strip_title_tags: bool = False,
_legacy_refs: bool = True
) -> tuple[str, list[tuple[str, Context | None]]]
Fix all references in the given HTML text.
Parameters:
-
html
(str
) –The text to fix.
-
url_mapper
(Callable[[str], tuple[str, str | None]]
) –A callable that gets an object's site URL by its identifier, such as mkdocs_autorefs.AutorefsPlugin.get_item_url.
-
record_backlink
(Callable[[str, str, str], None] | None
, default:None
) –A callable to record backlinks.
-
link_titles
(bool | Literal['external']
, default:True
) –How to set HTML titles on links. Always (
True
), never (False
), or external-only ("external"
). -
strip_title_tags
(bool
, default:False
) –Whether to strip HTML tags from link titles.
Returns:
-
tuple[str, list[tuple[str, Context | None]]]
–The fixed HTML, and a list of unmapped identifiers (string and optional context).
Source code in src/mkdocs_autorefs/_internal/references.py
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 |
|
relative_url ¤
Compute the relative path from URL A to URL B.
Parameters:
Returns:
-
str
–The relative URL to go from A to B.
Source code in src/mkdocs_autorefs/_internal/references.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
|