X-Git-Url: https://git.r.bdr.sh/rbdr/blog/blobdiff_plain/fac54389550aaab8bcb4ad1e6b0b1900fd8887d2..467390bc032585f28da6203f891764f4a66c33b7:/lib/archivers/gemlog.js?ds=sidebyside diff --git a/lib/archivers/gemlog.js b/lib/archivers/gemlog.js index d50d411..3e08be4 100644 --- a/lib/archivers/gemlog.js +++ b/lib/archivers/gemlog.js @@ -1,20 +1,35 @@ -const { mkdir, readdir, rm, writeFile } = require('fs/promises'); -const { debuglog, promisify } = require('util'); -const { ncp } = require('ncp'); -const { join } = require('path'); +const { cp, mkdir, readdir, writeFile } = require('fs/promises'); +const { debuglog } = require('util'); +const { resolve, join } = require('path'); +const { rmIfExists } = require('../utils'); const internals = { - kArchiveName: '.gemlog', + kArchiveName: resolve(join(__dirname, '../..', '.gemlog')), kIndexName: 'index.gmi', kGeminiRe: /\.gmi$/i, - ncp: promisify(ncp), 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) + const postIds = (await readdir(archiveDirectory)) + .sort((a, b) => Number(b) - Number(a)); const posts = []; for (const id of postIds) { const postDirectory = join(archiveDirectory, id); @@ -26,12 +41,18 @@ module.exports = async function(archiveDirectory) { internals.debuglog(`Read ${posts.length} posts`); - const index = ['# Unlimited Pizza Gemlog Archive', '', - ...posts.map((post) => `=> ./${post.id}/${post.slug}`)].join('\n'); + 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 rm(internals.kArchiveName, { recursive: true }); + await rmIfExists(internals.kArchiveName); } finally { internals.debuglog('Creating index'); @@ -40,6 +61,6 @@ module.exports = async function(archiveDirectory) { await writeFile(indexFile, index); internals.debuglog('Copying posts to archive'); - await internals.ncp(archiveDirectory, internals.kArchiveName); + await cp(archiveDirectory, internals.kArchiveName, { recursive: true }); } };