1- """ Backlinks module."""
1+ # Backlinks module.
22
33from __future__ import annotations
44
1414
1515 from markdown import Markdown
1616
17- from mkdocs_autorefs .plugin import AutorefsPlugin
17+ from mkdocs_autorefs ._internal . plugin import AutorefsPlugin
1818
1919try :
2020 from mkdocs .plugins import get_plugin_logger
2121
22- log = get_plugin_logger (__name__ )
22+ _log = get_plugin_logger (__name__ )
2323except ImportError :
2424 # TODO: remove once support for MkDocs <1.5 is dropped
25- log = logging .getLogger (f"mkdocs.plugins.{ __name__ } " ) # type: ignore[assignment]
25+ _log = logging .getLogger (f"mkdocs.plugins.{ __name__ } " ) # type: ignore[assignment]
2626
2727
2828@dataclass (eq = True , frozen = True , order = True )
2929class BacklinkCrumb :
3030 """A navigation breadcrumb for a backlink."""
3131
3232 title : str
33+ """The title of the page."""
3334 url : str
35+ """The URL of the page."""
3436
3537
3638@dataclass (eq = True , frozen = True , order = True )
3739class Backlink :
3840 """A backlink (list of breadcrumbs)."""
3941
4042 crumbs : tuple [BacklinkCrumb , ...]
43+ """The list of breadcrumbs."""
4144
4245
4346class BacklinksTreeProcessor (Treeprocessor ):
@@ -47,7 +50,10 @@ class BacklinksTreeProcessor(Treeprocessor):
4750 """
4851
4952 name : str = "mkdocs-autorefs-backlinks"
53+ """The name of the tree processor."""
5054 initial_id : str | None = None
55+ """The initial heading ID."""
56+
5157 _htags : ClassVar [set [str ]] = {"h1" , "h2" , "h3" , "h4" , "h5" , "h6" }
5258
5359 def __init__ (self , plugin : AutorefsPlugin , md : Markdown | None = None ) -> None :
@@ -57,25 +63,30 @@ def __init__(self, plugin: AutorefsPlugin, md: Markdown | None = None) -> None:
5763 plugin: A reference to the autorefs plugin, to use its `register_anchor` method.
5864 """
5965 super ().__init__ (md )
60- self .plugin = plugin
61- self .last_heading_id : str | None = None
66+ self ._plugin = plugin
67+ self ._last_heading_id : str | None = None
68+
69+ def run (self , root : Element ) -> None :
70+ """Run the tree processor.
6271
63- def run (self , root : Element ) -> None : # noqa: D102
64- if self .plugin .current_page is not None :
65- self .last_heading_id = self .initial_id
72+ Arguments:
73+ root: The root element of the document.
74+ """
75+ if self ._plugin .current_page is not None :
76+ self ._last_heading_id = self .initial_id
6677 self ._enhance_autorefs (root )
6778
6879 def _enhance_autorefs (self , parent : Element ) -> None :
6980 for el in parent :
7081 if el .tag == "a" : # Markdown anchor.
7182 if not (el .text or el .get ("href" ) or (el .tail and el .tail .strip ())) and (anchor_id := el .get ("id" )):
72- self .last_heading_id = anchor_id
83+ self ._last_heading_id = anchor_id
7384 elif el .tag in self ._htags : # Heading.
74- self .last_heading_id = el .get ("id" )
85+ self ._last_heading_id = el .get ("id" )
7586 elif el .tag == "autoref" :
7687 if "backlink-type" not in el .attrib :
7788 el .set ("backlink-type" , "referenced-by" )
78- if "backlink-anchor" not in el .attrib and self .last_heading_id :
79- el .set ("backlink-anchor" , self .last_heading_id )
89+ if "backlink-anchor" not in el .attrib and self ._last_heading_id :
90+ el .set ("backlink-anchor" , self ._last_heading_id )
8091 else :
8192 self ._enhance_autorefs (el )
0 commit comments