From b2b46a361589521d5f8ea1fca52ad41722367998 Mon Sep 17 00:00:00 2001 From: Leonardo Bishop Date: Tue, 8 Apr 2025 03:05:49 +0100 Subject: Add basic support for atom feeds --- app/builder/discoverFeed.ts | 67 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 app/builder/discoverFeed.ts (limited to 'app/builder/discoverFeed.ts') diff --git a/app/builder/discoverFeed.ts b/app/builder/discoverFeed.ts new file mode 100644 index 0000000..5ea1a16 --- /dev/null +++ b/app/builder/discoverFeed.ts @@ -0,0 +1,67 @@ +import path from "path"; +import fs from "fs"; +import { Feed, PageDirectory } from "./pageDirectory.js"; +import { logger } from "../logger.js"; + +export async function discoverFeed(feed: Feed, pageDirectory: PageDirectory): Promise { + const entries = []; + + const titleConfigPath = feed.paramStrategy?.title?.from || "title"; + const updatedConfigPath = feed.paramStrategy?.date?.from || "date"; + const descriptionConfigPath = feed.paramStrategy?.description?.from || "description"; + //todo support actual discovery strategies + + for (const page of pageDirectory.getPagesBeginningWith(feed.route)) { + if (page.name === 'index') { + continue; + } + const entry = { + title: page.config[titleConfigPath] || page.name, + updated: page.config[updatedConfigPath] || new Date(0), + id: page.name, + url: `${feed.url}/${page.name}`, + description: page.config[descriptionConfigPath] || "", + // description: page.html?.length > 100 ? `${page.html?.substring(0, 100)}...` || "" : page.html || "", + }; + entries.push(entry); + } + + feed.entries = entries; + feed.entries.sort((a, b) => b.updated.getTime() - a.updated.getTime()); + feed.updated = new Date(0); + for (const entry of feed.entries) { + if (entry.updated > feed.updated) { + feed.updated = entry.updated; + } + } + + const atomFeed = ` +${feed.title} +${feed.updated.toISOString()} + + +${feed.entries + .map((entry) => { + return ` +${entry.title} +${entry.updated.toISOString()} +${entry.id} + +${entry.description} +`; + }) + .join("\n")} +`; + + try { + const file = feed.buildPath; + const dir = path.dirname(file); + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + } + fs.writeFileSync(file, atomFeed); + } catch (e) { + logger.error(`Failed to write feed ${feed.buildPath}: ${e.message}`); + return false; + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2