Skip to content

Commit 10c06fb

Browse files
committed
add category links in llms.txt
1 parent 5f042e2 commit 10c06fb

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎website/build.py‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
273303
def 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
)

‎website/tests/test_build.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ def test_build_creates_llms_text_alternate_without_sponsors(self, tmp_path):
352352
assert "Sitemap: https://awesome-python.com/sitemap.xml" in llms_txt
353353
assert "## Categories" in llms_txt
354354
assert "**Tools**" in llms_txt
355-
assert "- [Widgets](#widgets)" in llms_txt
355+
assert "- [Widgets](https://awesome-python.com/categories/widgets/)" in llms_txt
356+
assert "- [Widgets](#widgets)" not in llms_txt
356357
assert "### Widgets" in llms_txt
357358
assert "- [w1](https://example.com) - A widget." in llms_txt
358359
assert "- [w2](https://github.com/owner/w2) - A starred widget. (GitHub stars: 42)" in llms_txt

0 commit comments

Comments
 (0)