deno.land / x / lume@v2.1.4 / plugins / feed.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280import { getExtension } from "../core/utils/path.ts";import { merge } from "../core/utils/object.ts";import { getCurrentVersion } from "../core/utils/lume_version.ts";import { getDataValue } from "../core/utils/data_values.ts";import { $XML, stringify } from "../deps/xml.ts";import { Page } from "../core/file.ts";
import type Site from "../core/site.ts";import type { Data } from "../core/file.ts";
export interface Options { /** The output filenames */ output?: string | string[];
/** The query to search the pages */ query?: string;
/** The sort order */ sort?: string;
/** The maximum number of items */ limit?: number;
/** The feed info */ info?: FeedInfoOptions;
/** The feed items configuration */ items?: FeedItemOptions;}
export interface FeedInfoOptions { /** The feed title */ title?: string;
/** The feed subtitle */ subtitle?: string;
/** * The feed published date * @default `new Date()` */ published?: Date;
/** The feed description */ description?: string;
/** The feed language */ lang?: string;
/** The feed generator. Set `true` to generate automatically */ generator?: string | boolean;}
export interface FeedItemOptions { /** The item title */ title?: string | ((data: Data) => string | undefined);
/** The item description */ description?: string | ((data: Data) => string | undefined);
/** The item published date */ published?: string | ((data: Data) => Date | undefined);
/** The item updated date */ updated?: string | ((data: Data) => Date | undefined);
/** The item content */ content?: string | ((data: Data) => string | undefined);
/** The item language */ lang?: string | ((data: Data) => string | undefined);}
export const defaults: Options = { /** The output filenames */ output: "/feed.rss",
/** The query to search the pages */ query: "",
/** The sort order */ sort: "date=desc",
/** The maximum number of items */ limit: 10,
/** The feed info */ info: { title: "My RSS Feed", published: new Date(), description: "", lang: "en", generator: true, }, items: { title: "=title", description: "=description", published: "=date", content: "=children", lang: "=lang", },};
export interface FeedData { title: string; url: string; description: string; published: Date; lang: string; generator?: string; items: FeedItem[];}
export interface FeedItem { title: string; url: string; description: string; published: Date; updated?: Date; content: string; lang: string;}
const defaultGenerator = `Lume ${getCurrentVersion()}`;
export default function (userOptions?: Options) { const options = merge(defaults, userOptions);
return (site: Site) => { site.addEventListener("beforeSave", () => { const output = Array.isArray(options.output) ? options.output : [options.output];
const pages = site.search.pages( options.query, options.sort, options.limit, ) as Data[];
const { info, items } = options; const rootData = site.source.data.get("/") || {};
const feed: FeedData = { title: getDataValue(rootData, info.title), description: getDataValue(rootData, info.description), published: getDataValue(rootData, info.published), lang: getDataValue(rootData, info.lang), url: site.url("", true), generator: info.generator === true ? defaultGenerator : info.generator || undefined, items: pages.map((data): FeedItem => { const content = getDataValue(data, items.content)?.toString(); const pageUrl = site.url(data.url, true); const fixedContent = fixUrls(new URL(pageUrl), content || "");
return { title: getDataValue(data, items.title), url: site.url(data.url, true), description: getDataValue(data, items.description), published: getDataValue(data, items.published), updated: getDataValue(data, items.updated), content: fixedContent, lang: getDataValue(data, items.lang), }; }), };
for (const filename of output) { const format = getExtension(filename).slice(1); const file = site.url(filename, true);
switch (format) { case "rss": case "feed": case "xml": site.pages.push( Page.create({ url: filename, content: generateRss(feed, file) }), ); break;
case "json": site.pages.push( Page.create({ url: filename, content: generateJson(feed, file) }), ); break;
default: throw new Error(`Invalid Feed format "${format}"`); } } }); };}
function fixUrls(base: URL, html: string): string { return html.replaceAll( /\s(href|src)="([^"]+)"/g, (_match, attr, value) => ` ${attr}="${new URL(value, base).href}"`, );}
function generateRss(data: FeedData, file: string): string { const feed = { [$XML]: { cdata: [["rss", "channel", "item", "content:encoded"]] }, xml: { "@version": "1.0", "@encoding": "UTF-8", }, rss: { "@xmlns:content": "http://purl.org/rss/1.0/modules/content/", "@xmlns:wfw": "http://wellformedweb.org/CommentAPI/", "@xmlns:dc": "http://purl.org/dc/elements/1.1/", "@xmlns:atom": "http://www.w3.org/2005/Atom", "@xmlns:sy": "http://purl.org/rss/1.0/modules/syndication/", "@xmlns:slash": "http://purl.org/rss/1.0/modules/slash/", "@version": "2.0", channel: clean({ title: data.title, link: data.url, "atom:link": { "@href": file, "@rel": "self", "@type": "application/rss+xml", }, description: data.description, lastBuildDate: data.published.toUTCString(), language: data.lang, generator: data.generator, item: data.items.map((item) => clean({ title: item.title, link: item.url, guid: { "@isPermaLink": false, "#text": item.url, }, description: item.description, "content:encoded": item.content, pubDate: item.published.toUTCString(), "atom:updated": item.updated?.toISOString(), }) ), }), }, };
return stringify(feed);}
function generateJson(data: FeedData, file: string): string { const feed = clean({ version: "https://jsonfeed.org/version/1", title: data.title, home_page_url: data.url, feed_url: file, description: data.description, items: data.items.map((item) => clean({ id: item.url, url: item.url, title: item.title, content_html: item.content, date_published: item.published.toUTCString(), date_modified: item.updated?.toUTCString(), }) ), });
return JSON.stringify(feed);}
/** Remove undefined values of an object */function clean(obj: Record<string, unknown>) { return Object.fromEntries( Object.entries(obj).filter(([, value]) => value !== undefined), );}
Version Info