]>
git.r.bdr.sh - rbdr/blog/blob - lib/archivers/gemlog.js
1bb0c10d6d31740c930c09e29436018c0abc142a
2 import { cp
, mkdir
, readdir
, readFile
, writeFile
} from 'fs/promises';
3 import { debuglog
} from 'util';
4 import { join
} from 'path';
5 import { rmIfExists
} from '../utils.js';
8 kIndexName: 'index.gmi',
11 debuglog: debuglog('blog'),
15 return `./${id}/${slug}`;
18 buildTitle(id
, slug
) {
20 const date
= new Date(Number(id
));
21 const shortDate
= date
.toISOString().split('T')[0];
22 const title
= slug
.split('-').join(' ');
23 return `${shortDate} ${title}`;
28 return `=> ${internals.buildUrl(id,slug)} ${internals.buildTitle(id,slug)}`;
32 export default async
function (templateDirectory
, source
, target
) {
34 internals
.debuglog(`Reading archive ${source}`);
35 const postIds
= (await
readdir(source
))
36 .sort((a
, b
) => Number(b
) - Number(a
));
38 for (const id
of postIds
) {
39 const postDirectory
= join(source
, id
);
40 const slug
= (await
readdir(postDirectory
))
41 .filter((entry
) => internals
.kGeminiRe
.test(entry
))[0];
43 posts
.push({ id
, slug
});
46 internals
.debuglog(`Read ${posts.length} posts`);
48 internals
.debuglog('Generating Archive Index');
49 const indexLocation
= join(templateDirectory
, internals
.kIndexName
);
51 internals
.debuglog(`Reading ${indexLocation}`);
52 const indexTemplate
= await
readFile(indexLocation
, { encoding: 'utf8' });
54 internals
.debuglog('Writing Archive Index');
55 const index
= Dot
.template(indexTemplate
, {
56 ...Dot
.templateSettings
,
59 posts: posts
.map((post
) => internals
.buildLink(post
.id
, post
.slug
)).join('\n')
63 internals
.debuglog('Removing index');
64 await
rmIfExists(target
);
67 internals
.debuglog('Creating index');
69 const indexFile
= join(target
, internals
.kIndexName
);
70 await
writeFile(indexFile
, index
);
72 internals
.debuglog('Copying posts to archive');
73 await
cp(source
, target
, { recursive: true });