From e5da7e6491724d09e3c1802c3daccff25e165d0f Mon Sep 17 00:00:00 2001 From: "Benjamin A. Shelton" Date: Sat, 5 Dec 2026 16:45:50 -0700 Subject: [PATCH] Added robots.txt and rss.xml generation. --- src/pages/robots.txt.ts | 18 ++++++++++++++ src/pages/rss.xml.js | 52 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/pages/robots.txt.ts create mode 100644 src/pages/rss.xml.js diff --git a/src/pages/robots.txt.ts b/src/pages/robots.txt.ts new file mode 100644 index 0000000..7e49b3f --- /dev/null +++ b/src/pages/robots.txt.ts @@ -0,0 +1,18 @@ +import type { APIRoute } from 'astro'; + +const getRobotsTxt = (sitemapURL: URL) => ` +User-agent: * +Allow: / +Disallow: /tags/ + +Sitemap: ${sitemapURL.href} +`; + +export const GET: APIRoute = ({ site }) => { + const sitemapURL = new URL('sitemap-index.xml', site); + return new Response(getRobotsTxt(sitemapURL).trimStart(), { + headers: { + 'Content-Type': 'text/plain; charset=utf-8', + }, + }); +}; \ No newline at end of file diff --git a/src/pages/rss.xml.js b/src/pages/rss.xml.js new file mode 100644 index 0000000..852bc91 --- /dev/null +++ b/src/pages/rss.xml.js @@ -0,0 +1,52 @@ +import rss from '@astrojs/rss'; +import sanitizeHtml from 'sanitize-html'; +import { experimental_AstroContainer as AstroContainer } from 'astro/container'; +import { getCollection, render } from 'astro:content'; + +export async function GET(context) { + const notes = (await getCollection('notes')).sort((a, b) => + a.data.date > b.data.date ? 1 : -1 + ).filter((note) => (note.data.published)); + const container = await AstroContainer.create(); + //const notes = await getCollection('notes'); + const items = await Promise.all( + notes.map(async (note) => { + const { Content } = await render(note); + const rawHTML = await container.renderToString(Content); + const content = sanitizeHtml(rawHTML, { + allowedTags: sanitizeHtml.defaults.allowedTags.concat(['a']), + allowedAttributes: { + ...sanitizeHtml.defaults.allowedAttributes, + a: ['href', 'name', 'class'], + '*': ['id'], + }, + transformTags: { + a: (tagName, attribs) => ({ + tagName, + attribs: { + ...attribs, + href: attribs.href?.startsWith('/') + ? new URL(attribs.href, context.site).toString() + : attribs.href, + }, + }), + } + }) + return { + title: note.data.title, + pubDate: note.data.date, + description: note.data.title, + link: `/notes/${note.id}`, + content, + }; + }) + ); + + return rss({ + title: "It's Just Us", + description: 'Just Us Class Notes and More', + site: context.site, + items, + customData: 'en-us', + }); +}; \ No newline at end of file