|  | 
| 2 | 2 | 
 | 
| 3 | 3 | After each page is processed by the Markdown converter, this plugin stores absolute URLs of every HTML anchors | 
| 4 | 4 | it finds to later be able to fix unresolved references. | 
| 5 |  | -It stores them during the [`on_page_content` event hook](https://www.mkdocs.org/user-guide/plugins/#on_page_content). | 
| 6 | 5 | 
 | 
| 7 |  | -Just before writing the final HTML to the disc, during the | 
| 8 |  | -[`on_post_page` event hook](https://www.mkdocs.org/user-guide/plugins/#on_post_page), | 
| 9 |  | -this plugin searches for references of the form `[identifier][]` or `[title][identifier]` that were not resolved, | 
| 10 |  | -and fixes them using the previously stored identifier-URL mapping. | 
|  | 6 | +Once every page has been rendered and all identifiers and their URLs collected, | 
|  | 7 | +the plugin fixes unresolved references in the HTML content of the pages. | 
| 11 | 8 | """ | 
| 12 | 9 | 
 | 
| 13 | 10 | from __future__ import annotations | 
|  | 
| 22 | 19 | 
 | 
| 23 | 20 | from mkdocs.config.base import Config | 
| 24 | 21 | from mkdocs.config.config_options import Type | 
| 25 |  | -from mkdocs.plugins import BasePlugin | 
|  | 22 | +from mkdocs.plugins import BasePlugin, event_priority | 
| 26 | 23 | from mkdocs.structure.pages import Page | 
| 27 | 24 | 
 | 
| 28 | 25 | from mkdocs_autorefs.references import AutorefsExtension, fix_refs, relative_url | 
| 29 | 26 | 
 | 
| 30 | 27 | if TYPE_CHECKING: | 
| 31 | 28 |     from collections.abc import Sequence | 
| 32 | 29 | 
 | 
|  | 30 | +    from jinja2.environment import Environment | 
| 33 | 31 |     from mkdocs.config.defaults import MkDocsConfig | 
|  | 32 | +    from mkdocs.structure.files import Files | 
| 34 | 33 |     from mkdocs.structure.pages import Page | 
| 35 | 34 |     from mkdocs.structure.toc import AnchorLink | 
| 36 | 35 | 
 | 
| @@ -67,9 +66,9 @@ class AutorefsPlugin(BasePlugin[AutorefsConfig]): | 
| 67 | 66 | 
 | 
| 68 | 67 |     This plugin defines the following event hooks: | 
| 69 | 68 | 
 | 
| 70 |  | -    - `on_config` | 
| 71 |  | -    - `on_page_content` | 
| 72 |  | -    - `on_post_page` | 
|  | 69 | +    - `on_config`, to configure itself | 
|  | 70 | +    - `on_page_markdown`, to set the current page in order for Markdown extension to use it | 
|  | 71 | +    - `on_env`, to apply cross-references once all pages have been rendered | 
| 73 | 72 | 
 | 
| 74 | 73 |     Check the [Developing Plugins](https://www.mkdocs.org/user-guide/plugins/#developing-plugins) page of `mkdocs` | 
| 75 | 74 |     for more information about its plugin system. | 
| @@ -337,37 +336,47 @@ def map_urls(self, base_url: str, anchor: AnchorLink) -> None: | 
| 337 | 336 |         for child in anchor.children: | 
| 338 | 337 |             self.map_urls(base_url, child) | 
| 339 | 338 | 
 | 
| 340 |  | -    def on_post_page(self, output: str, page: Page, **kwargs: Any) -> str:  # noqa: ARG002 | 
| 341 |  | -        """Fix cross-references. | 
|  | 339 | +    @event_priority(-50)  # Late, after mkdocstrings has finished loading inventories. | 
|  | 340 | +    def on_env(self, env: Environment, /, *, config: MkDocsConfig, files: Files) -> Environment:  # noqa: ARG002 | 
|  | 341 | +        """Apply cross-references. | 
| 342 | 342 | 
 | 
| 343 |  | -        Hook for the [`on_post_page` event](https://www.mkdocs.org/user-guide/plugins/#on_post_page). | 
|  | 343 | +        Hook for the [`on_env` event](https://www.mkdocs.org/user-guide/plugins/#on_env). | 
| 344 | 344 |         In this hook, we try to fix unresolved references of the form `[title][identifier]` or `[identifier][]`. | 
| 345 | 345 |         Doing that allows the user of `autorefs` to cross-reference objects in their documentation strings. | 
| 346 | 346 |         It uses the native Markdown syntax so it's easy to remember and use. | 
| 347 | 347 | 
 | 
| 348 |  | -        We log a warning for each reference that we couldn't map to an URL, but try to be smart and ignore identifiers | 
| 349 |  | -        that do not look legitimate (sometimes documentation can contain strings matching | 
| 350 |  | -        our [`AUTO_REF_RE`][mkdocs_autorefs.references.AUTO_REF_RE] regular expression that did not intend to reference anything). | 
| 351 |  | -        We currently ignore references when their identifier contains a space or a slash. | 
|  | 348 | +        We log a warning for each reference that we couldn't map to an URL. | 
| 352 | 349 | 
 | 
| 353 | 350 |         Arguments: | 
| 354 |  | -            output: HTML converted from Markdown. | 
| 355 |  | -            page: The related MkDocs page instance. | 
| 356 |  | -            kwargs: Additional arguments passed by MkDocs. | 
|  | 351 | +            env: The MkDocs environment. | 
|  | 352 | +            config: The MkDocs config object. | 
|  | 353 | +            files: The list of files in the MkDocs project. | 
| 357 | 354 | 
 | 
| 358 | 355 |         Returns: | 
| 359 |  | -            Modified HTML. | 
|  | 356 | +            The unmodified environment. | 
| 360 | 357 |         """ | 
| 361 |  | -        log.debug("Fixing references in page %s", page.file.src_path) | 
| 362 |  | - | 
| 363 |  | -        # YORE: Bump 2: Replace `, fallback=self.get_fallback_anchor` with `` within line. | 
| 364 |  | -        url_mapper = functools.partial(self.get_item_url, from_url=page.url, fallback=self.get_fallback_anchor) | 
| 365 |  | -        # YORE: Bump 2: Replace `, _legacy_refs=self.legacy_refs` with `` within line. | 
| 366 |  | -        fixed_output, unmapped = fix_refs(output, url_mapper, _legacy_refs=self.legacy_refs) | 
| 367 |  | - | 
| 368 |  | -        if unmapped and log.isEnabledFor(logging.WARNING): | 
| 369 |  | -            for ref, context in unmapped: | 
| 370 |  | -                message = f"from {context.filepath}:{context.lineno}: ({context.origin}) " if context else "" | 
| 371 |  | -                log.warning(f"{page.file.src_path}: {message}Could not find cross-reference target '{ref}'") | 
| 372 |  | - | 
| 373 |  | -        return fixed_output | 
|  | 358 | +        for file in files: | 
|  | 359 | +            if file.page and file.page.content: | 
|  | 360 | +                log.debug("Applying cross-refs in page %s", file.page.file.src_path) | 
|  | 361 | + | 
|  | 362 | +                # YORE: Bump 2: Replace `, fallback=self.get_fallback_anchor` with `` within line. | 
|  | 363 | +                url_mapper = functools.partial( | 
|  | 364 | +                    self.get_item_url, | 
|  | 365 | +                    from_url=file.page.url, | 
|  | 366 | +                    fallback=self.get_fallback_anchor, | 
|  | 367 | +                ) | 
|  | 368 | +                # YORE: Bump 2: Replace `, _legacy_refs=self.legacy_refs` with `` within line. | 
|  | 369 | +                file.page.content, unmapped = fix_refs( | 
|  | 370 | +                    file.page.content, | 
|  | 371 | +                    url_mapper, | 
|  | 372 | +                    _legacy_refs=self.legacy_refs, | 
|  | 373 | +                ) | 
|  | 374 | + | 
|  | 375 | +                if unmapped and log.isEnabledFor(logging.WARNING): | 
|  | 376 | +                    for ref, context in unmapped: | 
|  | 377 | +                        message = f"from {context.filepath}:{context.lineno}: ({context.origin}) " if context else "" | 
|  | 378 | +                        log.warning( | 
|  | 379 | +                            f"{file.page.file.src_path}: {message}Could not find cross-reference target '{ref}'", | 
|  | 380 | +                        ) | 
|  | 381 | + | 
|  | 382 | +        return env | 
0 commit comments