Skip to content

Instantly share code, notes, and snippets.

@julientaq
Last active July 1, 2021 23:16
Show Gist options
  • Save julientaq/e8f5990102a98864bdab88050c418066 to your computer and use it in GitHub Desktop.
Save julientaq/e8f5990102a98864bdab88050c418066 to your computer and use it in GitHub Desktop.
creating an index with json
{{ collections.allSearch | searchSingle(folder) | dump | safe }}
const elasticlunr = require("elasticlunr");
module.exports = function (folder) {
// folder gets out undefined
console.log(folder);
// what fields we'd like our index to consist of
var index = elasticlunr(function () {
this.addField("title");
this.addField("tags");
this.addField("chapnum");
this.addField("content");
this.setRef("id");
});
// loop through each page and add it to the index
collection.forEach((item) => {
console.log(`folder`, folder);
console.log(`item.data.folder`, item.data.folder)
if(item.data.folder == folder) {
index.addDoc({
id: item.url,
content: item.templateContent,
chapnum: item.data.order,
title: item.data.title,
tags: item.data.tags
});
}
});
return index.toJSON();
};
---
folder: modernworldhistory
permalink: "/books/{{folder}}/content.json"
title: "search in the book"
menu: false
layout: searchindex.njk
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment