diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/index.js | 7 | ||||
-rw-r--r-- | src/modules/metadata.js | 46 | ||||
-rw-r--r-- | src/views/index.html | 4 |
3 files changed, 56 insertions, 1 deletions
diff --git a/src/index.js b/src/index.js index 630ac08..c16d952 100644 --- a/src/index.js +++ b/src/index.js @@ -10,10 +10,15 @@ const router = new Router() const view = require('koa-nunjucks-next') +const metadata = require('./modules/metadata.js') +const postMetadata = metadata.getFolderMetadata(process.env.POST_DIR) + app.use(view(`${__dirname}/views`)) router.get('/', async function (ctx, next) { - await ctx.render('index') + await ctx.render('index', { + posts: postMetadata + }) }) app.use(router.routes()).use(router.allowedMethods()) diff --git a/src/modules/metadata.js b/src/modules/metadata.js new file mode 100644 index 0000000..9f24d8e --- /dev/null +++ b/src/modules/metadata.js @@ -0,0 +1,46 @@ +'use strict' + +const fs = require('fs') +const path = require('path') +const matter = require('gray-matter') + +const options = { + 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 readFileMetadata (filename) { + return canonicaliseMetadata(matter.read(filename, options)) +} + +function getFileMetadata (filename) { + const result = readFileMetadata(filename) + return result && result.data +} + +function getFolderMetadata (folder) { + return fs + .readdirSync(folder) + .map(f => path.resolve(folder, f)) + .map(readFileMetadata) +} + +module.exports = { + getFileMetadata, + getFolderMetadata +} diff --git a/src/views/index.html b/src/views/index.html index 3b18e51..0e900e6 100644 --- a/src/views/index.html +++ b/src/views/index.html @@ -1 +1,5 @@ hello world + +{% for post in posts %} + {{ post.data.get('title') }} +{% endfor %} |