From 7b52bf423214194ab39392db6e9bc2de9d36a02d Mon Sep 17 00:00:00 2001 From: Alex Shpak Date: Sun, 10 Nov 2019 14:35:07 +0100 Subject: [PATCH] #87, Add support for multilang search index generation --- assets/search-data.js | 6 ++++-- assets/search.js | 4 +++- exampleSite/config.toml | 22 ++++++++++++---------- exampleSite/config.yaml | 2 +- layouts/partials/docs/html-head.html | 3 ++- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/assets/search-data.js b/assets/search-data.js index a3fd51b..9c7f6a9 100644 --- a/assets/search-data.js +++ b/assets/search-data.js @@ -2,8 +2,10 @@ (function() { const indexCfg = {{ with .Site.Params.BookSearchConfig }} - {{ . }} - {{ end }}; + {{ . }}; + {{ else }} + {}; + {{ end }} indexCfg.doc = { id: 'id', diff --git a/assets/search.js b/assets/search.js index 89302b1..f21d4bf 100644 --- a/assets/search.js +++ b/assets/search.js @@ -1,6 +1,8 @@ 'use strict'; -{{- $searchData := resources.Get "search-data.js" | resources.ExecuteAsTemplate "search-data.js" . | resources.Minify | resources.Fingerprint }} +{{ $searchDataFile := printf "%s.%s" .Language.Lang "search-data.js" }} +{{ $searchData := resources.Get "search-data.js" | resources.ExecuteAsTemplate $searchDataFile . | resources.Minify | resources.Fingerprint }} + (function() { const input = document.querySelector('#book-search-input'); const results = document.querySelector('#book-search-results'); diff --git a/exampleSite/config.toml b/exampleSite/config.toml index db2bb08..96e8165 100644 --- a/exampleSite/config.toml +++ b/exampleSite/config.toml @@ -17,15 +17,17 @@ pygmentsCodeFences = true defaultContentLanguage = 'en' [languages] - [languages.en] - languageName = 'English' - weight = 1 - [languages.ru] - languageName = 'Russian' - weight = 2 - [languages.cn] - languageName = 'Chinese' - weight = 3 +[languages.en] + languageName = 'English' + weight = 1 + +[languages.ru] + languageName = 'Russian' + weight = 2 + +[languages.cn] + languageName = 'Chinese' + weight = 3 [params] # (Optional, default 6) Set how many table of contents levels to be showed on page. @@ -63,7 +65,7 @@ defaultContentLanguage = 'en' # Index is built on fly, therefore it might slowdown your website. BookSearch = true - # (Optional, default none) Search config for flexsearch index. + # (Optional, default {}) Search config for flexsearch index. # Read documentation at https://github.com/nextapps-de/flexsearch#usage. By default 'balance' preset is used. # This option allows to set specific configs for languages support. Must be valid JavaScript object. BookSearchConfig = '{ cache: true }' diff --git a/exampleSite/config.yaml b/exampleSite/config.yaml index 720d5d3..6db35ed 100644 --- a/exampleSite/config.yaml +++ b/exampleSite/config.yaml @@ -63,7 +63,7 @@ params: # Index is built on fly, therefore it might slowdown your website. BookSearch: true - # (Optional, default none) Search config for flexsearch index. + # (Optional, default {}) Search config for flexsearch index. # Read documentation at https://github.com/nextapps-de/flexsearch#usage. By default 'balance' preset is used. # This option allows to set specific configs for languages support. Must be valid JavaScript object. BookSearchConfig: | diff --git a/layouts/partials/docs/html-head.html b/layouts/partials/docs/html-head.html index 28815a1..c3a692f 100644 --- a/layouts/partials/docs/html-head.html +++ b/layouts/partials/docs/html-head.html @@ -10,7 +10,8 @@ {{ if default true .Site.Params.BookSearch }} -{{- $searchJS := resources.Get "search.js" | resources.ExecuteAsTemplate "search.js" . | resources.Minify | resources.Fingerprint }} +{{- $searchJSFile := printf "%s.%s" .Language.Lang "search.js" -}} +{{- $searchJS := resources.Get "search.js" | resources.ExecuteAsTemplate $searchJSFile . | resources.Minify | resources.Fingerprint }} {{ end }}