1
0
mirror of synced 2025-12-23 11:54:18 -05:00
Files
docs/script/search/lunr-search-index.js
James M. Greene 542a459c06 Move script-only dependencies to devDependencies (#19542)
* Move lib/search/sync.js to script/search/sync.js

* Move mdast-util-from-markdown to devDeps

* Move lib/redirects/add-redirect-to-frontmatter.js to script/helpers/

* Move mkdirp to devDeps

* Move linkinator to devDeps

* Move rimraf to devDeps

* Fix script/search/sync.js require paths

* Move lib/search/build-records.js to script/search/

* Move lib/search/find-indexable-pages to script/search/

* Fix require paths for build-records

* Fix require paths for find-indexable-pages

* Move lib/search/algolia-get-remote-index-names.js to script/search/

* Movbe lib/search/algolia-search-index.js to script/search/

* Move lib/search/lunr-search-index.js to script/search/

* Move lib/search/lunr-get-index-names.js to script/search/

* Fix Lunr search index paths

* Move lib/search/validate-records.js to script/search/

* Move is-url to devDeps

* Move lib/search/algolia-client.js to script/search/

* Move lib/search/parse-page-sections-into-records.js to script/search/

* Move lib/search/rank.js to script/search/

* Fix path to cached-index-names.json file

* Normalize require for fs.promises
2021-05-25 20:44:19 +00:00

95 lines
2.3 KiB
JavaScript

const lunr = require('lunr')
require('lunr-languages/lunr.stemmer.support')(lunr)
require('lunr-languages/tinyseg')(lunr)
require('lunr-languages/lunr.ja')(lunr)
require('lunr-languages/lunr.es')(lunr)
require('lunr-languages/lunr.pt')(lunr)
require('lunr-languages/lunr.de')(lunr)
const fs = require('fs').promises
const path = require('path')
const rank = require('./rank')
const validateRecords = require('./validate-records')
const { compress } = require('../../lib/search/compress')
module.exports = class LunrIndex {
constructor (name, records) {
this.name = name
// Add custom rankings
this.records = records.map(record => {
record.customRanking = rank(record)
return record
})
this.validate()
return this
}
validate () {
return validateRecords(this.name, this.records)
}
build () {
const language = this.name.split('-').pop()
const records = this.records
this.index = lunr(function constructIndex () { // No arrow here!
if (['ja', 'es', 'pt', 'de'].includes(language)) {
this.use(lunr[language])
}
this.ref('objectID')
this.field('url')
this.field('slug')
this.field('breadcrumbs')
this.field('heading')
this.field('title')
this.field('content')
this.field('topics')
this.field('customRanking')
this.metadataWhitelist = ['position']
for (const record of records) {
this.add(record)
}
})
}
toJSON () {
this.build()
return JSON.stringify(this.index, null, 2)
}
get recordsObject () {
return Object.fromEntries(
this.records.map(record => [record.objectID, record])
)
}
async write () {
this.build()
// Write the parsed records
await Promise.resolve(this.recordsObject)
.then(JSON.stringify)
.then(compress)
.then(content => fs.writeFile(
path.posix.join(__dirname, '../../lib/search/indexes', `${this.name}-records.json.br`),
content
// Do not set to 'utf8'
))
// Write the index
await Promise.resolve(this.index)
.then(JSON.stringify)
.then(compress)
.then(content => fs.writeFile(
path.posix.join(__dirname, '../../lib/search/indexes', `${this.name}.json.br`),
content
// Do not set to 'utf8'
))
}
}