Skip to content

Commit 9b32feb

Browse files
committed
feat: Support specifying an object path
Issue-22: #22
1 parent 7a02f94 commit 9b32feb

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

src/mkdocstrings_handlers/typescript/handler.py

+39-7
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,51 @@ def collect(self, identifier: str, config: MutableMapping[str, Any]) -> Collecto
123123
Returns:
124124
Anything you want, as long as you can feed it to the `render` method.
125125
"""
126+
# If we are in fallback mode, we don't want to collect anything.
126127
if config.get("fallback", False):
127128
raise CollectionError("Not loading modules during fallback")
128-
if identifier not in self._collected:
129+
130+
# Split the identifier into package and path.
131+
try:
132+
package, path = identifier.split("::", 1)
133+
except ValueError:
134+
package, path = identifier, ""
135+
136+
# Load the data if not already collected.
137+
if package not in self._collected:
129138
data = load_typedoc(["typedoc"])
130139
self._collected[data.name] = data
131140
if data.kind is ReflectionKind.PROJECT:
132141
self._collected.update({module.name: module for module in data.children})
133142
logger.debug(f"Collected {', '.join(self._collected.keys())}")
134-
if identifier in self._collected:
135-
for child in self._collected[identifier].children:
143+
144+
# Find the object in the collected data.
145+
if package in self._collected:
146+
for child in self._collected[package].children:
136147
if child.kind is ReflectionKind.MODULE and child.name == "index":
137-
return child
138-
return self._collected[identifier]
148+
package_obj = child
149+
break
150+
else:
151+
package_obj = self._collected[package]
152+
153+
# If no path, return the package object.
154+
if not path:
155+
return package_obj
156+
157+
# Find the object in the package.
158+
parts = path.split(".")
159+
obj = package_obj
160+
while parts:
161+
part = parts.pop(0)
162+
for child in obj.children:
163+
if child.name == part:
164+
obj = child
165+
break
166+
else:
167+
raise CollectionError(f"Could not find {path} in {package}")
168+
return obj
169+
170+
# If we couldn't find the object, raise an error.
139171
raise CollectionError(f"Could not collect {identifier}")
140172

141173
def render(self, data: CollectorItem, config: Mapping[str, Any]) -> str:
@@ -151,10 +183,10 @@ def render(self, data: CollectorItem, config: Mapping[str, Any]) -> str:
151183
"""
152184
final_config = {**self.default_config, **config}
153185
heading_level = final_config["heading_level"]
154-
template = self.env.get_template("module.html.jinja")
186+
template = self.env.get_template("dispatch.html.jinja")
155187
return template.render(
156188
config=final_config,
157-
module=data,
189+
obj=data,
158190
heading_level=heading_level,
159191
root=True,
160192
)

0 commit comments

Comments
 (0)