2020-06-10 15:51:32 +02:00
|
|
|
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`);
|
2020-06-16 16:13:58 +02:00
|
|
|
const index = makeIndex(posts);
|
2020-06-10 15:51:32 +02:00
|
|
|
console.log(JSON.stringify(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
run()
|
|
|
|
.then(() => process.exit(0))
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
|
|
|
});
|