From: Ruben Beltran del Rio Date: Mon, 19 Dec 2022 20:52:52 +0000 (+0000) Subject: Smarter filenames X-Git-Url: https://git.r.bdr.sh/rbdr/linkding-linkblog-updater/commitdiff_plain/f0b2faff6e6e69de483fccb940f7fc989c4c9030 Smarter filenames --- diff --git a/index.js b/index.js index c0e41dc..549e223 100644 --- a/index.js +++ b/index.js @@ -13,15 +13,30 @@ const internals = { date: (new Date()).toISOString().split('T')[0], - generateGemtext(posts) { + generateGemtext(title, text) { - const title = posts.length === 1 ? 'A link' : `${posts.length} links`; + return `# ${title}\n\n${text}`; + }, - const linkText = posts.map((pin) => { - return `=> ${pin.href} ${pin.description}\n${pin.extended}`; + getText(posts) { + + const linkText = posts.map((post) => { + + return `=> ${post.href} ${post.description}\n${post.extended}`; }).join('\n\n'); + }, + + getTitle(posts) { - return `# ${title} for ${internals.date}\n\n${linkText}`; + 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, '-') } }; @@ -38,8 +53,12 @@ async function run() { return; } - const gemtext = internals.generateGemtext(pins); - const gemfile = resolve(join(__dirname, `linkblog-${internals.date}.gmi`)); + 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); await internals.exec(`blog --add ${gemfile}`); await internals.exec(`blog --publish ${internals.blogUrl}`);