1// Copyright (c) 2019 Baldur Karlsson
2//
3// SPDX-License-Identifier: Apache-2.0
4
5var lunr = require('lunr'),
6    stdin = process.stdin,
7    stdout = process.stdout,
8    buffer = []
9
10stdin.resume()
11stdin.setEncoding('utf8')
12
13stdin.on('data', function (data) {
14  buffer.push(data)
15})
16
17stdin.on('end', function () {
18  var documents = JSON.parse(buffer.join(''))
19
20  var idx = lunr(function () {
21    this.ref('id')
22    this.field('title')
23    this.field('body')
24
25    documents.forEach(function (doc) {
26      this.add(doc)
27    }, this)
28  })
29
30  stdout.write("var searchindex = " + JSON.stringify(idx) + ";\n")
31
32  var searchlookup = {};
33
34  for(var i=0; i < documents.length; i++) {
35    searchlookup[documents[i].id] = documents[i].title;
36  }
37
38  stdout.write("var searchlookup = " + JSON.stringify(searchlookup) + ";\n")
39})
40