about summary refs log tree commit diff stats
path: root/src/domain
diff options
context:
space:
mode:
authorAlan Pearce2017-07-02 15:30:20 +0200
committerAlan Pearce2017-07-02 15:30:20 +0200
commitb7bf162e6c3bc834097e65936704e3eac59eb4bd (patch)
tree9f080d9c67d6ee17a506e5ab52761f22b649137c /src/domain
parent2d931962b74fe06c1bfbc2454fa166d24e8e2f59 (diff)
downloadhomestead-b7bf162e6c3bc834097e65936704e3eac59eb4bd.tar.lz
homestead-b7bf162e6c3bc834097e65936704e3eac59eb4bd.tar.zst
homestead-b7bf162e6c3bc834097e65936704e3eac59eb4bd.zip
feat: Use microformats classes
Add snapshot-based tests to ensure microformats data can be extracted
correctly.
Diffstat (limited to 'src/domain')
-rw-r--r--src/domain/posts.js73
1 files changed, 37 insertions, 36 deletions
diff --git a/src/domain/posts.js b/src/domain/posts.js
index ea81729..fd4fb3d 100644
--- a/src/domain/posts.js
+++ b/src/domain/posts.js
@@ -1,81 +1,82 @@
-'use strict'
+"use strict";
 
-const fs = require('fs')
-const path = require('path')
-const matter = require('gray-matter')
-const markdown = require('../modules/markdown.js')
-const { indentForTemplate, postIndentLevel } = require('../responders.js')
+const fs = require("fs");
+const path = require("path");
+const matter = require("gray-matter");
+const markdown = require("../modules/markdown.js");
+const { indentForTemplate, postIndentLevel } = require("../responders.js");
 
 const grayMatterOptions = {
-  lang: 'toml',
-  delims: '+++'
-}
+  lang: "toml",
+  delims: "+++"
+};
 
-function* lowercaseKeys (iterator) {
+function* lowercaseKeys(iterator) {
   for (let [k, v] of iterator) {
-    yield [String(k).toLowerCase(), v]
+    yield [String(k).toLowerCase(), v];
   }
 }
 
-function canonicaliseMetadata (meta) {
+function canonicaliseMetadata(meta) {
   if (meta.data) {
-    meta.data = new Map(lowercaseKeys(Object.entries(meta.data)))
+    meta.data = new Map(lowercaseKeys(Object.entries(meta.data)));
   } else {
-    meta.data = new Map()
+    meta.data = new Map();
   }
-  return meta
+  return meta;
 }
 
-function getTitle (file) {
-  return path.basename(file.path, path.extname(file.path))
+function getTitle(file) {
+  return path.basename(file.path, path.extname(file.path));
 }
 
-function get (filename) {
-  const fileMatter = matter.read(filename, grayMatterOptions)
-  fileMatter.basename = getTitle(fileMatter)
-  delete fileMatter.orig
+function get(getURL, filename) {
+  const fileMatter = matter.read(filename, grayMatterOptions);
+  fileMatter.basename = getTitle(fileMatter);
+  delete fileMatter.orig;
   fileMatter.body = indentForTemplate(
     markdown(fileMatter.content),
     postIndentLevel
-  )
-  return canonicaliseMetadata(fileMatter)
+  );
+  fileMatter.url = getURL(fileMatter.basename);
+  return canonicaliseMetadata(fileMatter);
 }
 
-function getFolder (folder) {
+function getFolder(folder, getURL) {
   return new Map(
     fs
       .readdirSync(folder)
       .map(f => path.resolve(folder, f))
-      .map(get)
+      .map(get.bind(this, getURL))
       .map(f => [getTitle(f), f])
-  )
+  );
 }
 
-function taxonomise (taxonomies, posts) {
-  const taxons = new Map(Object.keys(taxonomies).map(t => [t, new Map()]))
+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)
+          const current = taxons.get(singularName).get(term);
           taxons
             .get(singularName)
-            .set(term, current ? current.concat(post) : [post])
+            .set(term, current ? current.concat(post) : [post]);
         }
       }
     }
   }
 
-  return taxons
+  return taxons;
 }
 
-module.exports = function (config) {
-  const posts = getFolder(config.folder)
-  const taxonomies = taxonomise(config.taxonomies, posts)
+module.exports = function(config, getURL) {
+  const posts = getFolder(config.folder, getURL);
+  const taxonomies = taxonomise(config.taxonomies, posts);
   return {
     posts,
     taxonomies,
     get
-  }
-}
+  };
+};