44
55from typing import TYPE_CHECKING , Any , ClassVar , Mapping , MutableMapping
66
7- from mkdocs . exceptions import PluginError
7+ from markdown import Markdown
88from mkdocstrings .handlers .base import BaseHandler , CollectionError , CollectorItem
99from mkdocstrings .loggers import get_logger
1010
11- if TYPE_CHECKING :
12- from markdown import Markdown
1311
12+ from griffe_typedoc .loader import load as load_typedoc
13+ from griffe_typedoc .logger import patch_loggers
14+ from griffe_typedoc .dataclasses import ReflectionKind
1415
16+ patch_loggers (get_logger )
1517logger = get_logger (__name__ )
1618
1719
@@ -31,9 +33,53 @@ class TypescriptHandler(BaseHandler):
3133 """The configuration used to collect item during autorefs fallback."""
3234
3335 default_config : ClassVar [dict ] = {
36+ "base_file_path" : "." ,
37+ "docstring_style" : "google" ,
38+ "docstring_options" : {},
39+ "show_symbol_type_heading" : False ,
40+ "show_symbol_type_toc" : False ,
3441 "show_root_heading" : False ,
3542 "show_root_toc_entry" : True ,
43+ "show_root_full_path" : True ,
44+ "show_root_members_full_path" : False ,
45+ "show_object_full_path" : False ,
46+ "show_category_heading" : False ,
47+ "show_if_no_docstring" : False ,
48+ "show_signature" : True ,
49+ "show_signature_annotations" : False ,
50+ "signature_crossrefs" : False ,
51+ "separate_signature" : False ,
52+ "line_length" : 60 ,
53+ "merge_init_into_class" : False ,
54+ "show_docstring_attributes" : True ,
55+ "show_docstring_functions" : True ,
56+ "show_docstring_classes" : True ,
57+ "show_docstring_modules" : True ,
58+ "show_docstring_description" : True ,
59+ "show_docstring_examples" : True ,
60+ "show_docstring_other_parameters" : True ,
61+ "show_docstring_parameters" : True ,
62+ "show_docstring_raises" : True ,
63+ "show_docstring_receives" : True ,
64+ "show_docstring_returns" : True ,
65+ "show_docstring_warns" : True ,
66+ "show_docstring_yields" : True ,
67+ "show_source" : True ,
68+ "show_bases" : True ,
69+ "show_submodules" : False ,
70+ "group_by_category" : True ,
3671 "heading_level" : 2 ,
72+ "members_order" : "alphabetical" ,
73+ "docstring_section_style" : "table" ,
74+ "members" : None ,
75+ "inherited_members" : False ,
76+ "filters" : ["!^_[^_]" ],
77+ "annotations_path" : "brief" ,
78+ "preload_modules" : None ,
79+ "allow_inspection" : True ,
80+ "summary" : False ,
81+ "unwrap_annotated" : False ,
82+ "parameter_headings" : False ,
3783 }
3884 """The default configuration options.
3985
@@ -44,6 +90,11 @@ class TypescriptHandler(BaseHandler):
4490 **`heading_level`** | `int` | The initial heading level to use. | `2`
4591 """
4692
93+ def __init__ (self , * args , ** kwargs ) -> None :
94+ config_file_path = kwargs .pop ("config_file_path" , None )
95+ super ().__init__ (* args , ** kwargs )
96+ self ._collected = {}
97+
4798 def collect (self , identifier : str , config : MutableMapping [str , Any ]) -> CollectorItem : # noqa: ARG002
4899 """Collect data given an identifier and selection configuration.
49100
@@ -60,8 +111,22 @@ def collect(self, identifier: str, config: MutableMapping[str, Any]) -> Collecto
60111 Returns:
61112 Anything you want, as long as you can feed it to the `render` method.
62113 """
63- raise CollectionError ("Implement me!" )
64-
114+ if config .get ("fallback" , False ):
115+ raise CollectionError ("Not loading modules during fallback" )
116+ if identifier not in self ._collected :
117+ data = load_typedoc (["typedoc" ])
118+ if data .kind is ReflectionKind .PROJECT :
119+ self ._collected .update ({module .name : module for module in data .children })
120+ else :
121+ self ._collected [data .name ] = data
122+ logger .debug (f"Collected { ', ' .join (self ._collected .keys ())} " )
123+ if identifier in self ._collected :
124+ for child in self ._collected [identifier ].children :
125+ if child .kind is ReflectionKind .MODULE and child .name == "index" :
126+ return child
127+ return self ._collected [identifier ]
128+ raise CollectionError (f"Could not collect { identifier } " )
129+
65130 def render (self , data : CollectorItem , config : Mapping [str , Any ]) -> str : # noqa: ARG002
66131 """Render a template using provided data and configuration options.
67132
@@ -73,13 +138,12 @@ def render(self, data: CollectorItem, config: Mapping[str, Any]) -> str: # noqa
73138 Returns:
74139 The rendered template as HTML.
75140 """
76- # final_config = {**self.default_config, **config}
77- # heading_level = final_config["heading_level"]
78- # template = self.env.get_template(f"{data...}.html")
79- # return template.render(
80- # **{"config": final_config, data...: data, "heading_level": heading_level, "root": True},
81- # )
82- raise PluginError ("Implement me!" )
141+ final_config = {** self .default_config , ** config }
142+ heading_level = final_config ["heading_level" ]
143+ template = self .env .get_template (f"module.html" )
144+ return template .render (
145+ ** {"config" : final_config , "module" : data , "heading_level" : heading_level , "root" : True },
146+ )
83147
84148 def update_env (self , md : Markdown , config : dict ) -> None :
85149 """Update the Jinja environment with any custom settings/filters/options for this handler.
@@ -116,7 +180,5 @@ def get_handler(
116180 handler = "typescript" ,
117181 theme = theme ,
118182 custom_templates = custom_templates ,
119- # To pass the following argument,
120- # you'll need to override the handler's __init__ method.
121- # config_file_path=config_file_path,
183+ config_file_path = config_file_path ,
122184 )
0 commit comments