summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorAlan Pearce2017-07-20 18:08:12 +0200
committerAlan Pearce2017-07-20 18:08:12 +0200
commit7bf25f232569aa62edf1c88e7014a9f3c1b37014 (patch)
tree7d7d40cfa295c405427cb3bc859757b1effb0e71 /src
parenta62fb1e7584ffa53a256ff8ce4d6a451ca542f20 (diff)
downloadhomestead-7bf25f232569aa62edf1c88e7014a9f3c1b37014.tar.lz
homestead-7bf25f232569aa62edf1c88e7014a9f3c1b37014.tar.zst
homestead-7bf25f232569aa62edf1c88e7014a9f3c1b37014.zip
feat: Create feed handler for root
Diffstat (limited to 'src')
-rw-r--r--src/app.js13
-rw-r--r--src/domain/posts.js2
-rw-r--r--src/responders.js42
-rw-r--r--src/templates/feed.xml24
4 files changed, 79 insertions, 2 deletions
diff --git a/src/app.js b/src/app.js
index 912dda2..a323b9c 100644
--- a/src/app.js
+++ b/src/app.js
@@ -13,6 +13,13 @@ const config = require("./modules/config.js");
 const Router = require("koa-router");
 const router = new Router();
 
+const makeTagURI = (authority, startDate) => specific =>
+  `tag:${authority},${startDate}:${specific}`;
+
+app.context.makeTagURI = makeTagURI(
+  config.feed.originalDomainName,
+  config.feed.domainStartDate
+);
 app.context.getURL = router.url.bind(router);
 
 module.exports = async function() {
@@ -35,6 +42,12 @@ module.exports = async function() {
   );
 
   router.get(
+    "feed",
+    "/index.xml",
+    actions.posts(config, responders.feed, Posts.posts)
+  );
+
+  router.get(
     "post",
     "/post/:filename",
     actions.post(config, responders.post, Posts.posts)
diff --git a/src/domain/posts.js b/src/domain/posts.js
index 98488ba..ab35518 100644
--- a/src/domain/posts.js
+++ b/src/domain/posts.js
@@ -86,7 +86,9 @@ function taxonomise(taxonomies, posts) {
 module.exports = async function(config, getURL) {
   const posts = await getFolder(config.folder, getURL);
   const taxonomies = taxonomise(config.taxonomies, posts);
+  const lastPostDate = Math.max(Array.from(posts.values(), p => p.date));
   return {
+    lastPostDate,
     posts,
     taxonomies,
     get
diff --git a/src/responders.js b/src/responders.js
index 8f084a3..f4ea0a6 100644
--- a/src/responders.js
+++ b/src/responders.js
@@ -1,6 +1,7 @@
 "use strict";
 
 const fs = require("fs");
+const URL = require("url").URL;
 const Case = require("case");
 const hyperfast = require("hyperfast");
 const indent = require("indent-string");
@@ -25,7 +26,7 @@ const findPostContent = /^(\s+)<div class="e-content/m;
 const postIndentLevel =
   baseIndentLevel + getTemplateIndent(findPostContent, "post.html");
 
-function indentForTemplate(text, indentLevel) {
+function indentForTemplate(text, indentLevel = 0) {
   return indent(text, indentLevel).slice(indentLevel).replace(/\n+$/, "");
 }
 
@@ -37,7 +38,8 @@ const templates = {
   layout: templateReader("layout.html"),
   home: templateReader("home.html", baseIndentLevel),
   post: templateReader("post.html", baseIndentLevel),
-  list: templateReader("list.html", baseIndentLevel)
+  list: templateReader("list.html", baseIndentLevel),
+  feed: templateReader("feed.xml")
 };
 
 function title(siteTitle, pageTitle) {
@@ -62,6 +64,22 @@ const renderPost = ctx => post => {
   };
 };
 
+const renderPostAtom = (ctx, config) => post => {
+  return {
+    id: ctx.makeTagURI(`post:${post.basename}`),
+    title: post.data.get("title"),
+    updated: post.data.get("date"),
+    summary: post.data.get("summary"),
+    "link[rel=alternate]": {
+      href: new URL(ctx.getURL("post", post.basename), config.site.baseURL)
+    },
+    "content > div": {
+      _html: post.body
+    },
+    "author > name": config.author.name
+  };
+};
+
 function layout(config, pageTitle, pageElement) {
   return hyperfast(templates.layout, {
     title: title(config.author.name, pageTitle),
@@ -125,5 +143,25 @@ module.exports = {
       post.data.get("title"),
       hyperfast(templates.post, renderPost(ctx)(post))
     );
+  },
+
+  feed(ctx, config, { posts, lastPostDate }) {
+    ctx.type = "atom";
+
+    ctx.body = hyperfast(
+      templates.feed,
+      {
+        "feed > title": config.author.name,
+        "feed > link": {
+          href: config.site.baseURL
+        },
+        "feed > id": {
+          _text: ctx.makeTagURI("feed")
+        },
+        "feed > updated": lastPostDate,
+        "feed > entry": posts.map(renderPostAtom(ctx, config))
+      },
+      { xmlMode: true }
+    ).outerHTML;
   }
 };
diff --git a/src/templates/feed.xml b/src/templates/feed.xml
new file mode 100644
index 0000000..03d68d7
--- /dev/null
+++ b/src/templates/feed.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+  <title>Example Feed</title>
+  <link href="http://example.org/"/>
+  <id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id>
+  <updated>2003-12-13T18:30:02Z</updated>
+
+  <entry>
+    <title>Atom-Powered Robots Run Amok</title>
+    <link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/>
+    <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
+    <updated>2003-12-13T18:30:02Z</updated>
+    <summary>Some text.</summary>
+    <content type="xhtml">
+      <div xmlns="http://www.w3.org/1999/xhtml">
+        <p>This is the entry content.</p>
+      </div>
+    </content>
+    <author>
+      <name>John Doe</name> 
+    </author>
+  </entry>
+
+</feed>