From ef77542146a29d6f281dbe4305328fead9262494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Fri, 16 Feb 2024 16:19:00 +0100 Subject: [PATCH] fixup! feat: Add option to scan and register HTML anchors --- src/mkdocs_autorefs/plugin.py | 24 +++++++++++++++--------- src/mkdocs_autorefs/references.py | 4 +--- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/mkdocs_autorefs/plugin.py b/src/mkdocs_autorefs/plugin.py index 9f1a31e..0c332bb 100644 --- a/src/mkdocs_autorefs/plugin.py +++ b/src/mkdocs_autorefs/plugin.py @@ -73,9 +73,7 @@ def __init__(self) -> None: super().__init__() self._url_map: dict[str, str] = {} self._abs_url_map: dict[str, str] = {} - self._extension: AutorefsExtension | None = None self.get_fallback_anchor: Callable[[str], str | None] | None = None - self.current_page: str | None = None def register_anchor(self, page: str, identifier: str, anchor: str | None = None) -> None: """Register that an anchor corresponding to an identifier was encountered when rendering the page. @@ -151,15 +149,23 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None: The modified config. """ log.debug("Adding AutorefsExtension to the list") - anchor_scanner_factory = ( - partial(AnchorScannerTreeProcessor, self) if self.scan_anchors or self.config.scan_anchors else None - ) - # anchor_scanner_factory = None - self._extension = AutorefsExtension(anchor_scanner_factory=anchor_scanner_factory) - config["markdown_extensions"].append(self._extension) + scan_anchors = self.scan_anchors or self.config.scan_anchors + anchor_scanner_factory = partial(AnchorScannerTreeProcessor, self) if scan_anchors else None + config["markdown_extensions"].append(AutorefsExtension(anchor_scanner_factory)) return config - def on_page_markdown(self, markdown: str, *, page: Page, **kwargs: Any) -> str | None: # noqa: ARG002, D102 + def on_page_markdown(self, markdown: str, page: Page, **kwargs: Any) -> str: # noqa: ARG002 + """Remember which page is the current one. + + Arguments: + markdown: Input Markdown. + page: The related MkDocs page instance. + kwargs: Additional arguments passed by MkDocs. + + Returns: + 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. + """ self.current_page = page.url return markdown diff --git a/src/mkdocs_autorefs/references.py b/src/mkdocs_autorefs/references.py index 5ce86a7..d5a2538 100644 --- a/src/mkdocs_autorefs/references.py +++ b/src/mkdocs_autorefs/references.py @@ -241,7 +241,6 @@ def __init__( """ super().__init__(**kwargs) self.anchor_scanner_factory = anchor_scanner_factory - self.anchor_scanner: AnchorScannerTreeProcessor | None = None def extendMarkdown(self, md: Markdown) -> None: # noqa: N802 (casing: parent method's name) """Register the extension. @@ -257,9 +256,8 @@ def extendMarkdown(self, md: Markdown) -> None: # noqa: N802 (casing: parent me priority=168, # Right after markdown.inlinepatterns.ReferenceInlineProcessor ) if self.anchor_scanner_factory: - self.anchor_scanner = self.anchor_scanner_factory(md) md.treeprocessors.register( - self.anchor_scanner, + self.anchor_scanner_factory(md), "mkdocs-autorefs-anchors-scanner", priority=0, )