Skip to content

Commit

Permalink
fixup! feat: Add option to scan and register HTML anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Feb 16, 2024
1 parent 631783e commit ef77542
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
24 changes: 15 additions & 9 deletions src/mkdocs_autorefs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions src/mkdocs_autorefs/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
)

0 comments on commit ef77542

Please sign in to comment.