diff options
| author | Mirek Kratochvil <exa.exa@gmail.com> | 2023-06-17 20:40:11 +0200 |
|---|---|---|
| committer | Mirek Kratochvil <exa.exa@gmail.com> | 2023-06-17 20:40:40 +0200 |
| commit | 32a49d3179a969604410ff7507af939c77045b4f (patch) | |
| tree | 70e8a0e3bacad4b7208797b45e1b0805c8d70678 /make-search-index.js | |
| parent | 0f97b7a64fa3733689b713a2210a4f7b64e069d7 (diff) | |
| download | reploy-32a49d3179a969604410ff7507af939c77045b4f.tar.gz reploy-32a49d3179a969604410ff7507af939c77045b4f.tar.bz2 | |
export search data, add indexing script
Diffstat (limited to 'make-search-index.js')
| -rw-r--r-- | make-search-index.js | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/make-search-index.js b/make-search-index.js new file mode 100644 index 0000000..bb92b68 --- /dev/null +++ b/make-search-index.js @@ -0,0 +1,39 @@ + +/* + * 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 search (currently titles) + * are stored separately in an extra file. + * + * Installing dependencies: + * yarnpkg add lunr + * + * Usage: + * site ....some args.... --search-data-output search-raw.json + * node make-search-index.js search-raw.json search-index.json search-meta.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: 10}) + this.field('text') + documents.forEach(function (doc) { + this.add(doc) + }, 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, x.title])) + ), {encoding: 'utf8'}) |
