Skip to content

Commit

Permalink
Add multi-language support for the search index
Browse files Browse the repository at this point in the history
  • Loading branch information
jp7677 committed Apr 7, 2023
1 parent 0b400e8 commit d5ef5c6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class SearchViewModel(generatorContext: GeneratorContext) : PageViewModel(genera
override val pageSubTitle = "Search results"
override val url = url()

val language: String = generatorContext.workspace.views
.configuration.properties.getOrDefault("structurizr.style.search.language", "")

val documents = buildList {
add(home(generatorContext.workspace.documentation, this@SearchViewModel))
addAll(workspaceDecisions(generatorContext.workspace.documentation, this@SearchViewModel))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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')
Expand All @@ -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",
)

0 comments on commit d5ef5c6

Please sign in to comment.