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