-
Notifications
You must be signed in to change notification settings - Fork 4
/
templates.ts
403 lines (346 loc) · 11.9 KB
/
templates.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import type {
GoldsmithFile,
GoldsmithLiteralHTMLLayoutContext,
GoldsmithLiteralHTMLLayoutCallback,
GoldsmithLiteralHTMLLayoutMap,
} from "./deps.ts";
import { html } from "https://deno.land/x/[email protected]/mod.ts";
import { hexToRGB, rgbToHSL, hslToRGB, rgbToHex } from "./colorsmith.ts";
// CSS template
export interface GenerateCSSOptions {
title?: string;
heading?: string;
link?: string;
comment?: string;
}
const cssTemplate = `:root { color-scheme: dark; }
html, body { margin: 0; }
img {
max-width: 100%;
object-fit: contain;
}
body {
display: flex;
flex-direction: column;
align-items: center;
font-family: sans-serif;
overflow-y: scroll;
}
body > header, main { width: 40em; }
body > header, main { padding: 0.5em; }
@media screen and (max-width: 40em) {
body > header, main { width: calc(100% - 1em); }
}
body { line-height: 1.5; }
pre code { line-height: 1.3; }
h1, h2, h3, h4, h5 { line-height: 1.2; }
td { line-height: 1.2; }
body > header { text-align: center; }
body > header > h1 { margin-bottom: 0; }
body > header > p { margin: 0.3em 0; }
nav { padding: 0.3em 0; }
nav > ul { display: inline; padding: 0; }
nav > ul > li { display: inline; }
nav > ul > li + li:before { content: " | "; }
body > header > h1 > a {
font-size: 1.75rem;
text-decoration: inherit;
}
article > header > h1 { margin-bottom: 0.25em; }
article > header > p { margin-top: 0em; }
main { overflow: auto; }
pre { overflow: auto; }
code { font-size: 1rem; }
pre code { font-size: 0.8125rem; }
th, td { padding: 0.25em; }
pre { border: solid 1px; padding: 0em 0.25em; }
table, th, tr, td { border-collapse: collapse; border: solid 1px; }
main > ul { padding-left: 0em; }
main > ul > li { list-style: none; margin-bottom: 2em; }
ul { padding-left: 1.5em; }
li { margin-bottom: 0.5em; }
h1 { font-size: 1.6rem; font-weight: bold; }
h2 { font-size: 1.3rem; font-weight: bold; }
h3 { font-size: 1.2rem; font-weight: normal; }
h4 { font-size: 1rem; font-weight: bold; }
h5 { font-size: 1rem; font-weight: normal; }
:is(h1, h2, h3, h4, h5) :is(a:link, a:visited) { color: inherit; }
body {
background-color: @background;
color: @textDefault;
}
body > main { border-bottom: 1px solid @backgroundLightest; }
th { background-color: @backgroundLightest; color: @textLight; }
pre, table, th, tr, td { border-color: @border; }
pre { background-color: @backgroundLighter; }
tr:nth-child(even) { background-color: @backgroundLight; }
code {
background-color: @backgroundEvenLighter;
border-radius: 0.2em;
padding: 0em 0.1em;
}
pre code {
background-color: revert;
border-radius: revert;
padding: revert;
}
body > header > h1 { color: @textTitle; }
nav.site { font-weight: bold; }
nav > strong { font-weight: bold; color: @textHeading; }
h1, h2, h3, h4, h5 { color: @textHeading; }
a:link { color: @textLink; }
a:visited { color: @textLinkVisited; }
/* Also b5cea8 or 7dce52 or 7bbf56 */
/* Syntax highlighting */
.hljs-comment { color: @textCommentDark; }
.hljs-tag,
.hljs-punctuation { color: @textDark; }
.hljs-literal { color: @textLinkVisited; }
.hljs-title.class_,
.hljs-tag .hljs-name,
.hljs-tag .hljs-attr { color: @textComment; }
.hljs-attr,
.hljs-symbol,
.hljs-variable,
.hljs-template-variable,
.hljs-link,
.hljs-selector-attr,
.hljs-selector-pseudo { color: @textLink; }
.hljs-keyword,
.hljs-attribute,
.hljs-selector-tag,
.hljs-meta .hljs-keyword,
.hljs-doctag,
.hljs-name { color: @textLinkVisited; }
.hljs-type,
.hljs-string,
.hljs-number,
.hljs-quote,
.hljs-template-tag,
.hljs-deletion,
.hljs-title,
.hljs-section,
.hljs-meta { color: @textHeading; }
.hljs-regexp,
.hljs-meta .hljs-string { color: @textHeadingDark; }
.hljs-title.function_,
.hljs-built_in,
.hljs-bullet,
.hljs-code,
.hljs-addition,
.hljs-selector-id,
.hljs-selector-class { color: @textTitle; }
`;
export function generateCSS(options: GenerateCSSOptions): string {
let css = cssTemplate;
// Base colors
const textTitle = options.title ?? "#e6b95c";
const textHeading = options.heading ?? "#d97c57";
const textLink = options.link ?? "#59c5ff";
const textComment = options.comment ?? "#7bbf56";
const textDefault = "#c8c8c8";
const background = "#181818";
let colors: { [name: string]: string } = {
textTitle,
textHeading,
textLink,
textComment,
textDefault,
background,
};
// Validate format of each color
for (const value of Object.values(colors)) {
hexToRGB(value);
}
// Derived colors
const desaturateStep = -0.15;
const darkenStep = -0.05;
const lightenStep = 0.04;
function adjust(hex: string, h?: number, s?: number, l?: number): string {
const hsl = rgbToHSL(hexToRGB(hex));
if (h) {
hsl.h = (hsl.h + h) % 360;
}
if (s) {
hsl.s = Math.min(1, Math.max(0, hsl.s + s));
}
if (l) {
hsl.l = Math.min(1, Math.max(0, hsl.l + l));
}
return rgbToHex(hslToRGB(hsl));
}
colors = {
...colors,
textDark: adjust(textDefault, 0, 0, -0.15),
textLight: adjust(textDefault, 0, 0, lightenStep * 2),
textHeadingDark: adjust(textHeading, 0, desaturateStep, darkenStep),
textLinkVisited: adjust(textLink, 0, desaturateStep, darkenStep),
textCommentDark: adjust(textComment, 0, desaturateStep, darkenStep),
backgroundLight: adjust(background, 0, 0, lightenStep),
backgroundLighter: adjust(background, 0, 0, lightenStep * 2),
backgroundEvenLighter: adjust(background, 0, 0, lightenStep * 3),
backgroundLightest: adjust(background, 0, 0, lightenStep * 5),
border: adjust(background, 0, 0, lightenStep * 7),
}
// Find and replace
for (const colorName of Object.keys(colors)) {
css = css.replaceAll(`@${colorName};`, `${colors[colorName]};`);
}
return css;
}
// HTML templates
interface PartialBaseOptions {
navigationVerbatim?: string;
headVerbatim?: string;
}
function isRelativeLink(href: string): boolean {
return !(href.startsWith("/") || href.includes(":"));
}
const partialBase = (m: GoldsmithLiteralHTMLLayoutContext, mainVerbatim: string, o?: PartialBaseOptions) =>
html`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>${m.title ?? m.site!.title!}</title>
${{verbatim: m.description ? html`<meta name="description" content="${m.description}" />` : ""}}
${{verbatim: m.keywords ? html`<meta name="keywords" content="${m.keywords.join(",")}" />` : ""}}
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<link rel="stylesheet" href="${m.pathToRoot!}css/style.css" />
${{verbatim: m.isRoot ? html`<link rel="alternate" type="application/rss+xml" href="${m.pathToRoot!}feed.xml" />` : ""}}
${{verbatim: o?.headVerbatim ?? ""}}
</head>
<body>
<header>
<h1><a href="${m.pathToRoot!}index.html">${m.site!.title!}</a></h1>
${{verbatim: m.site?.header?.text ? html`<p>${m.site.header.text}</p>` : ""}}
${{verbatim: (m.site?.header?.links && Object.keys(m.site.header.links).length > 0) ? html`<nav class="site"><ul>
${{verbatim: Object.entries(m.site.header.links).map(([name, link]) => html`<li><a href="${isRelativeLink(link) ? (m.pathToRoot! + link) : link}">${name}</a></li>`).join("\n")}}
</ul></nav>` : ""}}
${{verbatim: o?.navigationVerbatim ?? ""}}
</header>
<main>
${{verbatim: mainVerbatim}}
</main>
${{verbatim: m.site?.footer ? html`<footer>
${{verbatim: m.site.footer.text ? html`<p>${m.site.footer.text}</p>` : ""}}
</footer>` : ""}}
</body>
</html>
`;
interface PartialNavigationOptions {
incomplete?: boolean;
isTagIndex?: boolean;
tag?: string;
}
function partialNavigation(m: GoldsmithLiteralHTMLLayoutContext, tags: string[], o?: PartialNavigationOptions): string {
return (tags && tags.length > 0) ? html`<nav>
<strong>Topics: </strong>
<ul>
${{verbatim: tags.map(t => (o?.isTagIndex && t === o.tag) ? html`<li>${o.tag}</li>` : html`<li><a href="${m.pathToRoot ?? ""}posts/${t}/index.html">${t}</a></li>`).join("\n")}}
${{verbatim: o?.incomplete ? html`<li><a href="${m.pathToRoot!}posts/index.html">…</a></li>\n` : ""}}</ul>
</nav>` : "";
}
const dateFormatter = new Intl.DateTimeFormat("en-US", { month: "long", day: "numeric", year: "numeric", timeZone: "UTC" });
const formatDateShort = (date: Date) => date.toISOString().replace(/T.*$/, "");
const formatDate = (date: Date) => dateFormatter.format(date);
function partialDate(date: Date): string {
return html`<p><time datetime="${formatDateShort(date)}">${formatDate(date)}</time></p>`;
}
const partialArticleSummary: (m: GoldsmithLiteralHTMLLayoutContext, post: GoldsmithFile) => string = (m, post) => {
return html`<article>
<header>
<h1><a href="${m.pathToRoot!}${post.pathFromRoot!}">${post.title!}</a></h1>
${{verbatim: partialDate(post.date!)}}
</header>
${{verbatim: post.description ? html`<p>${post.description}</p>` : ""}}
</article>
`;
};
function partialArticleSummaryList(m: GoldsmithLiteralHTMLLayoutContext, posts: GoldsmithFile[]): string {
return html`<ul>
${{verbatim: posts.map((post: GoldsmithFile) => html`<li>${{verbatim: partialArticleSummary(m, post)}}</li>`).join("\n")}}
</ul>`;
}
const template404: GoldsmithLiteralHTMLLayoutCallback = (_content, m) => partialBase(m,
`<h1>Not found</h1>
<p>The requested page does not exist.</p>
<p><a href="index.html">Click here</a> to go to the home page.</p>
`);
const templateArchive: GoldsmithLiteralHTMLLayoutCallback = (_content, m) => partialBase(
{
title: `${m.site!.title!}: Archive of all posts since the beginning of time`,
...m
},
partialArticleSummaryList(m, m.collections!.posts!),
{ navigationVerbatim: partialNavigation(m, m.tagsAll!) }
);
const templateDefault: GoldsmithLiteralHTMLLayoutCallback = (content, m) => partialBase(
m,
html`<article>
${{verbatim: content}}
</article>`,
{ navigationVerbatim: partialNavigation(m, m.tags!) }
);
const templatePost: GoldsmithLiteralHTMLLayoutCallback = (content, m) => partialBase(
m,
html`<article>
<header>
<h1><a href="${m.pathToRoot!}${m.pathFromRoot!}">${m.title!}</a></h1>
${{verbatim: partialDate(m.date!)}}
</header>
${{verbatim: content}}
<footer>
<p>↵ <a href="${m.pathToRoot!}index.html">Back to home</a></p>
</footer>
</article>`,
{
navigationVerbatim: partialNavigation(m, m.tags!),
headVerbatim: html`<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BlogPosting",
"headline": "${{scriptString: m.title!}}",
${{verbatim: m.description ? html` "abstract": "${{scriptString: m.description}}",` : ""}}
${{verbatim: m.tags ? html` "keywords": "${{scriptString: m.tags.join(",")}}",` : ""}}
"datePublished": "${{scriptString: formatDateShort(m.date!)}}"
}
</script>`
}
);
const templateRoot: GoldsmithLiteralHTMLLayoutCallback = (_content, m) => partialBase(
{
isRoot: true,
description: m.site?.description,
...m,
},
html`${{verbatim: partialArticleSummaryList(m, m.collections!.postsRecent!)}}
<footer>
<p>→ <a href="posts/index.html">See all articles</a> or subscribe to the <a href="feed.xml">Atom feed</a></p>
</footer>`,
{
navigationVerbatim: partialNavigation(m, m.tagsTop!, {
incomplete: m.tagsTop!.length !== m.tagsAll!.length,
}),
}
);
const templateTagIndex: GoldsmithLiteralHTMLLayoutCallback = (_content, m) => partialBase(
{
title: `${m.site!.title!}: Posts tagged with: ${m.term!}`,
...m
},
partialArticleSummaryList(m, m.postsWithTag!),
{
navigationVerbatim: partialNavigation(m, m.tagsAll!, {
isTagIndex: true,
tag: m.tag,
}),
}
);
export const templates: GoldsmithLiteralHTMLLayoutMap = {
"404": template404,
"archive": templateArchive,
"default": templateDefault,
"index": templateRoot,
"post": templatePost,
"tagIndex": templateTagIndex,
};