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 17, 2024
1 parent 3541aaa commit 3091b77
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/mkdocs_autorefs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import contextlib
import functools
import logging
from functools import partial
from typing import TYPE_CHECKING, Any, Callable, Sequence
from urllib.parse import urlsplit

Expand All @@ -25,7 +24,7 @@
from mkdocs.plugins import BasePlugin
from mkdocs.structure.pages import Page

from mkdocs_autorefs.references import AnchorScannerTreeProcessor, AutorefsExtension, fix_refs, relative_url
from mkdocs_autorefs.references import AutorefsExtension, fix_refs, relative_url

if TYPE_CHECKING:
from mkdocs.config.defaults import MkDocsConfig
Expand Down Expand Up @@ -147,8 +146,7 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None:
"""
log.debug("Adding AutorefsExtension to the list")
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))
config["markdown_extensions"].append(AutorefsExtension(plugin=self if scan_anchors else None))
return config

def on_page_markdown(self, markdown: str, page: Page, **kwargs: Any) -> str: # noqa: ARG002
Expand Down
8 changes: 4 additions & 4 deletions src/mkdocs_autorefs/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class AutorefsExtension(Extension):

def __init__(
self,
anchor_scanner_factory: Callable[[Markdown], AnchorScannerTreeProcessor] | None = None,
plugin: AutorefsPlugin | None = None,
**kwargs: Any,
) -> None:
"""Initialize the Markdown extension.
Expand All @@ -256,7 +256,7 @@ def __init__(
**kwargs: Keyword arguments passed to the [base constructor][markdown.extensions.Extension].
"""
super().__init__(**kwargs)
self.anchor_scanner_factory = anchor_scanner_factory
self.plugin = plugin

def extendMarkdown(self, md: Markdown) -> None: # noqa: N802 (casing: parent method's name)
"""Register the extension.
Expand All @@ -271,9 +271,9 @@ def extendMarkdown(self, md: Markdown) -> None: # noqa: N802 (casing: parent me
"mkdocs-autorefs",
priority=168, # Right after markdown.inlinepatterns.ReferenceInlineProcessor
)
if self.anchor_scanner_factory:
if self.plugin:
md.treeprocessors.register(
self.anchor_scanner_factory(md),
AnchorScannerTreeProcessor(self.plugin, md),
"mkdocs-autorefs-anchors-scanner",
priority=0,
)
5 changes: 2 additions & 3 deletions tests/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

from __future__ import annotations

from functools import partial
from textwrap import dedent

import markdown
import pytest

from mkdocs_autorefs.plugin import AutorefsPlugin
from mkdocs_autorefs.references import AnchorScannerTreeProcessor, AutorefsExtension, fix_refs, relative_url
from mkdocs_autorefs.references import AutorefsExtension, fix_refs, relative_url


@pytest.mark.parametrize(
Expand Down Expand Up @@ -233,7 +232,7 @@ def test_external_references() -> None:
def test_register_html_anchors() -> None:
"""Check that HTML anchors are registered when enabled."""
plugin = AutorefsPlugin()
md = markdown.Markdown(extensions=["attr_list", AutorefsExtension(partial(AnchorScannerTreeProcessor, plugin))])
md = markdown.Markdown(extensions=["attr_list", AutorefsExtension(plugin)])
plugin.current_page = ""
md.convert(
dedent(
Expand Down

0 comments on commit 3091b77

Please sign in to comment.