src/modules/posts.js (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 'use strict' const fs = require('fs') const path = require('path') const matter = require('gray-matter') const grayMatterOptions = { lang: 'toml', delims: '+++' } function* lowercaseKeys (iterator) { for (let [k, v] of iterator) { yield [String(k).toLowerCase(), v] } } function canonicaliseMetadata (meta) { if (meta.data) { meta.data = new Map(lowercaseKeys(Object.entries(meta.data))) } else { meta.data = new Map() } return meta } function get (filename) { return canonicaliseMetadata(matter.read(filename, grayMatterOptions)) } function getTitle (file) { return path.basename(file.path, path.extname(file.path)) } function getFolder (folder) { return new Map( fs .readdirSync(folder) .map(f => path.resolve(folder, f)) .map(get) .map(f => [getTitle(f), f]) ) } module.exports = { get, getFolder } |