const { exec } = require('child_process');
const { resolve, join} = require('path');
const { rm, writeFile } = require('fs/promises');
-const { debuglog, promisify } = require('util');
+const { promisify } = require('util');
const Pinboard = require('node-pinboard').default;
const internals = {
exec: promisify(exec),
- debuglog: debuglog('blog'),
+
apiToken: process.env.PINBOARD_TOKEN,
+ blogUrl: process.env.BLOG_URL,
+ archiveUrl: process.env.ARCHIVE_URL,
+
date: (new Date()).toISOString().split('T')[0],
- generateGemtext(posts) {
+ generateGemtext(title, text) {
+
+ return `# ${title}\n\n${text}`;
+ },
+
+ getText(posts) {
- const title = posts.length === 1 ? 'A link' : `${posts.length} links`;
+ const linkText = posts.map((post) => {
- const linkText = posts.map((pin) => {
- return `=> ${pin.href} ${pin.description}\n${pin.extended}`;
+ return `=> ${post.href} ${post.description}\n${post.extended}`;
}).join('\n\n');
+ },
- return `# ${title} for ${internals.date}\n\n${linkText}`;
+ getTitle(posts) {
+
+ if (posts.length === 1) {
+ return posts[0].description;
+ }
+ return `${posts.length} links for ${internals.date}`;
+ },
+
+ slugify(text) {
+
+ return text.toLowerCase().replace(/[^a-z0-9 ]/g, '').replace(/ +/g, '-')
}
};
async function run() {
const pinboard = new Pinboard(internals.apiToken);
- const getPins = promisify(pinboard.get);
+ const getPins = promisify(pinboard.all);
const addPin = promisify(pinboard.add);
- internals.debuglog('Fetching Pins');
const pins = await getPins({ tag: 'linkblog' });
- internals.debuglog(`Found ${pins.posts.length} pins`);
- if (pins.posts.length === 0) {
+ if (pins.length === 0) {
console.error('No links to post');
return;
}
- internals.debuglog('Generating gemtext');
- const gemtext = internals.generateGemtext(pins.posts);
- const gemfile = resolve(join(__dirname, `linkblog-${internals.date}.gmi`));
- internals.debuglog(`Writing to ${gemfile}`);
+ const title = internals.getTitle(posts);
+ const text = internals.getText(posts);
+ const gemtext = internals.generateGemtext(title, text);
+ const filename = internals.slugify(title);
+
+ const gemfile = resolve(join(__dirname, `${filename}.gmi`));
await writeFile(gemfile, gemtext);
- internals.debuglog('Adding gemfile to log');
- const output = await internals.exec(`blog --add ${gemfile}`);
- internals.debuglog(output.stderr);
- internals.debuglog(`Removing ${gemfile}`);
+ await internals.exec(`blog --add ${gemfile}`);
+ await internals.exec(`blog --publish ${internals.blogUrl}`);
+ await internals.exec(`blog --publish-archive ${internals.archiveUrl}`);
await rm(gemfile);
- for (const pin of pins.posts) {
- internals.debuglog(`Updating ${pin.href}`);
+ for (const pin of pins) {
await addPin({
url: pin.href,
description: pin.description,