aboutsummaryrefslogtreecommitdiff
path: root/scripts/make-search-index.js
blob: 635804babd3e923e52116606b8af71f5ee1ae1f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

/*
 * make-search-index.js
 *
 * This converts a "search data" file produced by the haskell site builder into
 * a lunr.js index and saves it in JSON. Metadata for rendering search output
 * (currently titles and tags) are stored separately in an extra file.
 *
 * Installing dependencies:
 * yarnpkg add lunr
 *
 * Usage:
 * site ....some args.... --search-data-output tmp/search-raw.json
 * node scripts/make-search-index.js tmp/search-raw.json _site/search-index.json _site/search-metadata.json
 */

lunr = require("lunr")
fs = require("fs")

if(process.argv.length !== 5) {
  console.error('Needs exactly 3 arguments (input json, output index).');
  process.exit(1);
}

documents = JSON.parse(fs.readFileSync(process.argv[2], {encoding: 'utf8'}))

var idx = lunr(function () {
  this.ref('link')
  this.field('title', {boost: 9})
  this.field('tag', {boost: 3})
  this.field('text')

  documents.forEach(function (doc) {
    const {link, title, text, tags} = doc;
    this.add({link, title, text, tag:tags.map(x => x.join(" ")).join("\n")})
  }, this)
})

fs.writeFileSync(process.argv[3], JSON.stringify(idx), {encoding: 'utf8'})
fs.writeFileSync(process.argv[4], JSON.stringify(
    Object.fromEntries(documents.map(x => [x.link, {"title": x.title, "tags": x.tags}]))
  ), {encoding: 'utf8'})