2021-07-14 23:49:18 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
import path from 'path'
|
|
|
|
import lunr from 'lunr'
|
2021-07-29 23:28:30 +03:00
|
|
|
import lunrStemmerSupport from 'lunr-languages/lunr.stemmer.support.js'
|
|
|
|
import tinyseg from 'lunr-languages/tinyseg.js'
|
|
|
|
import lunrJa from 'lunr-languages/lunr.ja.js'
|
|
|
|
import lunrEs from 'lunr-languages/lunr.es.js'
|
|
|
|
import lunrPt from 'lunr-languages/lunr.pt.js'
|
|
|
|
import lunrDe from 'lunr-languages/lunr.de.js'
|
|
|
|
import fs from 'fs/promises'
|
2021-07-14 23:49:18 +03:00
|
|
|
import rank from './rank.js'
|
|
|
|
import validateRecords from './validate-records.js'
|
|
|
|
import { compress } from '../../lib/search/compress.js'
|
2021-07-29 23:28:30 +03:00
|
|
|
|
2021-07-14 23:49:18 +03:00
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
2021-07-29 23:28:30 +03:00
|
|
|
lunrStemmerSupport(lunr)
|
|
|
|
tinyseg(lunr)
|
|
|
|
lunrJa(lunr)
|
|
|
|
lunrEs(lunr)
|
|
|
|
lunrPt(lunr)
|
|
|
|
lunrDe(lunr)
|
2021-07-14 23:49:18 +03:00
|
|
|
|
|
|
|
export default class LunrIndex {
|
2021-07-15 00:35:01 +03:00
|
|
|
constructor(name, records) {
|
2021-01-28 22:38:22 +03:00
|
|
|
this.name = name
|
|
|
|
|
|
|
|
// Add custom rankings
|
2021-07-15 00:35:01 +03:00
|
|
|
this.records = records.map((record) => {
|
2021-01-28 22:38:22 +03:00
|
|
|
record.customRanking = rank(record)
|
|
|
|
return record
|
|
|
|
})
|
|
|
|
|
|
|
|
this.validate()
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2021-07-15 00:35:01 +03:00
|
|
|
validate() {
|
2021-01-28 22:38:22 +03:00
|
|
|
return validateRecords(this.name, this.records)
|
|
|
|
}
|
|
|
|
|
2021-07-15 00:35:01 +03:00
|
|
|
build() {
|
2021-01-28 22:38:22 +03:00
|
|
|
const language = this.name.split('-').pop()
|
|
|
|
const records = this.records
|
|
|
|
|
2021-07-15 00:35:01 +03:00
|
|
|
this.index = lunr(function constructIndex() {
|
|
|
|
// No arrow here!
|
2021-01-28 22:38:22 +03:00
|
|
|
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')
|
2021-03-17 23:11:29 +03:00
|
|
|
this.field('topics')
|
2021-01-28 22:38:22 +03:00
|
|
|
this.field('customRanking')
|
|
|
|
|
|
|
|
this.metadataWhitelist = ['position']
|
|
|
|
|
|
|
|
for (const record of records) {
|
|
|
|
this.add(record)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-15 00:35:01 +03:00
|
|
|
toJSON() {
|
2021-01-28 22:38:22 +03:00
|
|
|
this.build()
|
|
|
|
return JSON.stringify(this.index, null, 2)
|
|
|
|
}
|
|
|
|
|
2021-07-15 00:35:01 +03:00
|
|
|
get recordsObject() {
|
|
|
|
return Object.fromEntries(this.records.map((record) => [record.objectID, record]))
|
2021-01-28 22:38:22 +03:00
|
|
|
}
|
|
|
|
|
2021-07-15 00:35:01 +03:00
|
|
|
async write() {
|
2021-01-28 22:38:22 +03:00
|
|
|
this.build()
|
|
|
|
|
|
|
|
// Write the parsed records
|
|
|
|
await Promise.resolve(this.recordsObject)
|
|
|
|
.then(JSON.stringify)
|
|
|
|
.then(compress)
|
2021-07-15 00:35:01 +03:00
|
|
|
.then((content) =>
|
|
|
|
fs.writeFile(
|
|
|
|
path.posix.join(__dirname, '../../lib/search/indexes', `${this.name}-records.json.br`),
|
|
|
|
content
|
|
|
|
// Do not set to 'utf8'
|
|
|
|
)
|
|
|
|
)
|
2021-01-28 22:38:22 +03:00
|
|
|
|
|
|
|
// Write the index
|
|
|
|
await Promise.resolve(this.index)
|
|
|
|
.then(JSON.stringify)
|
|
|
|
.then(compress)
|
2021-07-15 00:35:01 +03:00
|
|
|
.then((content) =>
|
|
|
|
fs.writeFile(
|
|
|
|
path.posix.join(__dirname, '../../lib/search/indexes', `${this.name}.json.br`),
|
|
|
|
content
|
|
|
|
// Do not set to 'utf8'
|
|
|
|
)
|
|
|
|
)
|
2021-01-28 22:38:22 +03:00
|
|
|
}
|
|
|
|
}
|