summary refs log tree commit diff stats
path: root/src/responders.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/responders.js')
-rw-r--r--src/responders.js42
1 files changed, 40 insertions, 2 deletions
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;
   }
 };