-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
152 lines (125 loc) · 4.17 KB
/
index.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import fs from 'fs/promises';
import path from 'path';
import express from 'express';
import he from 'he';
import mustache from 'mustache';
import MarkdownIt from 'markdown-it';
import toml from 'toml';
import * as Content from './content';
type MustacheLambdaFunction = (input: string) => string;
const markdown = new MarkdownIt({
html: true,
linkify: true,
});
async function readAllContent(): Promise<Content.ContentBase[]> {
const contentDirectory = path.join(process.cwd(), 'content');
const contentFileNames = await fs.readdir(contentDirectory);
const allContent: Content.ContentBase[] = [];
for (const contentFileName of contentFileNames) {
const contentFileBuffer = await fs.readFile(
path.join(contentDirectory, contentFileName)
);
const parsedContent = toml.parse(contentFileBuffer.toString('utf8'));
if (
!Content.getEnumKeys(Content.ContentBaseRequiredKeys).every(
(key) => !!parsedContent[key]
)
) {
throw new Error(
`${contentFileName} is missing a one of the required keys (${Object.keys(
Content.ContentBaseRequiredKeys
).join(', ')})`
);
}
allContent.push(parsedContent as Content.ContentBase);
}
return allContent;
}
async function renderTemplate(
content: Content.ContentWithTemplate | Content.ContentForIndex
): Promise<string> {
const templateBuffer = await fs.readFile(
path.join(process.cwd(), 'templates', `${content.template}.html`)
);
const template = templateBuffer.toString('utf8');
function mediaLambda() {
function _mediaLambda(mediaFile: string, render: MustacheLambdaFunction) {
return `https://cdn.joekent.nyc/${render(mediaFile)}`;
}
return _mediaLambda;
}
function markdownLambda() {
function _markdownLambda(markup: string, render: MustacheLambdaFunction) {
// TOML has dumb character escape rules.
// And mustache doesn't run lambda's on replace variable values.
// This escapes the HTML entities from the TOML multi-line string,
// and also fixes mustache running Lambdas against nested variable values.
return markdown.render(render(he.decode(render(he.decode(markup)))), {
html: true,
linkify: true,
});
}
return _markdownLambda;
}
const templateFunctions = {
markdown: markdownLambda,
media: mediaLambda,
};
const templateData =
typeof content.templateData === 'string' ? {} : content.templateData;
const renderedTemplate = mustache.render(template, {
...content,
...templateData,
...templateFunctions,
});
return renderedTemplate;
}
function trimLastSlash(path: string) {
return path === '/'
? path
: path.endsWith('/')
? path.substring(0, path.length - 1)
: path;
}
(async () => {
if (process.env.NODE_ENV === 'development') {
const app = express();
app.use('/dist', express.static('www/dist'));
app.get('/*', async (request, response) => {
try {
const content = await readAllContent();
const match: Content.ContentBase | undefined = content.find(
(compare) => compare.path === trimLastSlash(request.path)
);
if (!match) {
return response.status(404).send('not found');
}
const templateHtml = await renderTemplate(match);
const html = await renderTemplate({
...match,
template: 'index',
templateHtml,
});
response.set('content-type', 'text/html').send(html);
} catch (error) {
console.error(error);
response.status(500).send('server error');
}
});
app.listen(5000, () => console.log(`Listening on port 5000`));
} else {
console.log('generating pages...');
const allContent = await readAllContent();
for (const content of allContent) {
const templateHtml = await renderTemplate(content);
const html = await renderTemplate({
...content,
template: 'index',
templateHtml,
});
const indexFolderPath = path.join(process.cwd(), 'www', content.path);
await fs.mkdir(indexFolderPath, { recursive: true });
await fs.writeFile(path.join(indexFolderPath, 'index.html'), html);
}
}
})();