brainbaking/build-lunr-index.js

61 lines
1.8 KiB
JavaScript

const fs = require('fs').promises;
const { resolve } = require('path');
const {promisify} = require('util');
const frontMatterParser = require('parser-front-matter');
const parse = promisify(frontMatterParser.parse.bind(frontMatterParser));
// https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
async function getFiles(dir) {
const dirents = await fs.readdir(dir, { withFileTypes: true });
const files = await Promise.all(dirents.map((dirent) => {
const res = resolve(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res;
}));
return Array.prototype.concat(...files);
}
async function loadPostsWithFrontMatter(postsDirectoryPath) {
const postNames = await getFiles(postsDirectoryPath);
const posts = await Promise.all(
// could be .DS_Store stuff found using recursive function above...
postNames.filter(name => name.endsWith('.md')).map(async fileName => {
const fileContent = await fs.readFile(fileName, 'utf8');
const {content, data} = await parse(fileContent);
return {
content: content.slice(0, 3000),
...data
};
})
);
return posts;
}
const lunrjs = require('lunr');
function makeIndex(posts) {
return lunrjs(function() {
this.ref('title');
this.field('title');
this.field('content');
this.field('tags');
posts.forEach(p => {
this.add(p);
});
});
}
async function run() {
const posts = await loadPostsWithFrontMatter(`${__dirname}/content/post`);
const notes = await loadPostsWithFrontMatter(`${__dirname}/content/notes`);
const index = makeIndex(posts.concat(notes));
console.log(JSON.stringify(index));
}
run()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});