]> git.r.bdr.sh - rbdr/blog/blob - lib/archivers/gemlog.js
Add archive publishing
[rbdr/blog] / lib / archivers / gemlog.js
1 const { mkdir, readdir, rm, writeFile } = require('fs/promises');
2 const { debuglog, promisify } = require('util');
3 const { ncp } = require('ncp');
4 const { resolve, join } = require('path');
5
6 const internals = {
7 kArchiveName: resolve(join(__dirname, '../..', '.gemlog')),
8 kIndexName: 'index.gmi',
9 kGeminiRe: /\.gmi$/i,
10
11 ncp: promisify(ncp),
12 debuglog: debuglog('blog'),
13
14 buildUrl(id, slug) {
15 return `./${id}/${slug}`;
16 },
17
18 buildTitle (id, slug) {
19 const date = new Date(Number(id));
20 const shortDate = date.toISOString().split('T')[0]
21 const title = slug.split('-').join(' ');
22 return `${shortDate} ${title}`
23 },
24
25 buildLink(id, slug) {
26 return `=> ${internals.buildUrl(id,slug)} ${internals.buildTitle(id,slug)}`;
27 }
28 };
29
30 module.exports = async function(archiveDirectory) {
31 internals.debuglog(`Reading archive ${archiveDirectory}`);
32 const postIds = (await readdir(archiveDirectory))
33 .sort((a, b) => Number(b) - Number(a));
34 const posts = [];
35 for (const id of postIds) {
36 const postDirectory = join(archiveDirectory, id);
37 const slug = (await readdir(postDirectory))
38 .filter((entry) => internals.kGeminiRe.test(entry))[0];
39
40 posts.push({ id, slug })
41 };
42
43 internals.debuglog(`Read ${posts.length} posts`);
44
45 const index = [
46 '# Unlimited Pizza Gemlog Archive', '',
47 '=> https://blog.unlimited.pizza/feed.xml 📰 RSS Feed',
48 '=> https://blog.unlimited.pizza/index.txt 📑 http text version (latest 3 posts)',
49 '',
50 ...posts.map((post) => internals.buildLink(post.id, post.slug)),
51 '', '=> ../ ðŸŠī Back to main page'
52 ].join('\n');
53
54 try {
55 internals.debuglog('Removing index');
56 await rm(internals.kArchiveName, { recursive: true });
57 }
58 finally {
59 internals.debuglog('Creating index');
60 await mkdir(internals.kArchiveName);
61 const indexFile = join(internals.kArchiveName, internals.kIndexName);
62 await writeFile(indexFile, index);
63
64 internals.debuglog('Copying posts to archive');
65 await internals.ncp(archiveDirectory, internals.kArchiveName);
66 }
67 };