summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerghei Iakovlev <egrep@protonmail.ch>2022-07-17 12:32:24 +0200
committerSerghei Iakovlev <egrep@protonmail.ch>2022-07-17 12:32:24 +0200
commitbe4957a05dfc36f870e608f119a878f911a4d48c (patch)
tree84c4b0e6604c7fc2bde045070cc98acf0e105c0f
parent581ad2adf8536d20a0a37e6b2158aa6438a1a130 (diff)
downloadgohugo-theme-ed-be4957a05dfc36f870e608f119a878f911a4d48c.tar.gz
Set up the pipeline for indexing content in multiple languages
-rw-r--r--assets/js/search.js15
-rw-r--r--exampleSite/config/_default/params.yaml13
-rw-r--r--layouts/index.json10
-rw-r--r--layouts/shortcodes/form-search.html19
4 files changed, 45 insertions, 12 deletions
diff --git a/assets/js/search.js b/assets/js/search.js
index e964358..6cca4a5 100644
--- a/assets/js/search.js
+++ b/assets/js/search.js
@@ -12,13 +12,26 @@ async function initSearchIndex() {
// Create the lunr index for the search
searchIndex = lunr(function () { // eslint-disable-line no-undef
- this.use(lunr.multiLanguage('de', 'en', 'es', 'fr', 'it', 'pt', 'ru')); // eslint-disable-line no-undef
+ // Set up the pipeline for indexing content in multiple languages
+ if (Array.isArray(searchConfig.lunrLanguages)) {
+ // Lunr has full support for the indexing and searching of
+ // documents in English. So no need add 'en'.
+ let langs = searchConfig.lunrLanguages.slice();
+ langs = langs.filter(lang => lang !== 'en');
+
+ const pipeline = lunr.multiLanguage( // eslint-disable-line no-undef
+ ...langs
+ );
+
+ this.use(pipeline);
+ }
this.field('objectID');
this.field('title');
this.field('categories');
this.field('tags');
this.field('content');
+
this.ref('href');
pagesIndex.forEach((page) => this.add(page));
diff --git a/exampleSite/config/_default/params.yaml b/exampleSite/config/_default/params.yaml
index 9a306b3..8a7d1b8 100644
--- a/exampleSite/config/_default/params.yaml
+++ b/exampleSite/config/_default/params.yaml
@@ -103,3 +103,16 @@ search:
# Maximum length (in words) of each text blurb. You can change this
# value if you find that 100 is too short or too long for your taste.
maxSummaryLength: 100
+ # Lunr has full support for the indexing and searching of documents
+ # in English. If your documents are in another language, uncomment
+ # required languages below. No need add 'en' as its supported by Lunr
+ # out of box.
+ #
+ # For more see: https://lunrjs.com/guides/language_support.html
+ languages:
+ # - de
+ - es
+ # - fr
+ # - it
+ # - pt
+ # - ru
diff --git a/layouts/index.json b/layouts/index.json
index 538cfcc..a71d1e6 100644
--- a/layouts/index.json
+++ b/layouts/index.json
@@ -21,20 +21,20 @@
{{ if .File }}
{{- $pageData := (dict
"objectID" $page.File.UniqueID
- "date" $page.Date.UTC.Unix
"publishDate" $page.PublishDate
+ "date" $page.Date.UTC.Unix
"lastmod" $page.Lastmod.UTC.Unix
- "expiryDate" $page.ExpiryDate.UTC.Unix
- "lang" $page.Lang
+ "lang" ($page.Params.lang | default $page.Lang)
"title" $page.Title
"href" $page.Permalink
"kind" $page.Kind
"type" $page.Type
"section" $page.Section
- "tags" (delimit ($page.Params.tags | default slice) " ; ")
- "categories" (delimit ($page.Params.categories | default slice) " ; ")
"content" $page.Plain
+ "categories" ($page.Params.categories | default slice)
+ "tags" ($page.Params.tags | default slice)
) -}}
+
{{- $.Scratch.Add "pagesIndex" $pageData -}}
{{- $.Scratch.Add "urlsAdded" $page.Permalink -}}
{{- end -}}
diff --git a/layouts/shortcodes/form-search.html b/layouts/shortcodes/form-search.html
index 94a951c..a917aec 100644
--- a/layouts/shortcodes/form-search.html
+++ b/layouts/shortcodes/form-search.html
@@ -38,11 +38,18 @@
{{- $lunrSearch := slice (resources.Get "js/vendor/lunr.js") -}}
{{- /* Add lunr multilanguage support. */ -}}
-{{- $lunrSearch = $lunrSearch | append (resources.Get "js/vendor/lunr.multi.js") -}}
-{{- $lunrSearch = $lunrSearch | append (resources.Get "js/vendor/lunr.stemmer.support.js") -}}
-{{- $lunrLangs := slice "de" "es" "fr" "it" "pt" "ru" -}}
-{{- range $lunrLangs -}}
- {{ $lunrSearch = $lunrSearch | append (resources.Get (printf "js/vendor/lunr.%s.js" .)) -}}
+{{- $lunrLanguages := site.Params.search.languages | default slice -}}
+{{- if gt (len $lunrLanguages) 0 -}}
+ {{- $lunrSearch = $lunrSearch | append (resources.Get "js/vendor/lunr.multi.js") -}}
+ {{- $lunrSearch = $lunrSearch | append (resources.Get "js/vendor/lunr.stemmer.support.js") -}}
+
+ {{- range $lunrLanguages -}}
+ {{- $lang := . -}}
+ {{- $lang = $lang | lower -}}
+ {{- if and $lang (ne $lang "en") -}}
+ {{- $lunrSearch = $lunrSearch | append (resources.Get (printf "js/vendor/lunr.%s.js" $lang)) -}}
+ {{- end -}}
+ {{- end -}}
{{- end -}}
{{- /* Build lunr. */ -}}
@@ -61,7 +68,7 @@
{{- /* Configure search engine. */ -}}
{{- $maxSummaryLength := site.Params.search.maxSummaryLength | default 100 -}}
-{{- $searchConfig := dict "indexURI" ("/index.json" | relLangURL) "maxSummaryLength" $maxSummaryLength -}}
+{{- $searchConfig := dict "indexURI" ("/index.json" | relLangURL) "maxSummaryLength" $maxSummaryLength "lunrLanguages" $lunrLanguages -}}
{{- $searchI18n := dict "noResults" (i18n "search_no_results") -}}
{{- $searchParams := dict "searchConfig" $searchConfig "i18n" $searchI18n -}}