@@ -270,6 +270,36 @@ def extract_categories_body(markdown: str) -> str:
270270 return "" .join (lines [start_idx :end_idx ]).rstrip () + "\n "
271271
272272
273+ def github_markdown_anchor (text : str ) -> str :
274+ anchor = text .strip ().lower ()
275+ anchor = re .sub (r"[^\w\s-]" , "" , anchor )
276+ anchor = re .sub (r"\s" , "-" , anchor )
277+ return f"#{ anchor } "
278+
279+
280+ def link_llms_category_index_to_canonical_pages (markdown : str , categories : Sequence [ParsedSection ]) -> str :
281+ """Point the README-derived category index at canonical category pages."""
282+ category_urls = {}
283+ for category in categories :
284+ public_url = category_public_url (category )
285+ category_urls [f"#{ category ['slug' ]} " ] = public_url
286+ category_urls [github_markdown_anchor (category ["name" ])] = public_url
287+ lines = markdown .splitlines (keepends = True )
288+ out : list [str ] = []
289+
290+ def replace_link (match : re .Match [str ]) -> str :
291+ target = match .group (1 )
292+ url = category_urls .get (target )
293+ if url is None :
294+ return match .group (0 )
295+ return match .group (0 ).replace (f"({ target } )" , f"({ url } )" , 1 )
296+
297+ for line in lines :
298+ out .append (MARKDOWN_LINK_RE .sub (replace_link , line ))
299+
300+ return "" .join (out )
301+
302+
273303def build_llms_txt (
274304 template_text : str ,
275305 * ,
@@ -280,7 +310,10 @@ def build_llms_txt(
280310) -> str :
281311 """Render the llms.txt entry point with the curated category catalog."""
282312 categories_md = annotate_entries_with_stars (
283- extract_categories_body (readme_text ).rstrip (),
313+ link_llms_category_index_to_canonical_pages (
314+ extract_categories_body (readme_text ).rstrip (),
315+ categories ,
316+ ),
284317 stars_data ,
285318 format_stars = lambda n : f"GitHub stars: { n } " ,
286319 )
0 commit comments