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
|
import Dot from 'dot';
import { cp, mkdir, readdir, readFile, writeFile } from 'fs/promises';
import { debuglog } from 'util';
import { join } from 'path';
import { rmIfExists } from '../utils.js';
const internals = {
kIndexName: 'index.gmi',
kGeminiRe: /\.gmi$/i,
debuglog: debuglog('blog'),
buildUrl(id, slug) {
return `./${id}/${slug}`;
},
buildTitle(id, slug) {
const date = new Date(Number(id));
const shortDate = date.toISOString().split('T')[0];
const title = slug.split('-').join(' ');
return `${shortDate} ${title}`;
},
buildLink(id, slug) {
return `=> ${internals.buildUrl(id,slug)} ${internals.buildTitle(id,slug)}`;
}
};
export default async function (templateDirectory, source, target) {
internals.debuglog(`Reading archive ${source}`);
const postIds = (await readdir(source))
.sort((a, b) => Number(b) - Number(a));
const posts = [];
for (const id of postIds) {
const postDirectory = join(source, id);
const slug = (await readdir(postDirectory))
.filter((entry) => internals.kGeminiRe.test(entry))[0];
posts.push({ id, slug });
}
internals.debuglog(`Read ${posts.length} posts`);
internals.debuglog('Generating Archive Index');
const indexLocation = join(templateDirectory, internals.kIndexName);
internals.debuglog(`Reading ${indexLocation}`);
const indexTemplate = await readFile(indexLocation, { encoding: 'utf8' });
internals.debuglog('Writing Archive Index');
const index = Dot.template(indexTemplate, {
...Dot.templateSettings,
strip: false
})({
posts: posts.map((post) => internals.buildLink(post.id, post.slug)).join('\n')
});
try {
internals.debuglog('Removing index');
await rmIfExists(target);
}
finally {
internals.debuglog('Creating index');
await mkdir(target);
const indexFile = join(target, internals.kIndexName);
await writeFile(indexFile, index);
internals.debuglog('Copying posts to archive');
await cp(source, target, { recursive: true });
}
}
|