1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
"use strict";
const fs = require("fs");
const Case = require("case");
const hyperfast = require("hyperfast");
const indent = require("indent-string");
const getTemplate = name =>
fs.readFileSync(`${__dirname}/templates/${name}.html`, "utf8");
function getTemplateIndent(re, template) {
return re.exec(getTemplate(template))[1].length;
}
const findMain = /^(\s+)<main/m;
const baseIndentLevel = getTemplateIndent(findMain, "layout");
const findPostContent = /^(\s+)<div class="post-content/m;
const postIndentLevel =
baseIndentLevel + getTemplateIndent(findPostContent, "post");
function indentForTemplate(text, indentLevel) {
return indent(text, indentLevel).slice(indentLevel).replace(/\n+$/, "");
}
function templateReader(template, indentLevel) {
return indentForTemplate(getTemplate(template), indentLevel);
}
const templates = {
layout: templateReader("layout"),
home: templateReader("home", baseIndentLevel),
post: templateReader("post", baseIndentLevel),
taxon: templateReader("taxon", baseIndentLevel)
};
function title(siteTitle, pageTitle) {
return pageTitle ? `${pageTitle} · ${siteTitle}` : siteTitle;
}
const renderPostListItem = ctx => post => ({
a: {
href: ctx.getURL("post", post.basename),
_text: post.data.get("title")
}
});
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": {
alt: config.site.author.name,
src: config.site.author.photo
},
"body > main": pageElement.outerHTML
}).outerHTML.trim();
}
module.exports = {
baseIndentLevel,
postIndentLevel,
indentForTemplate,
home(ctx, config, posts) {
ctx.type = "html";
ctx.body = layout(
config,
null,
hyperfast(templates.home, {
".post": posts.map(renderPostListItem(ctx))
})
);
},
post(ctx, config, post) {
ctx.type = "html";
ctx.body = layout(
config,
post.data.get("title"),
hyperfast(templates.post, {
"article h1": post.data.get("title"),
"article .post-content": {
_html: post.body
}
})
);
},
taxon(ctx, config, term, value, taxonItems) {
ctx.type = "html";
ctx.body = layout(
config,
Case.title(value),
hyperfast(templates.taxon, {
".post": taxonItems.map(renderPostListItem(ctx))
})
);
}
};
|