X-Git-Url: https://git.r.bdr.sh/rbdr/blog/blobdiff_plain/d3f282a164e44f54678cdb45aad7a09c8a92b89e..172f4c8807d44ebe38c7f227b7fdc2d6a9dbe323:/lib/archivers/gemlog.js diff --git a/lib/archivers/gemlog.js b/lib/archivers/gemlog.js deleted file mode 100644 index 3e08be4..0000000 --- a/lib/archivers/gemlog.js +++ /dev/null @@ -1,66 +0,0 @@ -const { cp, mkdir, readdir, writeFile } = require('fs/promises'); -const { debuglog } = require('util'); -const { resolve, join } = require('path'); -const { rmIfExists } = require('../utils'); - -const internals = { - kArchiveName: resolve(join(__dirname, '../..', '.gemlog')), - 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)}`; - } -}; - -module.exports = async function(archiveDirectory) { - internals.debuglog(`Reading archive ${archiveDirectory}`); - const postIds = (await readdir(archiveDirectory)) - .sort((a, b) => Number(b) - Number(a)); - const posts = []; - for (const id of postIds) { - const postDirectory = join(archiveDirectory, id); - const slug = (await readdir(postDirectory)) - .filter((entry) => internals.kGeminiRe.test(entry))[0]; - - posts.push({ id, slug }) - }; - - internals.debuglog(`Read ${posts.length} posts`); - - const index = [ - '# Unlimited Pizza Gemlog Archive', '', - '=> https://blog.unlimited.pizza/feed.xml 📰 RSS Feed', - '=> https://blog.unlimited.pizza/index.txt 📑 http text version (latest 3 posts)', - '', - ...posts.map((post) => internals.buildLink(post.id, post.slug)), - '', '=> ../ 🪴 Back to main page' - ].join('\n'); - - try { - internals.debuglog('Removing index'); - await rmIfExists(internals.kArchiveName); - } - finally { - internals.debuglog('Creating index'); - await mkdir(internals.kArchiveName); - const indexFile = join(internals.kArchiveName, internals.kIndexName); - await writeFile(indexFile, index); - - internals.debuglog('Copying posts to archive'); - await cp(archiveDirectory, internals.kArchiveName, { recursive: true }); - } -};