diff options
| author | Mirek Kratochvil <miroslav.kratochvil@uni.lu> | 2023-06-22 11:17:44 +0200 |
|---|---|---|
| committer | Mirek Kratochvil <miroslav.kratochvil@uni.lu> | 2023-06-22 11:43:28 +0200 |
| commit | 65944a1cdfe3c06df737e5c54aec43292d0180bf (patch) | |
| tree | 05175d89f4781c5627ac1152d5b5668f0ff9970d /scripts/make-search-index.js | |
| parent | c86ece1433da3e8a85d84233f236a7d3b7945534 (diff) | |
| download | reploy-65944a1cdfe3c06df737e5c54aec43292d0180bf.tar.gz reploy-65944a1cdfe3c06df737e5c54aec43292d0180bf.tar.bz2 | |
make a separate scripts directory (this gonna grow)
Diffstat (limited to 'scripts/make-search-index.js')
| -rw-r--r-- | scripts/make-search-index.js | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/scripts/make-search-index.js b/scripts/make-search-index.js new file mode 100644 index 0000000..37c5ce5 --- /dev/null +++ b/scripts/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 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: 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, {"title": x.title, "tags": x.tags}])) + ), {encoding: 'utf8'}) |
