@@ -36,6 +36,7 @@ class _MDPageInfo(NamedTuple):
36
36
path_md : Path
37
37
md_url : str
38
38
content : str
39
+ description : str
39
40
40
41
41
42
class MkdocsLLMsTxtPlugin (BasePlugin [_PluginConfig ]):
@@ -56,13 +57,21 @@ class MkdocsLLMsTxtPlugin(BasePlugin[_PluginConfig]):
56
57
md_pages : dict [str , list [_MDPageInfo ]]
57
58
"""Dictionary mapping section names to a list of page infos."""
58
59
59
- def _expand_inputs (self , inputs : list [str ], page_uris : list [str ]) -> list [str ]:
60
- expanded : list [str ] = []
61
- for input_file in inputs :
60
+ _sections : dict [str , dict [str , str ]]
61
+
62
+ def _expand_inputs (self , inputs : list [str | dict [str , str ]], page_uris : list [str ]) -> dict [str , str ]:
63
+ expanded : dict [str , str ] = {}
64
+ for input_item in inputs :
65
+ if isinstance (input_item , dict ):
66
+ input_file , description = next (iter (input_item .items ()))
67
+ else :
68
+ input_file = input_item
69
+ description = ""
62
70
if "*" in input_file :
63
- expanded .extend (fnmatch .filter (page_uris , input_file ))
71
+ for match in fnmatch .filter (page_uris , input_file ):
72
+ expanded [match ] = description
64
73
else :
65
- expanded . append ( input_file )
74
+ expanded [ input_file ] = description
66
75
return expanded
67
76
68
77
def on_config (self , config : MkDocsConfig ) -> MkDocsConfig | None :
@@ -81,6 +90,7 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None:
81
90
if config .site_url is None :
82
91
raise ValueError ("'site_url' must be set in the MkDocs configuration to be used with the 'llmstxt' plugin" )
83
92
self .mkdocs_config = config
93
+
84
94
# A `defaultdict` could be used, but we need to retain the same order between `config.sections` and `md_pages`
85
95
# (which wouldn't be guaranteed when filling `md_pages` in `on_page_content()`).
86
96
self .md_pages = {section : [] for section in self .config .sections }
@@ -100,10 +110,10 @@ def on_files(self, files: Files, *, config: MkDocsConfig) -> Files | None: # no
100
110
Modified collection or none.
101
111
"""
102
112
page_uris = list (files .src_uris )
103
-
104
- for section_name , file_list in list ( self .config . sections . items ()):
105
- self .config .sections [ section_name ] = self . _expand_inputs ( file_list , page_uris = page_uris )
106
-
113
+ self . _sections = {
114
+ section_name : self ._expand_inputs ( file_list , page_uris = page_uris ) # type: ignore[arg-type]
115
+ for section_name , file_list in self .config .sections . items ( )
116
+ }
107
117
return files
108
118
109
119
def on_page_content (self , html : str , * , page : Page , ** kwargs : Any ) -> str | None : # noqa: ARG002
@@ -115,8 +125,9 @@ def on_page_content(self, html: str, *, page: Page, **kwargs: Any) -> str | None
115
125
html: The rendered HTML.
116
126
page: The page object.
117
127
"""
118
- for section_name , file_list in self .config .sections .items ():
119
- if page .file .src_uri in file_list :
128
+ src_uri = page .file .src_uri
129
+ for section_name , files in self ._sections .items ():
130
+ if src_uri in files :
120
131
path_md = Path (page .file .abs_dest_path ).with_suffix (".md" )
121
132
page_md = _generate_page_markdown (
122
133
html ,
@@ -138,10 +149,11 @@ def on_page_content(self, html: str, *, page: Page, **kwargs: Any) -> str | None
138
149
139
150
self .md_pages [section_name ].append (
140
151
_MDPageInfo (
141
- title = page .title if page .title is not None else page . file . src_uri ,
152
+ title = page .title if page .title is not None else src_uri ,
142
153
path_md = path_md ,
143
154
md_url = md_url ,
144
155
content = page_md ,
156
+ description = files [src_uri ],
145
157
),
146
158
)
147
159
@@ -169,10 +181,10 @@ def on_post_build(self, *, config: MkDocsConfig, **kwargs: Any) -> None: # noqa
169
181
170
182
for section_name , file_list in self .md_pages .items ():
171
183
markdown += f"## { section_name } \n \n "
172
- for page_title , path_md , md_url , content in file_list :
184
+ for page_title , path_md , md_url , content , desc in file_list :
173
185
path_md .write_text (content , encoding = "utf8" )
174
186
_logger .debug (f"Generated MD file to { path_md } " )
175
- markdown += f"- [{ page_title } ]({ md_url } )\n "
187
+ markdown += f"- [{ page_title } ]({ md_url } ){ ( ': ' + desc ) if desc else '' } \n "
176
188
markdown += "\n "
177
189
178
190
output_file .write_text (markdown , encoding = "utf8" )
0 commit comments