Give each language edition its own search index - #30
Conversation
`mkdocs-static-i18n` merges every edition into one `search/search_index.json`, and Material resolves that file against `__config.base`, which is the site root on every page. Searching from a translated page therefore returns hits in the other languages, and the top results are often not in the language being read. Split the merged index per locale after the build and repoint `__config.base` on each edition's pages at the edition root — the only value Material derives the index URL from. The hook aborts the build when an edition ends up with no entries, when the merged index is absent, or when a page carries no `__config` script, so a change in any of those mechanisms fails loudly instead of silently restoring site-wide search. Ported from hatlabs/halpi2, where it is in production. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 10 included reviews per hour; 6 remain after this review. 📝 WalkthroughWalkthroughThe PR adds an MkDocs post-build hook that validates the merged search index, creates locale-specific indexes, retains default-language documents in the root index, and rewrites translated page base paths. ChangesInternationalized search build
Estimated code review effort: 3 (Moderate) | ~20 minutes Merge Risk: ⚪ Minimal · up to The change gives each language edition its own search index, preventing cross-language search results. The guarded build behavior and validation support merge readiness, with no actionable merge-blocking risk remaining beyond normal checks. Sequence Diagram(s)sequenceDiagram
participant MkDocs
participant i18n_search
participant SearchIndex
participant LocaleIndexes
participant TranslatedPages
MkDocs->>i18n_search: Run post-build hook
i18n_search->>SearchIndex: Validate and load merged index
i18n_search->>LocaleIndexes: Write locale-specific indexes
i18n_search->>TranslatedPages: Rewrite __config.base
i18n_search->>SearchIndex: Update root index with default-language documents
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. (1 skipped: 1 unsupported.)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Comment |
Searching from a translated page returns hits in the other nine languages.
mkdocs-static-i18nmerges every edition into onesearch/search_index.jsonby design, and Material resolves that file asnew URL("search/search_index.json", __config.base)wherebaseis the site root on every page, translated or not. Itsreconfigure_searchoption does not separate them — it only adds each locale to lunr'slanglist and drops duplicates.hooks/i18n_search.pysplits the merged index by locale after the build, strips the locale prefix from each entry'slocation, and repoints__config.baseon that edition's pages at the edition root.baseappears three times in the whole Material bundle: twice for the search index URL, once for a sitemap stream only subscribed undernavigation.instant, which this site does not enable — so the repoint changes nothing but search.Measured here after the change:
langnbfalls back toen, which has no lunr stemmer)mkdocs build --strictclean,check_anchors.py siteresolving 2600 links across 142 pages, and every locale page carries the edition-rootbase— checked programmatically against the expected depth rather than by eye.The hook fails the build when an edition ends up with no entries, when the merged index is missing, or when a page carries no
__configscript. Those are the three ways it could silently revert to site-wide search while the build stayed green; each guard was exercised on halpi2 rather than assumed.Worth noting for this repository specifically: the counts differ per edition because
tutorials/openplotter-server/index.mdis missing in all nine locales. That is the same gap the translation gate reports, and this change neither fixes nor worsens it — a locale short a page simply gets a smaller index.Ported unchanged from
hatlabs/halpi2, where it has been in production since this morning.🤖 Generated with Claude Code
Summary by CodeRabbit