Skip to content

Commit 444562c

Browse files
authored
fix: use algolia search lang (#459)
1 parent a8f147f commit 444562c

File tree

2 files changed

+4
-41
lines changed

2 files changed

+4
-41
lines changed

docs/config/algolia-search.md

+1-38
Original file line numberDiff line numberDiff line change
@@ -47,41 +47,4 @@ module.exports = {
4747
}
4848
```
4949

50-
VitePress will automatically add a `language` _facetFilter_ to the `searchParameters.facetFilter` array with the correct language value. **Make sure to properly configure your DocSearch config as well** by adding `language` as a _custom attribute for faceting_ and by setting it based on the `lang` attribute of the `<html>` element. Here is a short example of DocSearch config:
51-
52-
```json
53-
{
54-
"index_name": "<the name of your library>",
55-
"start_urls": [
56-
{
57-
"url": "<your deployed url>"
58-
}
59-
],
60-
"stop_urls": ["(?:(?<!\\.html)(?<!/))$"],
61-
"selectors": {
62-
"lvl0": {
63-
"selector": ".sidebar > .sidebar-links > .sidebar-link .sidebar-link-item.active",
64-
"global": true,
65-
"default_value": "Documentation"
66-
},
67-
"lvl1": ".content h1",
68-
"lvl2": ".content h2",
69-
"lvl3": ".content h3",
70-
"lvl4": ".content h4",
71-
"lvl5": ".content h5",
72-
"lvl6": ".content p, .content li",
73-
"text": ".content [class^=language-]",
74-
"language": {
75-
"selector": "/html/@lang",
76-
"type": "xpath",
77-
"global": true,
78-
"default_value": "en-US"
79-
}
80-
},
81-
"custom_settings": {
82-
"attributesForFaceting": ["language"]
83-
}
84-
}
85-
```
86-
87-
You can take a look at the [DocSearch config used by Vue Router](https://github.com/algolia/docsearch-configs/blob/master/configs/next_router_vuejs.json) for a complete example.
50+
VitePress will automatically add a `lang` _facetFilter_ to the `searchParameters.facetFilter` array with the correct language value. Algolia automatically adds the correct facet filter based on the `lang` attribute on the `<html>` tag. This will match search results with the currently viewed language of the page.

src/client/theme-default/components/AlgoliaSearchBox.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const { lang } = useData()
5555
// if the user has multiple locales, the search results should be filtered
5656
// based on the language
5757
const facetFilters: string[] = props.multilang
58-
? ['language:' + lang.value]
58+
? ['lang:' + lang.value]
5959
: []
6060
6161
if (props.options.searchParameters?.facetFilters) {
@@ -66,10 +66,10 @@ watch(
6666
lang,
6767
(newLang, oldLang) => {
6868
const index = facetFilters.findIndex(
69-
(filter) => filter === 'language:' + oldLang
69+
(filter) => filter === 'lang:' + oldLang
7070
)
7171
if (index > -1) {
72-
facetFilters.splice(index, 1, 'language:' + newLang)
72+
facetFilters.splice(index, 1, 'lang:' + newLang)
7373
}
7474
}
7575
)

0 commit comments

Comments
 (0)