diff options
author | Alan Pearce | 2017-06-24 22:05:18 +0200 |
---|---|---|
committer | Alan Pearce | 2017-06-24 22:05:18 +0200 |
commit | dd819c45b58088f9a98384ab237268d865836fe0 (patch) | |
tree | abf2c2847571041454a9119dff7ccfc6b7d0f854 /src/modules | |
parent | bdd6610208e9fe5971e6aed4378598092f2b9b5a (diff) | |
download | homestead-dd819c45b58088f9a98384ab237268d865836fe0.tar.lz homestead-dd819c45b58088f9a98384ab237268d865836fe0.tar.zst homestead-dd819c45b58088f9a98384ab237268d865836fe0.zip |
refactor: re-architect to be closer to ADR
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/markdown.js | 12 | ||||
-rw-r--r-- | src/modules/posts.js | 88 |
2 files changed, 12 insertions, 88 deletions
diff --git a/src/modules/markdown.js b/src/modules/markdown.js new file mode 100644 index 0000000..9f0af45 --- /dev/null +++ b/src/modules/markdown.js @@ -0,0 +1,12 @@ +'use strict' + +const Markdown = require('markdown-it') + +const markdownOptions = { + html: true, + typographer: true +} + +const markdown = new Markdown(markdownOptions) + +module.exports = markdown.render.bind(markdown) diff --git a/src/modules/posts.js b/src/modules/posts.js deleted file mode 100644 index cf2d80e..0000000 --- a/src/modules/posts.js +++ /dev/null @@ -1,88 +0,0 @@ -'use strict' - -const fs = require('fs') -const path = require('path') -const matter = require('gray-matter') -const Markdown = require('markdown-it') - -const grayMatterOptions = { - lang: 'toml', - delims: '+++' -} - -const markdownOptions = { - html: true, - typographer: true -} - -const markdown = new Markdown(markdownOptions) - -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 getTitle (file) { - return path.basename(file.path, path.extname(file.path)) -} - -function render (post) { - return markdown.render(post.content) -} - -function get (filename) { - const fileMatter = matter.read(filename, grayMatterOptions) - fileMatter.basename = getTitle(fileMatter) - return canonicaliseMetadata(fileMatter) -} - -function getFolder (folder) { - return new Map( - fs - .readdirSync(folder) - .map(f => path.resolve(folder, f)) - .map(get) - .map(f => [getTitle(f), f]) - ) -} - -function taxonomise (taxonomies, posts) { - const taxons = new Map(Object.keys(taxonomies).map(t => [t, new Map()])) - - for (let [, post] of posts) { - for (let [singularName, pluralName] of Object.entries(taxonomies)) { - if (post.data.has(pluralName)) { - for (let term of post.data.get(pluralName)) { - const current = taxons.get(singularName).get(term) - taxons - .get(singularName) - .set(term, current ? current.concat(post) : [post]) - } - } - } - } - - return taxons -} - -function toTags (posts) { - return taxonomise({ tag: 'tags' }, posts).get('tag') -} - -module.exports = { - get, - getFolder, - toTags, - taxonomise, - render -} |