From 7bf25f232569aa62edf1c88e7014a9f3c1b37014 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Thu, 20 Jul 2017 18:08:12 +0200 Subject: feat: Create feed handler for root --- src/responders.js | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'src/responders.js') 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+)
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; } }; -- cgit 1.4.1