Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use algolia search lang #459

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 1 addition & 38 deletions docs/config/algolia-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,41 +47,4 @@ module.exports = {
}
```

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:

```json
{
"index_name": "<the name of your library>",
"start_urls": [
{
"url": "<your deployed url>"
}
],
"stop_urls": ["(?:(?<!\\.html)(?<!/))$"],
"selectors": {
"lvl0": {
"selector": ".sidebar > .sidebar-links > .sidebar-link .sidebar-link-item.active",
"global": true,
"default_value": "Documentation"
},
"lvl1": ".content h1",
"lvl2": ".content h2",
"lvl3": ".content h3",
"lvl4": ".content h4",
"lvl5": ".content h5",
"lvl6": ".content p, .content li",
"text": ".content [class^=language-]",
"language": {
"selector": "/html/@lang",
"type": "xpath",
"global": true,
"default_value": "en-US"
}
},
"custom_settings": {
"attributesForFaceting": ["language"]
}
}
```

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.
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.
6 changes: 3 additions & 3 deletions src/client/theme-default/components/AlgoliaSearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const { lang } = useData()
// if the user has multiple locales, the search results should be filtered
// based on the language
const facetFilters: string[] = props.multilang
? ['language:' + lang.value]
? ['lang:' + lang.value]
: []

if (props.options.searchParameters?.facetFilters) {
Expand All @@ -66,10 +66,10 @@ watch(
lang,
(newLang, oldLang) => {
const index = facetFilters.findIndex(
(filter) => filter === 'language:' + oldLang
(filter) => filter === 'lang:' + oldLang
)
if (index > -1) {
facetFilters.splice(index, 1, 'language:' + newLang)
facetFilters.splice(index, 1, 'lang:' + newLang)
}
}
)
Expand Down