Skip to content

Commit

Permalink
Custom trimmer to index @ (#1581)
Browse files Browse the repository at this point in the history
Replaces default trimmer with custom trimmer to allow `@` at the beginning of a word.

Closes #1578.
  • Loading branch information
timothyvanderaerden committed Jun 25, 2022
1 parent 7f3b649 commit 14f0efc
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions assets/js/search-page.js
Expand Up @@ -59,6 +59,7 @@ function loadIndex () {
const serializedIndex = sessionStorage.getItem(indexStorageKey())
if (serializedIndex) {
registerElixirTokenFunction()
registerElixirTrimmerFunction()
return lunr.Index.load(JSON.parse(serializedIndex))
} else {
return null
Expand Down Expand Up @@ -90,6 +91,8 @@ function createIndex () {
this.metadataWhitelist = ['position']
this.pipeline.remove(lunr.stopWordFilter)
this.use(elixirTokenSplitter)
this.pipeline.remove(lunr.trimmer)
this.use(elixirTrimmer)

searchNodes.forEach(searchNode => {
this.add(searchNode)
Expand Down Expand Up @@ -122,6 +125,22 @@ function registerElixirTokenFunction () {
return lunr.Pipeline.registerFunction(elixirTokenFunction, 'elixirTokenSplitter')
}

function elixirTrimmer (builder) {
registerElixirTrimmerFunction()
builder.pipeline.after(lunr.stemmer, elixirTrimmerFunction)
builder.searchPipeline.after(lunr.stemmer, elixirTrimmerFunction)
}

function elixirTrimmerFunction (token) {
return token.update(function (s) {
return s.replace(/^@?\W+/, '').replace(/\W+$/, '')
})
}

function registerElixirTrimmerFunction () {
return lunr.Pipeline.registerFunction(elixirTrimmerFunction, 'elixirTrimmer')
}

function searchResultsToDecoratedSearchNodes (results) {
return results
// If the docs are regenerated without changing its version,
Expand Down

0 comments on commit 14f0efc

Please sign in to comment.