diff options
author | Alan Pearce | 2017-06-18 12:24:24 +0200 |
---|---|---|
committer | Alan Pearce | 2017-06-18 12:24:24 +0200 |
commit | da9ff90d3e1b1348c43cd378c0806e159a700810 (patch) | |
tree | ab81bc58d982c6ffe77e160ae9b107acdc72eee6 /src/modules | |
parent | 3bda5fcfb7a23619ba66bef029d89bbd18d2c915 (diff) | |
download | homestead-da9ff90d3e1b1348c43cd378c0806e159a700810.tar.lz homestead-da9ff90d3e1b1348c43cd378c0806e159a700810.tar.zst homestead-da9ff90d3e1b1348c43cd378c0806e159a700810.zip |
feat: Parse front matter as metadata
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 +} |