-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multi-language support for the search index
- Loading branch information
Showing
2 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,12 @@ import nl.avisi.structurizr.site.generatr.site.asUrlToFile | |
import nl.avisi.structurizr.site.generatr.site.model.SearchViewModel | ||
|
||
fun HTML.searchPage(viewModel: SearchViewModel) { | ||
val language = if (viewModel.language == "en") "" else viewModel.language | ||
if (!supportedLanguages.contains(language)) | ||
throw IllegalArgumentException( | ||
"Indexing language $language is not supported, available languages are $supportedLanguages" | ||
) | ||
|
||
page(viewModel) { | ||
contentDiv { | ||
h2 { +viewModel.pageSubTitle } | ||
|
@@ -18,12 +24,19 @@ fun HTML.searchPage(viewModel: SearchViewModel) { | |
type = ScriptType.textJavaScript, | ||
src = "https://cdn.jsdelivr.net/npm/[email protected]/min/lunr.stemmer.support.min.js" | ||
) { } | ||
if (language.isNotBlank()) | ||
script( | ||
type = ScriptType.textJavaScript, | ||
src = "https://cdn.jsdelivr.net/npm/[email protected]/min/lunr.$language.min.js" | ||
) { } | ||
|
||
script(type = ScriptType.textJavaScript) { | ||
unsafe { | ||
+"const documents = ${Json.encodeToString(viewModel.documents)};" | ||
+"const idx = lunr(function () {" | ||
if (viewModel.language.isNotBlank()) | ||
+"this.use(lunr.${viewModel.language});" | ||
+""" | ||
const idx = lunr(function () { | ||
this.ref('href') | ||
this.field('text') | ||
|
@@ -42,3 +55,32 @@ fun HTML.searchPage(viewModel: SearchViewModel) { | |
} | ||
} | ||
} | ||
|
||
// From https://github.com/olivernn/lunr-languages | ||
private val supportedLanguages = listOf( | ||
"", | ||
"ar", | ||
"da", | ||
"de", | ||
"du", | ||
"es", | ||
"fi", | ||
"fr", | ||
"hi", | ||
"hu", | ||
"it", | ||
"ja", | ||
"jp", | ||
"ko", | ||
"nl", | ||
"no", | ||
"pt", | ||
"ro", | ||
"ru", | ||
"sv", | ||
"ta", | ||
"th", | ||
"tr", | ||
"vi", | ||
"zh", | ||
) |