feat(website): embed filter-to-url map in index for client routing

Adds filter_urls dict (categories, groups, subcategories) in build.py,
passes filter_urls_json to the template, and injects a JSON script block
before the results section in index.html. Covered by a new test that
verifies all three URL types are present and correctly resolved.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Vinta Chen
2026-05-03 00:36:41 +08:00
parent e320ba7278
commit e0e7fc9168
3 changed files with 51 additions and 0 deletions

View File

@@ -315,6 +315,15 @@ def build(repo_root: Path) -> None:
entries = sort_entries(entries)
category_urls = {cat["name"]: category_path(cat) for cat in categories}
filter_urls: dict[str, str] = {}
for cat in categories:
filter_urls[cat["name"]] = category_path(cat)
for group in parsed_groups:
filter_urls[group["name"]] = f"/categories/{group['slug']}/"
for entry in entries:
for sub in entry.get("subcategories", []):
filter_urls[sub["value"]] = sub["url"]
env = Environment(
loader=FileSystemLoader(website / "templates"),
autoescape=True,
@@ -339,6 +348,7 @@ def build(repo_root: Path) -> None:
build_date=build_date.strftime("%B %d, %Y"),
sponsors=sponsors,
category_urls=category_urls,
filter_urls_json=json.dumps(filter_urls, sort_keys=True),
),
encoding="utf-8",
)

View File

@@ -113,6 +113,7 @@
</section>
{% endif %}
<script type="application/json" id="filter-urls">{{ filter_urls_json | safe }}</script>
<section class="results-section" id="library-index">
<div class="results-intro section-shell" data-reveal>
<div>

View File

@@ -615,6 +615,46 @@ class TestBuild:
parent = (site / "categories" / "web-frameworks" / "index.html").read_text(encoding="utf-8")
assert "category-breadcrumb" not in parent
def test_index_embeds_filter_urls_json(self, tmp_path):
readme = textwrap.dedent("""\
# T
---
**AI & ML**
## Deep Learning
- [dl1](https://example.com/dl1) - DL.
## Machine Learning
- Classical
- [ml1](https://example.com/ml1) - ML.
# Contributing
Done.
""")
self._copy_real_templates(tmp_path)
(tmp_path / "README.md").write_text(readme, encoding="utf-8")
build(tmp_path)
site = tmp_path / "website" / "output"
index_html = (site / "index.html").read_text(encoding="utf-8")
marker = '<script type="application/json" id="filter-urls">'
assert marker in index_html
start = index_html.index(marker) + len(marker)
end = index_html.index("</script>", start)
data = json.loads(index_html[start:end])
assert data["Deep Learning"] == "/categories/deep-learning/"
assert data["Machine Learning"] == "/categories/machine-learning/"
assert data["AI & ML"] == "/categories/ai-ml/"
assert data["Machine Learning > Classical"] == "/categories/machine-learning/classical/"
def test_build_creates_group_pages(self, tmp_path):
readme = textwrap.dedent("""\
# T