Skip to content

Commit 85b3fba

Browse files
committed
feat: show only search results for current language
1 parent 98a2bb0 commit 85b3fba

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

2021/docs/scripts/extra.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
window.addEventListener("DOMContentLoaded", _ => {
2+
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
3+
const observer = new MutationObserver((mutations, _) => {
4+
const nodesForRemoval = [];
5+
for (const record of mutations) {
6+
for (const liNode of record.addedNodes) {
7+
let removeNode = false;
8+
for (const anchor of liNode.querySelectorAll("a")) {
9+
const searchResultLocale = getSearchResultLocaleFromAnchor(anchor);
10+
const isSearchResultFromCurrentPageLocale = searchResultLocale === document.querySelector('html[lang]').lang;
11+
if (!isSearchResultFromCurrentPageLocale) {
12+
removeNode = true;
13+
continue;
14+
}
15+
}
16+
17+
if (removeNode) {
18+
nodesForRemoval.push(liNode);
19+
}
20+
}
21+
}
22+
23+
for (const node of nodesForRemoval) {
24+
node.remove();
25+
}
26+
27+
const amountDisplay = document.querySelector(".md-search-result__meta");
28+
const result = document.querySelector('.md-search-result__list').childNodes.length
29+
amountDisplay.textContent = amountDisplay.textContent.replace(/\d+/i, result.toString());
30+
});
31+
32+
observer.observe(document.querySelector(".md-search-result__list"), { childList: true });
33+
});
34+
35+
function getSearchResultLocaleFromAnchor(anchor) {
36+
const localeSegment = anchor.href.split("/")[3];
37+
// Note that we make an assumption here that the only length 2
38+
// link segments will be the locale immediately after the site's base URL.
39+
return (localeSegment.length === 2 || localeSegment.length === 5 || localeSegment.length === 7) ? localeSegment : 'en';
40+
}

2021/mkdocs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ theme:
3838
extra_css:
3939
- stylesheets/extra.css
4040

41+
extra_javascript:
42+
- scripts/extra.js
43+
4144
markdown_extensions:
4245
- abbr
4346
- attr_list

0 commit comments

Comments
 (0)