summary refs log tree commit diff stats
path: root/src
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
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')
-rw-r--r--src/app.js36
-rw-r--r--src/domain/posts.js73
-rw-r--r--src/responders.js31
-rw-r--r--src/templates/home.html8
-rw-r--r--src/templates/post.html8
-rw-r--r--src/templates/taxon.html8
6 files changed, 82 insertions, 82 deletions
diff --git a/src/app.js b/src/app.js
index fa0d5c2..1a0c5cb 100644
--- a/src/app.js
+++ b/src/app.js
@@ -1,31 +1,33 @@
-'use strict'
+"use strict";
 
-const Koa = require('koa')
-const app = new Koa()
+const Koa = require("koa");
+const app = new Koa();
 
-const helmet = require('koa-helmet')
+const helmet = require("koa-helmet");
 
-const actions = require('./actions.js')
+const actions = require("./actions.js");
 
-const config = require('./modules/config.js')
+const config = require("./modules/config.js");
 
-const Router = require('koa-router')
-const router = new Router()
+const Router = require("koa-router");
+const router = new Router();
 
-app.context.getURL = router.url.bind(router)
+app.context.getURL = router.url.bind(router);
 
-const Posts = require('./domain/posts.js')(config.posts)
+const Posts = require("./domain/posts.js")(config.posts, basename =>
+  router.url("post", basename)
+);
 
-router.get('home', '/', actions.home(config, Posts.posts))
+router.get("home", "/", actions.home(config, Posts.posts));
 
-router.get('post', '/post/:filename', actions.post(config, Posts.posts))
+router.get("post", "/post/:filename", actions.post(config, Posts.posts));
 
 for (let [term, items] of Posts.taxonomies) {
   router.get(
     `taxon-${term}`,
     `/${term}/:value`,
     actions.taxonGenerator(config, term, items)
-  )
+  );
 }
 
 app.use(
@@ -34,10 +36,10 @@ app.use(
       setIf: ctx => ctx.secure
     }
   })
-)
+);
 
-app.use(router.routes()).use(router.allowedMethods())
+app.use(router.routes()).use(router.allowedMethods());
 
-app.use(actions.serveFiles)
+app.use(actions.serveFiles);
 
-module.exports = app
+module.exports = app;
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
-  }
-}
+  };
+};
diff --git a/src/responders.js b/src/responders.js
index 4049ebf..867310a 100644
--- a/src/responders.js
+++ b/src/responders.js
@@ -21,7 +21,7 @@ function getTemplateIndent(re, template) {
 }
 const findMain = /^(\s+)<main/m;
 const baseIndentLevel = getTemplateIndent(findMain, "layout");
-const findPostContent = /^(\s+)<div class="post-content/m;
+const findPostContent = /^(\s+)<div class="e-content/m;
 const postIndentLevel =
   baseIndentLevel + getTemplateIndent(findPostContent, "post");
 
@@ -49,12 +49,15 @@ const makeTime = date => ({
   _text: postDateFormatter.format(date)
 });
 
-const renderPostListItem = ctx => post => {
+const renderPost = ctx => post => {
   return {
-    time: makeTime(post.data.get("date")),
-    a: {
-      href: ctx.getURL("post", post.basename),
-      _text: post.data.get("title")
+    ".dt-published": makeTime(post.data.get("date")),
+    ".p-name": post.data.get("title"),
+    ".u-url": {
+      href: ctx.getURL("post", post.basename)
+    },
+    ".e-content": {
+      _html: post.body
     }
   };
 };
@@ -62,8 +65,8 @@ const renderPostListItem = ctx => post => {
 function layout(config, pageTitle, pageElement) {
   return hyperfast(templates.layout, {
     title: title(config.site.author.name, pageTitle),
-    "body > header .p-name": config.site.author.name,
-    "body > header .u-photo": {
+    ".h-card .p-name": config.site.author.name,
+    ".h-card .u-photo": {
       alt: config.site.author.name,
       src: config.site.author.photo
     },
@@ -83,7 +86,7 @@ module.exports = {
       config,
       null,
       hyperfast(templates.home, {
-        ".post": posts.map(renderPostListItem(ctx))
+        ".h-entry": posts.map(renderPost(ctx))
       })
     );
   },
@@ -94,13 +97,7 @@ module.exports = {
     ctx.body = layout(
       config,
       post.data.get("title"),
-      hyperfast(templates.post, {
-        "article h1": post.data.get("title"),
-        "article time": makeTime(post.data.get("date")),
-        "article .post-content": {
-          _html: post.body
-        }
-      })
+      hyperfast(templates.post, renderPost(ctx)(post))
     );
   },
 
@@ -111,7 +108,7 @@ module.exports = {
       config,
       Case.title(value),
       hyperfast(templates.taxon, {
-        ".post": taxonItems.map(renderPostListItem(ctx))
+        ".h-entry": taxonItems.map(renderPost(ctx))
       })
     );
   }
diff --git a/src/templates/home.html b/src/templates/home.html
index a3597a1..44244e5 100644
--- a/src/templates/home.html
+++ b/src/templates/home.html
@@ -1,6 +1,6 @@
-<ul class="posts">
-  <li class="post">
-    <a href="/">Test post please ignore</a>
-    <time></time>
+<ul class="h-feed">
+  <li class="h-entry">
+    <a class="u-url p-name" href="/">Test post please ignore</a>
+    <time class="dt-published"></time>
   </li>
 </ul>
diff --git a/src/templates/post.html b/src/templates/post.html
index 27bcdc3..a047aa4 100644
--- a/src/templates/post.html
+++ b/src/templates/post.html
@@ -1,7 +1,7 @@
-<article>
-  <h1>post title</h1>
-  <time></time>
-  <div class="post-content">
+<article class="h-entry">
+  <h1 class="p-name">post title</h1>
+  <time class="dt-published"></time>
+  <div class="e-content">
     Fringilla ut morbi tincidunt augue interdum velit euismod!
     Interdum velit laoreet id donec ultrices tincidunt arcu, non
     sodales neque sodales ut etiam sit amet nisl purus, in
diff --git a/src/templates/taxon.html b/src/templates/taxon.html
index a3597a1..44244e5 100644
--- a/src/templates/taxon.html
+++ b/src/templates/taxon.html
@@ -1,6 +1,6 @@
-<ul class="posts">
-  <li class="post">
-    <a href="/">Test post please ignore</a>
-    <time></time>
+<ul class="h-feed">
+  <li class="h-entry">
+    <a class="u-url p-name" href="/">Test post please ignore</a>
+    <time class="dt-published"></time>
   </li>
 </ul>