-
Notifications
You must be signed in to change notification settings - Fork 4
/
_config.ts
158 lines (139 loc) · 5.22 KB
/
_config.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
import lume from 'lume/mod.ts';
import postcss from 'lume/plugins/postcss.ts';
import nunjucks from 'lume/plugins/nunjucks.ts';
import date from 'lume/plugins/date.ts';
import temporalDate from './src/_lume-plugins/temporal-date.ts';
import { readingTime } from './src/_lume-plugins/reading-time.ts';
import { excerpts } from './src/_lume-plugins/excerpts.ts';
import { typeset } from './src/_lume-plugins/typeset.ts';
import cooklang from './src/_lume-plugins/cooklang.ts';
import sourceMaps from 'lume/plugins/source_maps.ts';
import sitemap from 'lume/plugins/sitemap.ts';
import { idOf, postsRoot } from './src/_includes/permalinks.ts';
import { microRoot } from './src/_includes/permalinks.ts';
import { booksRoot } from './src/_includes/permalinks.ts';
import postcssUtopia from 'npm:postcss-utopia@^1';
import nesting from 'npm:postcss-nesting@^12';
import { type Book, currentBookOf } from './api/model/book.ts';
import { feeds } from './_feeds.ts';
import { maybeSaveTodo } from './_syndicate.ts';
const site = lume({
src: 'src',
dest: 'build',
location: new URL('https://johan.im'), // Ignored in dev
});
site.use(typeset({ scope: '.prose' }))
.use(nunjucks())
.copy('public', '.')
.use(feeds())
.use(sitemap())
.use(
postcss({
plugins: [
nesting(),
postcssUtopia({
minWidth: 320,
maxWidth: 653,
}),
],
}),
)
.use(temporalDate())
.use(date())
.use(sourceMaps())
.use(excerpts())
.use(cooklang())
// Helpers
.filter('substr', (str: string, len: number) => str.substring(0, len))
.filter('readingTime', (pageOrContent: Lume.Page | string) => {
if (!pageOrContent) {
throw new Error(`Passed falsy value to readingTime filter: ${pageOrContent}`);
}
return readingTime(pageOrContent);
})
.filter('postAssetUrl', (filename: string) => `/assets/posts/${filename}`)
.filter('hostname', (url: string) => new URL(url).host.replace('www.', ''))
.filter('mastodonUrl', function (this: ThisContext) {
const { meta } = this.data;
return `https://${meta.mastodon.instance}/@${meta.mastodon.username}`;
})
.filter('isCurrentPage', function (this: ThisContext, url: string) {
const curr = this.data.url.replace(/\/$/, '');
const test = url.replace(/\/$/, '');
if (curr == test) return true;
const parts = curr.split('/').filter(Boolean);
if (parts.length > 1 && '/' + parts[0] == test) return true;
return false;
})
.filter('groupBooksByYear', (arr: Array<Book>) => {
const current = new Date().getUTCFullYear();
const groups: Record<string | number, Book[]> = {
[current]: [],
};
for (const a of arr) {
const date = a.finishedAt;
if (!date) {
if (!a.finished) groups[current].push(a);
continue;
}
if (date instanceof Date == false) {
throw new Error(`"finishedAt" is not a date: ${date} on ${JSON.stringify(a)}`);
}
const group = (date as Date).getFullYear();
if (groups[group]) groups[group].push(a);
else groups[group] = [a];
}
return groups;
})
.filter('id', (d: Lume.Data) => {
return idOf(d.page.sourcePath);
})
// Fixes `str` to be suitable for JSON output
.filter('jsonHtml', (str: string) => JSON.stringify(str.replace('"', '"')))
// Data
.data('pageSlug', function (this: ThisContext) {
return this.ctx.url.replaceAll('/', '');
})
.data('isCSSNakedDay', () => {
// https://css-naked-day.github.io
const now = new Date();
return now.getMonth() == 3 && now.getDate() == 9;
})
.data('parent', function (this: ThisContext) {
switch (this.ctx.page.data.type) {
case 'post':
return ['Writings', postsRoot];
case 'note':
return ['Micro', microRoot];
case 'book':
return ['Reading', booksRoot];
default:
return null;
}
})
.data('currentBook', function (this: ThisContext<{ books: Book[] }>) {
return currentBookOf(this.ctx.books).at(-1);
})
// Don't rebuild the entire site rebuild when --watching or --serving if .css files change
.scopedUpdates((path) => path.endsWith('.css'))
// Support skip links with #main on <main>. Main!
.process(['.html'], (pages) => {
for (const page of pages) {
const doc = page.document;
const main = doc?.querySelector('main');
if (main) {
main.id = 'main';
}
}
})
.addEventListener('afterBuild', async (evt) => {
const latestNote = evt.pages
//
.filter((t) => t.data.type == 'note')
.sort((a, b) => a.data.date.getTime() - b.data.date.getTime())
.at(-1);
if (!latestNote) return;
await maybeSaveTodo(latestNote);
});
export type ThisContext<T = Record<string, unknown>> = { ctx: T & Lume.Data & { page: Lume.Page }; data: Lume.Data };
export default site;