Skip to content

Commit 228fb73

Browse files
committed
feat: Register all anchors for each object in the inventory
1 parent 0b06d6a commit 228fb73

2 files changed

Lines changed: 36 additions & 22 deletions

File tree

src/mkdocstrings/extension.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,28 @@ def run(self, parent: Element, blocks: MutableSequence[str]) -> None:
137137
self._autorefs.register_anchor(page, rendered_anchor)
138138

139139
if "data-role" in heading.attrib:
140-
for anchor in sorted({rendered_anchor, *handler.get_anchors(data)}):
141-
self._handlers.inventory.register(
142-
name=anchor,
143-
domain=handler.domain,
144-
role=heading.attrib["data-role"],
145-
uri=f"{page}#{rendered_anchor}",
146-
)
140+
self._handlers.inventory.register(
141+
name=rendered_anchor,
142+
domain=handler.domain,
143+
role=heading.attrib["data-role"],
144+
priority=1, # register with standard priority
145+
uri=f"{page}#{rendered_anchor}",
146+
)
147+
148+
# also register other anchors for this object in the inventory
149+
try:
150+
data_object = handler.collect(rendered_anchor, handler.fallback_config)
151+
except CollectionError:
152+
continue
153+
for anchor in handler.get_anchors(data_object):
154+
if anchor not in self._handlers.inventory:
155+
self._handlers.inventory.register(
156+
name=anchor,
157+
domain=handler.domain,
158+
role=heading.attrib["data-role"],
159+
priority=2, # register with lower priority
160+
uri=f"{page}#{rendered_anchor}",
161+
)
147162

148163
parent.append(el)
149164

src/mkdocstrings/inventory.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(
2020
domain: str,
2121
role: str,
2222
uri: str,
23-
priority: str = "1",
23+
priority: int = 1,
2424
dispname: str | None = None,
2525
):
2626
"""Initialize the object.
@@ -30,14 +30,14 @@ def __init__(
3030
domain: The item domain, like 'python' or 'crystal'.
3131
role: The item role, like 'class' or 'method'.
3232
uri: The item URI.
33-
priority: The item priority. It can help for inventory suggestions.
33+
priority: The item priority. Only used internally by mkdocstrings and Sphinx.
3434
dispname: The item display name.
3535
"""
3636
self.name: str = name
3737
self.domain: str = domain
3838
self.role: str = role
3939
self.uri: str = uri
40-
self.priority: str = priority
40+
self.priority: int = priority
4141
self.dispname: str = dispname or name
4242

4343
def format_sphinx(self) -> str:
@@ -67,7 +67,7 @@ def parse_sphinx(cls, line: str) -> InventoryItem:
6767
uri = uri[:-1] + name
6868
if dispname == "-":
6969
dispname = name
70-
return cls(name, domain, role, uri, priority, dispname)
70+
return cls(name, domain, role, uri, int(priority), dispname)
7171

7272

7373
class Inventory(dict):
@@ -94,7 +94,7 @@ def register(
9494
domain: str,
9595
role: str,
9696
uri: str,
97-
priority: str = "1",
97+
priority: int = 1,
9898
dispname: str | None = None,
9999
) -> None:
100100
"""Create and register an item.
@@ -104,18 +104,17 @@ def register(
104104
domain: The item domain, like 'python' or 'crystal'.
105105
role: The item role, like 'class' or 'method'.
106106
uri: The item URI.
107-
priority: The item priority. It can help for inventory suggestions.
107+
priority: The item priority. Only used internally by mkdocstrings and Sphinx.
108108
dispname: The item display name.
109109
"""
110-
if name not in self:
111-
self[name] = InventoryItem(
112-
name=name,
113-
domain=domain,
114-
role=role,
115-
uri=uri,
116-
priority=priority,
117-
dispname=dispname,
118-
)
110+
self[name] = InventoryItem(
111+
name=name,
112+
domain=domain,
113+
role=role,
114+
uri=uri,
115+
priority=priority,
116+
dispname=dispname,
117+
)
119118

120119
def format_sphinx(self) -> bytes:
121120
"""Format this inventory as a Sphinx `objects.inv` file.

0 commit comments

Comments
 (0)