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 10, 2023
1 parent 16ba3da commit 6b1740c
Show file tree
Hide file tree
Showing 3 changed files with 65 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",
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,31 @@ package nl.avisi.structurizr.site.generatr.site.model
import assertk.assertThat
import assertk.assertions.containsExactly
import assertk.assertions.hasSize
import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo
import com.structurizr.documentation.Format
import com.structurizr.documentation.Section
import org.junit.jupiter.api.Test

class SearchViewModelTest : ViewModelTest() {

@Test
fun `no index language configured`() {
val viewModel = SearchViewModel(generatorContext())

assertThat(viewModel.language).isEmpty()
}

@Test
fun `index language configured`() {
val generatorContext = generatorContext().apply {
workspace.views.configuration.addProperty("structurizr.style.search.language", "nl")
}
val viewModel = SearchViewModel(generatorContext)

assertThat(viewModel.language).isEqualTo("nl")
}

@Test
fun `nothing to index`() {
val viewModel = SearchViewModel(generatorContext())
Expand Down

0 comments on commit 6b1740c

Please sign in to comment.