From e3775f0ee2d9cedcf645a473f53d729ec0189377 Mon Sep 17 00:00:00 2001 From: Justin Bennett Date: Thu, 3 Oct 2024 20:46:30 -0400 Subject: [PATCH] Add rss --- package.json | 1 + pnpm-lock.yaml | 24 ++++++++++++++++++++++++ src/pages/all.astro | 2 +- src/pages/api/[...path].ts | 2 +- src/pages/rss.xml.ts | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/pages/rss.xml.ts diff --git a/package.json b/package.json index 2997590..7397b26 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "dependencies": { "@astrojs/check": "^0.9.1", "@astrojs/cloudflare": "^11.0.4", + "@astrojs/rss": "^4.0.7", "@astrojs/tailwind": "^5.1.0", "astro": "^4.13.1", "hono": "^4.5.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 80d03b7..0d52403 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -25,6 +25,9 @@ importers: '@astrojs/cloudflare': specifier: ^11.0.4 version: 11.0.4(astro@4.15.11(@types/node@22.1.0)(rollup@4.24.0)(typescript@5.5.4)) + '@astrojs/rss': + specifier: ^4.0.7 + version: 4.0.7 '@astrojs/tailwind': specifier: ^5.1.0 version: 5.1.1(astro@4.15.11(@types/node@22.1.0)(rollup@4.24.0)(typescript@5.5.4))(tailwindcss@3.4.7) @@ -183,6 +186,9 @@ packages: resolution: {integrity: sha512-Z9IYjuXSArkAUx3N6xj6+Bnvx8OdUSHA8YoOgyepp3+zJmtVYJIl/I18GozdJVW1p5u/CNpl3Km7/gwTJK85cw==} engines: {node: ^18.17.1 || ^20.3.0 || >=21.0.0} + '@astrojs/rss@4.0.7': + resolution: {integrity: sha512-ZEG55XFB19l+DplUvBISmz04UbjDtKliRO4Y5+ERRhAMjgCVVobEBNE6ZwWG1h6orWUocy4nfPihKXDyB73x9g==} + '@astrojs/tailwind@5.1.1': resolution: {integrity: sha512-LwurA10uIKcGRxQP2R81RvAnBT0WPKzBntXZBF4hrAefDgM5Uumn0nsGr6tdIjSARgYz4X+Cq/Vh78t3bql3yw==} peerDependencies: @@ -1832,6 +1838,10 @@ packages: resolution: {integrity: sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow==} engines: {node: '>=8.6.0'} + fast-xml-parser@4.5.0: + resolution: {integrity: sha512-/PlTQCI96+fZMAOLMZK4CWG1ItCbfZ/0jx7UIJFChPNrx7tcEgerUgWbeieCM9MfHInUDyK8DWYZ+YrywDJuTg==} + hasBin: true + fastq@1.17.1: resolution: {integrity: sha512-sRVD3lWVIXWg6By68ZN7vho9a1pQcN/WBFaAAsDDFzlJjvoGx0P8z7V1t72grFJfJhu3YPZBuu25f7Kaw2jN1w==} @@ -2910,6 +2920,9 @@ packages: resolution: {integrity: sha512-dOESqjYr96iWYylGObzd39EuNTa5VJxyvVAEm5Jnh7KGo75V43Hk1odPQkNDyXNmUR6k+gEiDVXnjB8HJ3crXw==} engines: {node: '>=12'} + strnum@1.0.5: + resolution: {integrity: sha512-J8bbNyKKXl5qYcR36TIO8W3mVGVHrmmxsd5PAItGkmyzwJvybiw2IVq5nqd0i4LSNSkB/sx9VHllbfFdr9k1JA==} + sucrase@3.35.0: resolution: {integrity: sha512-8EbVDiu9iN/nESwxeSxDKe0dunta1GOlHufmSSXxMD2z2/tMZpDMpvXQGsc+ajGo8y2uYUmixaSRUc/QPoQ0GA==} engines: {node: '>=16 || 14 >=14.17'} @@ -3514,6 +3527,11 @@ snapshots: dependencies: prismjs: 1.29.0 + '@astrojs/rss@4.0.7': + dependencies: + fast-xml-parser: 4.5.0 + kleur: 4.1.5 + '@astrojs/tailwind@5.1.1(astro@4.15.11(@types/node@22.1.0)(rollup@4.24.0)(typescript@5.5.4))(tailwindcss@3.4.7)': dependencies: astro: 4.15.11(@types/node@22.1.0)(rollup@4.24.0)(typescript@5.5.4) @@ -5093,6 +5111,10 @@ snapshots: merge2: 1.4.1 micromatch: 4.0.8 + fast-xml-parser@4.5.0: + dependencies: + strnum: 1.0.5 + fastq@1.17.1: dependencies: reusify: 1.0.4 @@ -6496,6 +6518,8 @@ snapshots: strip-final-newline@3.0.0: {} + strnum@1.0.5: {} + sucrase@3.35.0: dependencies: '@jridgewell/gen-mapping': 0.3.5 diff --git a/src/pages/all.astro b/src/pages/all.astro index 3091a8a..bcffb70 100644 --- a/src/pages/all.astro +++ b/src/pages/all.astro @@ -1,6 +1,6 @@ --- import Layout from "../layouts/default.astro"; -let { objects: pages } = await Astro.locals.runtime.env.R2_BUCKET.list({ +const { objects: pages } = await Astro.locals.runtime.env.R2_BUCKET.list({ prefix: "0", // @ts-expect-error This is actually supported but the types are wrong include: ["customMetadata"], diff --git a/src/pages/api/[...path].ts b/src/pages/api/[...path].ts index d7e78e8..3fe3f25 100644 --- a/src/pages/api/[...path].ts +++ b/src/pages/api/[...path].ts @@ -59,7 +59,7 @@ const app = new Hono<{ Bindings: AstroContext }>() const customMetadata: Record = { slug, }; - ["stage", "title", "published", "updated"].forEach((key) => { + ["stage", "title", "published", "updated", "tags"].forEach((key) => { if (props[key]) { customMetadata[key] = props[key]; } diff --git a/src/pages/rss.xml.ts b/src/pages/rss.xml.ts new file mode 100644 index 0000000..3bbf40c --- /dev/null +++ b/src/pages/rss.xml.ts @@ -0,0 +1,35 @@ +import rss, { type RSSFeedItem } from "@astrojs/rss"; +import type { APIRoute } from "astro"; + +export const GET: APIRoute = async (context) => { + const { objects: pages } = await context.locals.runtime.env.R2_BUCKET.list({ + prefix: "0", + // @ts-expect-error This is actually supported but the types are wrong + include: ["customMetadata"], + }); + + const items: RSSFeedItem[] = pages + .filter( + (page) => + page.customMetadata?.stage !== "draft" && + page.customMetadata?.title && + page.customMetadata?.published && + page.customMetadata?.slug + ) + .map((page) => ({ + title: page.customMetadata?.title, + pubDate: + !page.customMetadata?.tags?.includes("events") && + page.customMetadata?.updated + ? new Date(page.customMetadata?.updated) + : new Date(page.customMetadata?.published!), + link: `${context.locals.runtime.env.SITE}/${page.customMetadata?.slug}`, + })); + + return rss({ + title: "Just Be", + description: "The personal site of Justin Bennett", + site: context.locals.runtime.env.SITE, + items, + }); +};