/** * Machine-readable views of the published site: RSS 2.0, `sitemap.xml` and `robots.txt`. Pure * functions over graph nodes (the starter's endpoints only fetch and wrap them), so the rules — * published only, noindex respected, canonical elsewhere excluded — live in one tested place. */ import type { ContentEntry, Page, Redirect, Site } from "@cloudhal/core-contracts"; import { collectionSettings, entryPath } from "@cloudhal/core-ts"; import { trimEndChar } from "@cloudhal/core-ts/names"; import type { ArchiveSummary, ArchiveView } from "./archive.ts"; import { markdownText, renderBody } from "./markdown.ts"; import type { AssetIndex } from "./media.ts"; import { notFoundPageIds } from "./not-found.ts"; import { absoluteUrl } from "./seo.ts"; import { entryTaxonomy, type TaxonomyIndex } from "./taxonomy.ts"; export const FEED_LIMIT = 50; const xml = (s: string) => s .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); /** CDATA cannot contain `]]>`; split it across two sections. */ const cdata = (s: string) => `/g, "]]]]>")}]]>`; function published(nodes: T[]): T[] { return nodes.filter((n) => n.status === "published"); } export interface FeedOptions { site: Site; origin: string; /** Newest first; drafts are dropped here even when a live source includes them. */ entries: ContentEntry[]; /** Where the feed itself is served (for `atom:link rel=self`). */ feedPath?: string; /** Channel description (e.g. the home page's SEO description); defaults to the site name. */ description?: string; /** Channel title; defaults to the site name. */ title?: string; /** Channel link path (the page the feed mirrors); defaults to the home page. */ linkPath?: string; /** * The site's terms: each item lists its public category and tag names as ``. Without * it (or for an entry not migrated to terms) the deprecated `tags` are the categories. */ taxonomy?: TaxonomyIndex; /** The site's assets: cover images get type, size and dimensions; `asset:` links resolve (RFC-0004 §7.7). */ assets?: AssetIndex; /** The feed's language (RFC-0033); default: the site's default locale. */ locale?: string; /** * Where the feed's links start (RFC-0033: `https://host/es` for a prefixed locale, a locale's * own domain); default `origin`. Media URLs always use `origin`. */ linkOrigin?: string; } /** Category names of an item: public terms (section first), else legacy tags. */ function itemCategories(entry: ContentEntry, index: TaxonomyIndex | undefined): string[] { const shown = entryTaxonomy(index, entry); const names = shown.groups .flatMap((g) => (g.taxonomy.key === "tag" ? [] : g.terms)) .map((t) => t.name); return [...new Set([...names, ...shown.keywords])]; } /** RSS 2.0 with full content (`content:encoded`), as feed readers and Ghost/WordPress readers expect. */ export function rssXml(options: FeedOptions): string { const { site, origin } = options; const base = options.linkOrigin ?? origin; const self = `${base}${options.feedPath ?? "/rss.xml"}`; // RFC-0040: a collection with `feed: false` stays out of every feed. const shown = published(options.entries).filter( (e) => collectionSettings(site, e.collection).feed, ); const items = shown.slice(0, FEED_LIMIT).map((e) => { const url = `${base}${entryPath(site, e)}`; const date = new Date(e.publishedAt ?? e.createdAt).toUTCString(); const cover = e.coverImage?.assetId ? options.assets?.get(e.coverImage.assetId) : undefined; const image = cover ? absoluteUrl( origin, cover.storage.kind === "external" ? cover.storage.url : e.coverImage?.src, ) : absoluteUrl(origin, e.coverImage?.src); const mediaAttrs = cover ? ` type="${xml(cover.mediaType)}" fileSize="${cover.bytes}"${cover.image ? ` width="${cover.image.width}" height="${cover.image.height}"` : ""}` : ""; const author = e.author.kind === "human" && e.author.name ? e.author.name : site.name; return [ "", `${xml(e.title)}`, `${xml(url)}`, `${xml(url)}`, `${date}`, `${cdata(author)}`, ...itemCategories(e, options.taxonomy).map((t) => `${xml(t)}`), e.summary ? `${cdata(e.summary)}` : "", image ? `` : "", `${cdata(renderBody(e.body, options.assets ? { assets: options.assets, locale: e.locale, defaultLocale: site.defaultLocale } : undefined))}`, "", ] .filter(Boolean) .join(""); }); const newest = shown[0]; return [ '', '', "", `${xml(options.title ?? site.name)}`, `${xml(`${base}${options.linkPath ?? "/"}`)}`, `${xml(options.description ?? site.name)}`, `${xml(options.locale ?? site.defaultLocale)}`, ``, "Cloudhal", newest ? `${new Date(newest.publishedAt ?? newest.createdAt).toUTCString()}` : "", ...items, "", "", ] .filter(Boolean) .join("\n"); } /** * A term's feed (`/rss.xml`, RFC-0002 §7): the archive's published entries, titled after * the term, linking to its archive, described by its SEO description or intro. */ export function archiveFeedXml( options: Omit & { archive: ArchiveView; }, ): string { const { archive, ...rest } = options; const label = archive.term.labels[archive.locale]; const description = label?.seo?.description ?? (label?.description ? markdownText(label.description) : undefined); return rssXml({ ...rest, entries: archive.entries, title: `${archive.shown.name} · ${options.site.name}`, linkPath: archive.root, ...(archive.feedPath ? { feedPath: archive.feedPath } : {}), ...(description ? { description } : {}), }); } export interface SitemapUrl { loc: string; lastmod: string; /** Every language version of the URL, itself and `x-default` included (RFC-0033). */ alternates?: ReadonlyArray<{ locale: string; href: string }>; } /** * What belongs in `sitemap.xml`: published pages and entries of the default locale, and the * indexable term archives (page 1 only, `lastmod` = the later of the newest listed entry and the * term; see `archiveSummaries` — pass it published entries). Pages and archives asking for * `noindex`, or canonicalised to another URL, are left out (a sitemap lists canonical URLs), and so * are the entries of collections with `sitemap: false` (RFC-0040). */ export function sitemapUrls( site: Site, pages: Page[], entries: ContentEntry[], origin: string, archives: readonly ArchiveSummary[] = [], ): SitemapUrl[] { const urls: SitemapUrl[] = []; const seen = new Set(); const notFound = notFoundPageIds(pages); for (const page of published(pages)) { if (page.locale !== site.defaultLocale || page.seo?.noindex || notFound.has(page.id)) continue; const own = `${origin}${page.path === "/" ? "/" : page.path}`; const canonical = absoluteUrl(origin, page.seo?.canonical); if (canonical && canonical !== own) continue; seen.add(page.path); urls.push({ loc: own, lastmod: page.updatedAt }); } for (const entry of published(entries)) { const path = entryPath(site, entry); if (entry.locale !== site.defaultLocale || seen.has(path)) continue; if (!collectionSettings(site, entry.collection).sitemap) continue; seen.add(path); urls.push({ loc: `${origin}${path}`, lastmod: entry.updatedAt }); } for (const archive of archives) { if (!archive.indexable || seen.has(archive.path)) continue; const own = `${origin}${archive.path}`; const canonical = absoluteUrl(origin, archive.term.labels[site.defaultLocale]?.seo?.canonical); if (canonical && canonical !== own) continue; seen.add(archive.path); urls.push({ loc: own, lastmod: archive.lastmod }); } return urls; } /** * The site's redirects as a `_redirects` file (Cloudflare Workers/Pages static assets, Netlify): * static hosts that read it answer with real 301/302s, where Astro's meta-refresh pages are only a * fallback. Exact redirects come first, then prefix redirects as `from` + `from/*` splats, longest * first (hosts apply the first match). */ export function redirectsFile(redirects: readonly Redirect[]): string { const exact = redirects.filter((r) => r.match === "exact"); const prefix = redirects .filter((r) => r.match === "prefix") .sort((a, b) => b.from.length - a.from.length || a.from.localeCompare(b.from)); const splat = (to: string) => { const i = to.search(/[?#]/); const head = trimEndChar(i < 0 ? to : to.slice(0, i), "/"); return `${head}/:splat${i < 0 ? "" : to.slice(i)}`; }; const lines = [ "# Generated by Cloudhal from the rendered snapshot's Redirect nodes (RFC-0002). Do not edit.", ...exact.map((r) => `${r.from} ${r.to} ${r.status}`), ...prefix.flatMap((r) => [ `${r.from} ${r.to} ${r.status}`, `${r.from === "/" ? "" : r.from}/* ${splat(r.to)} ${r.status}`, ]), ]; return `${lines.join("\n")}\n`; } export function sitemapXml(urls: SitemapUrl[]): string { const localized = urls.some((u) => u.alternates?.length); const link = (a: { locale: string; href: string }) => ``; return [ '', localized ? '' : '', ...urls.map( (u) => `${xml(u.loc)}${xml(u.lastmod)}${(u.alternates ?? []).map(link).join("")}`, ), "", ].join("\n"); } export function robotsTxt(origin: string): string { return `User-agent: *\nAllow: /\n\nSitemap: ${origin}/sitemap.xml\n`; }