"use strict"; const fs = require("fs"); const URL = require("url").URL; const Case = require("case"); const hyperfast = require("hyperfast"); const indent = require("indent-string"); const postDateFormatter = new Intl.DateTimeFormat("en-GB", { hour12: false, weekday: "long", year: "numeric", month: "long", day: "numeric" }); const getTemplate = name => fs.readFileSync(`${__dirname}/templates/${name}`, "utf8"); function getTemplateIndent(re, template) { return re.exec(getTemplate(template))[1].length; } const findMain = /^(\s+)
({ datetime: date.toISOString(), _text: postDateFormatter.format(date) }); const renderPost = ctx => post => { return { ".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 } }; }; 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), ".h-card .p-name": config.author.name, ".h-card .p-note": config.site.description, ".h-card .u-photo": { alt: config.author.name, src: config.author.photo }, "header nav li": config.site.nav.map(l => ({ a: { href: l.url, _text: l.text } })), "body > main": pageElement.outerHTML, ".contact-list li": config.author.contact.map(c => ({ a: { class: c.url.startsWith("mailto") ? "u-email" : "u-url", href: c.url, _text: c.text } })) }).outerHTML.trim(); } module.exports = { baseIndentLevel, postIndentLevel, indentForTemplate, home(ctx, config, { posts }) { ctx.type = "html"; ctx.body = layout( config, null, hyperfast(templates.home, { ".h-entry": posts.map(renderPost(ctx)) }) ); }, list(ctx, config, { listType, listName, posts }) { ctx.type = "html"; ctx.body = layout( config, Case.title(listName), hyperfast(templates.list, { ".h-entry": posts.map(renderPost(ctx)) }) ); }, tags(ctx, config, { tags }) { ctx.type = "html"; ctx.body = layout( config, "Tags", hyperfast(templates.tags, { ".tag": Array.from(tags).map(tag => ({ ".u-url": { href: ctx.getURL("tag", tag), _text: Case.title(tag) } })) }) ); }, post(ctx, config, { post }) { ctx.type = "html"; ctx.body = layout( config, 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; } };