From e8a6ab1b09d6cb0e85333c9db39619a7e132865a Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Wed, 8 Aug 2018 00:14:33 +0800 Subject: [PATCH] fix: algolia search doesn't work well at i18n mode Add lang facetFilters presets by following VuePress's i18n design logic --- lib/default-theme/AlgoliaSearchBox.vue | 32 +++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/default-theme/AlgoliaSearchBox.vue b/lib/default-theme/AlgoliaSearchBox.vue index 47a7cb78cf..0334ae0bbf 100644 --- a/lib/default-theme/AlgoliaSearchBox.vue +++ b/lib/default-theme/AlgoliaSearchBox.vue @@ -15,28 +15,44 @@ export default { props: ['options'], mounted () { - this.initialize() + this.initialize(this.options, this.$lang) }, methods: { - initialize () { + initialize (userOptions, lang) { Promise.all([ import(/* webpackChunkName: "docsearch" */ 'docsearch.js/dist/cdn/docsearch.min.js'), import(/* webpackChunkName: "docsearch" */ 'docsearch.js/dist/cdn/docsearch.min.css') ]).then(([docsearch]) => { docsearch = docsearch.default - docsearch(Object.assign(this.options, { - debug: true, - inputSelector: '#algolia-search-input' - })) + const { algoliaOptions = {}} = userOptions + docsearch(Object.assign( + {}, + userOptions, + { + inputSelector: '#algolia-search-input', + // #697 Make docsearch work well at i18n mode. + algoliaOptions: Object.assign({ + 'facetFilters': [`lang:${lang}`].concat(algoliaOptions.facetFilters || []) + }, algoliaOptions) + } + )) }) + }, + + update (options, lang) { + this.$el.innerHTML = '' + this.initialize(options, lang) } }, watch: { + $lang (newValue) { + this.update(this.options, newValue) + }, + options (newValue) { - this.$el.innerHTML = '' - this.initialize(newValue) + this.update(newValue, this.$lang) } } }