Skip to content

Commit

Permalink
Refactor indexing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Aug 16, 2022
1 parent c63e0f4 commit f50dba4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
25 changes: 6 additions & 19 deletions assets/js/search-page.js
Expand Up @@ -46,6 +46,11 @@ function renderResults ({ value, results, errorMessage }) {
}

function getIndex () {
lunr.QueryLexer.termSeparator = /\s+/
lunr.Pipeline.registerFunction(elixirTokenFunction, 'elixirTokenSplitter')
lunr.Pipeline.registerFunction(elixirTrimmerFunction, 'elixirTrimmer')
lunr.Pipeline.registerFunction(hyphenSearchFunction, 'hyphenSearch')

const cachedIndex = loadIndex()
if (cachedIndex) { return cachedIndex }

Expand All @@ -58,9 +63,6 @@ function loadIndex () {
try {
const serializedIndex = sessionStorage.getItem(indexStorageKey())
if (serializedIndex) {
registerElixirTokenFunction()
registerElixirTrimmerFunction()
registerHyphenSearchFunction()
return lunr.Index.load(JSON.parse(serializedIndex))
} else {
return null
Expand All @@ -85,7 +87,6 @@ function indexStorageKey () {
}

function createIndex () {
lunr.QueryLexer.termSeparator = /\s+/
return lunr(function () {
this.tokenizer.separator = /\s+/
this.ref('ref')
Expand All @@ -105,7 +106,6 @@ function createIndex () {
}

function elixirTokenSplitter (builder) {
registerElixirTokenFunction()
builder.pipeline.before(lunr.stemmer, elixirTokenFunction)
builder.searchPipeline.before(lunr.stemmer, elixirTokenFunction)
}
Expand All @@ -125,26 +125,18 @@ function elixirTokenFunction (token) {
return tokens
}

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) {
// Preserve @ at the beginning of tokens
return token.update(function (s) {
return s.replace(/^@?\W+/, '').replace(/\W+$/, '')
})
}

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

function hyphenSearchFunction (token) {
const tokenStr = token.toString()
if (tokenStr.indexOf('-') < 0) return token
Expand All @@ -162,15 +154,10 @@ function hyphenSearchFunction (token) {
}

function hyphenSearch (builder) {
registerHyphenSearchFunction ()
builder.pipeline.before(lunr.stemmer, hyphenSearchFunction)
builder.searchPipeline.before(lunr.stemmer, hyphenSearchFunction)
}

function registerHyphenSearchFunction () {
return lunr.Pipeline.registerFunction(hyphenSearchFunction, 'hypenSearch')
}

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

Large diffs are not rendered by default.

0 comments on commit f50dba4

Please sign in to comment.