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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
"use strict";
const fs = require("fs");
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+)<main/m;
const baseIndentLevel = getTemplateIndent(findMain, "layout.html");
const findPostContent = /^(\s+)<div class="e-content/m;
const postIndentLevel =
baseIndentLevel + getTemplateIndent(findPostContent, "post.html");
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.html"),
home: templateReader("home.html", baseIndentLevel),
post: templateReader("post.html", baseIndentLevel),
list: templateReader("list.html", baseIndentLevel)
};
function title(siteTitle, pageTitle) {
return pageTitle ? `${pageTitle} · ${siteTitle}` : siteTitle;
}
const makeTime = date => ({
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
}
};
};
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))
})
);
},
post(ctx, config, post) {
ctx.type = "html";
ctx.body = layout(
config,
post.data.get("title"),
hyperfast(templates.post, renderPost(ctx)(post))
);
}
};
|