1
- """ Backlinks module."""
1
+ # Backlinks module.
2
2
3
3
from __future__ import annotations
4
4
14
14
15
15
from markdown import Markdown
16
16
17
- from mkdocs_autorefs .plugin import AutorefsPlugin
17
+ from mkdocs_autorefs ._internal . plugin import AutorefsPlugin
18
18
19
19
try :
20
20
from mkdocs .plugins import get_plugin_logger
21
21
22
- log = get_plugin_logger (__name__ )
22
+ _log = get_plugin_logger (__name__ )
23
23
except ImportError :
24
24
# 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]
26
26
27
27
28
28
@dataclass (eq = True , frozen = True , order = True )
29
29
class BacklinkCrumb :
30
30
"""A navigation breadcrumb for a backlink."""
31
31
32
32
title : str
33
+ """The title of the page."""
33
34
url : str
35
+ """The URL of the page."""
34
36
35
37
36
38
@dataclass (eq = True , frozen = True , order = True )
37
39
class Backlink :
38
40
"""A backlink (list of breadcrumbs)."""
39
41
40
42
crumbs : tuple [BacklinkCrumb , ...]
43
+ """The list of breadcrumbs."""
41
44
42
45
43
46
class BacklinksTreeProcessor (Treeprocessor ):
@@ -47,7 +50,10 @@ class BacklinksTreeProcessor(Treeprocessor):
47
50
"""
48
51
49
52
name : str = "mkdocs-autorefs-backlinks"
53
+ """The name of the tree processor."""
50
54
initial_id : str | None = None
55
+ """The initial heading ID."""
56
+
51
57
_htags : ClassVar [set [str ]] = {"h1" , "h2" , "h3" , "h4" , "h5" , "h6" }
52
58
53
59
def __init__ (self , plugin : AutorefsPlugin , md : Markdown | None = None ) -> None :
@@ -57,25 +63,30 @@ def __init__(self, plugin: AutorefsPlugin, md: Markdown | None = None) -> None:
57
63
plugin: A reference to the autorefs plugin, to use its `register_anchor` method.
58
64
"""
59
65
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.
62
71
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
66
77
self ._enhance_autorefs (root )
67
78
68
79
def _enhance_autorefs (self , parent : Element ) -> None :
69
80
for el in parent :
70
81
if el .tag == "a" : # Markdown anchor.
71
82
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
73
84
elif el .tag in self ._htags : # Heading.
74
- self .last_heading_id = el .get ("id" )
85
+ self ._last_heading_id = el .get ("id" )
75
86
elif el .tag == "autoref" :
76
87
if "backlink-type" not in el .attrib :
77
88
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 )
80
91
else :
81
92
self ._enhance_autorefs (el )
0 commit comments