From 49a9198af9c02f11b4d3d5f2751a47d91366843f Mon Sep 17 00:00:00 2001 From: "brobert (aider)" Date: Tue, 1 Apr 2025 15:01:52 +0200 Subject: [PATCH] feat: add ePub generator script for blog posts --- generate-epub.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 generate-epub.ts diff --git a/generate-epub.ts b/generate-epub.ts new file mode 100644 index 0000000..ffa92ba --- /dev/null +++ b/generate-epub.ts @@ -0,0 +1,57 @@ +import { readFile } from 'fs/promises'; +import Epub from 'epub-gen'; +import { parseHTML } from 'linkedom'; + +interface BlogPost { + title: string; + author: string; + content: string; + url: string; +} + +async function generateEpub() { + // Read the scraped posts + const postsData = await readFile('posts.json', 'utf-8'); + const posts: BlogPost[] = JSON.parse(postsData); + + if (posts.length === 0) { + console.error('No posts found in posts.json'); + return; + } + + // Prepare ePub options + const options = { + title: 'Crónicas Periodísticas', + author: posts[0].author, // Using first post's author as main author + output: 'cronicas-periodisticas.epub', + content: [], + appendChapterTitles: true, + verbose: true + }; + + // Convert each post to ePub chapter format + for (const post of posts) { + const { document } = parseHTML(post.content); + + // Clean up content - remove unwanted elements if needed + const content = document.body.innerHTML; + + options.content.push({ + title: post.title, + author: post.author, + data: ` +

${post.title}

+

Por ${post.author}

+ ${content} +

Publicación original

+ ` + }); + } + + // Generate the ePub + console.log(`Generating ePub with ${posts.length} posts...`); + await new Epub(options).promise; + console.log('ePub generated successfully!'); +} + +generateEpub().catch(console.error);