diff options
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/metadata.js | 46 |
1 files changed, 46 insertions, 0 deletions
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 +} |