From da9ff90d3e1b1348c43cd378c0806e159a700810 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 18 Jun 2017 12:24:24 +0200 Subject: feat: Parse front matter as metadata --- src/modules/metadata.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/modules/metadata.js (limited to 'src/modules') 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 +} -- cgit 1.4.1