Added loading logic for lunr-languages

This commit is contained in:
squidfunk
2020-01-26 15:42:22 +01:00
parent d329b6921f
commit 1fdb65b88f
15 changed files with 320 additions and 115 deletions

View File

@@ -75,7 +75,7 @@ interface Options {
* @return Main area state observable
*/
export function watchMain(
el: HTMLElement, { viewport }: Agent, { header$ }: Options
el: HTMLElement, { viewport }: Agent, { header$ }: Options
): Observable<MainState> {
/* Compute necessary adjustment for header */

View File

@@ -20,7 +20,7 @@
* IN THE SOFTWARE.
*/
import * as lunr from "lunr"
import * as lunr from "expose-loader?lunr!lunr"
import {
ArticleDocument,

View File

@@ -20,7 +20,7 @@
* IN THE SOFTWARE.
*/
import { SearchIndex } from "modules"
import { SearchIndex, SearchIndexConfig } from "modules"
import { SearchMessage, SearchMessageType } from "../_"
@@ -33,6 +33,33 @@ import { SearchMessage, SearchMessageType } from "../_"
*/
let index: SearchIndex
/* ----------------------------------------------------------------------------
* Helper functions
* ------------------------------------------------------------------------- */
/**
* Setup multi-language support through `lunr-languages`
*
* @param config - Search index configuration
*/
function setupLunrLanguages(config: SearchIndexConfig): void {
const base = "../lunr"
/* Add scripts for languages */
const scripts = [`${base}/lunr.stemmer.support.min.js`]
for (const lang of config.lang) {
if (lang === "ja") scripts.push(`${base}/tinyseg.js`)
if (lang !== "en") scripts.push(`${base}/lunr.${lang}.min.js`)
}
/* Add multi-language support */
if (scripts.length > 1)
scripts.push(`${base}/lunr.multi.min.js`)
/* Load scripts synchronously */
importScripts(...scripts)
}
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
@@ -49,6 +76,7 @@ export function handler(message: SearchMessage): SearchMessage {
/* Setup search index */
case SearchMessageType.SETUP:
setupLunrLanguages(message.data.config)
index = new SearchIndex(message.data)
return {
type: SearchMessageType.DUMP,

View File

@@ -379,29 +379,6 @@
{% block scripts %}
<script src="{{ 'assets/javascripts/bundle.js' | url }}"></script>
<!-- Load additional languages for search -->
{% if lang.t("search.language") != "en" %}
{% set languages = lang.t("search.language").split(",") %}
{% if languages | length and languages[0] != "" %}
{% set path = "assets/javascripts/lunr/" %}
<script src="{{ (path ~ 'lunr.stemmer.support.js') | url }}"></script>
{% for language in languages | map("trim") %}
{% if language != "en" %}
{% if language == "ja" %}
<script src="{{ (path ~ 'tinyseg.js') | url }}"></script>
{% endif %}
{% if language in ($md-lunr-languages$) %}
<script src="{{ (path ~ 'lunr.' ~ language ~ '.js') | url }}">
</script>
{% endif %}
{% endif %}
{% endfor %}
{% if languages | length > 1 %}
<script src="{{ (path ~ 'lunr.multi.js') | url }}"></script>
{% endif %}
{% endif %}
{% endif %}
<!-- Translations -->
<script id="__lang" type="application/json">
{%- set translations = {} -%}

View File

@@ -21,3 +21,7 @@
-->
{% extends "base.html" %}
{% block announcement %}
The quick brown fox jumps over the lazy dog
{% endblock %}