Home / Function/ generateRSS() — astro Function Reference

generateRSS() — astro Function Reference

Architecture documentation for the generateRSS() function in index.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  afa06ba6_bbb9_385a_5c1e_7cdf9d6ad746["generateRSS()"]
  6ef7bfdb_4b52_2205_aa0f_e41748222587["index.ts"]
  afa06ba6_bbb9_385a_5c1e_7cdf9d6ad746 -->|defined in| 6ef7bfdb_4b52_2205_aa0f_e41748222587
  39f7c782_b8c0_c38e_4d52_c808fe876c46["getRssString()"]
  39f7c782_b8c0_c38e_4d52_c808fe876c46 -->|calls| afa06ba6_bbb9_385a_5c1e_7cdf9d6ad746
  style afa06ba6_bbb9_385a_5c1e_7cdf9d6ad746 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro-rss/src/index.ts lines 165–270

async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
	const { items, site } = rssOptions;

	const xmlOptions = {
		ignoreAttributes: false,
		// Avoid correcting self-closing tags to standard tags
		// when using `customData`
		// https://github.com/withastro/astro/issues/5794
		suppressEmptyNode: true,
		suppressBooleanAttributes: false,
	};
	const parser = new XMLParser(xmlOptions);
	const root: any = { '?xml': { '@_version': '1.0', '@_encoding': 'UTF-8' } };
	if (typeof rssOptions.stylesheet === 'string') {
		const isXSL = /\.xslt?$/i.test(rssOptions.stylesheet);
		root['?xml-stylesheet'] = {
			'@_href': rssOptions.stylesheet,
			...(isXSL && { '@_type': 'text/xsl' }),
		};
	}
	root.rss = { '@_version': '2.0' };
	if (items.find((result) => result.content)) {
		// the namespace to be added to the xmlns:content attribute to enable the <content> RSS feature
		const XMLContentNamespace = 'http://purl.org/rss/1.0/modules/content/';
		root.rss['@_xmlns:content'] = XMLContentNamespace;
		// Ensure that the user hasn't tried to manually include the necessary namespace themselves
		if (rssOptions.xmlns?.content && rssOptions.xmlns.content === XMLContentNamespace) {
			delete rssOptions.xmlns.content;
		}
	}

	// xmlns
	if (rssOptions.xmlns) {
		for (const [k, v] of Object.entries(rssOptions.xmlns)) {
			root.rss[`@_xmlns:${k}`] = v;
		}
	}

	// title, description, customData
	root.rss.channel = {
		title: rssOptions.title,
		description: rssOptions.description,
		link: createCanonicalURL(site, rssOptions.trailingSlash, undefined),
	};
	if (typeof rssOptions.customData === 'string')
		Object.assign(
			root.rss.channel,
			parser.parse(`<channel>${rssOptions.customData}</channel>`).channel,
		);
	// items
	root.rss.channel.item = items.map((result) => {
		const item: Record<string, unknown> = {};

		if (result.title) {
			item.title = result.title;
		}
		if (typeof result.link === 'string') {
			// If the item's link is already a valid URL, don't mess with it.
			const itemLink = isValidURL(result.link)
				? result.link
				: createCanonicalURL(result.link, rssOptions.trailingSlash, site);
			item.link = itemLink;
			item.guid = { '#text': itemLink, '@_isPermaLink': 'true' };
		}
		if (result.description) {
			item.description = result.description;
		}
		if (result.pubDate) {
			item.pubDate = result.pubDate.toUTCString();
		}
		// include the full content of the post if the user supplies it
		if (typeof result.content === 'string') {
			item['content:encoded'] = result.content;
		}
		if (typeof result.customData === 'string') {
			Object.assign(item, parser.parse(`<item>${result.customData}</item>`).item);
		}
		if (Array.isArray(result.categories)) {
			item.category = result.categories;
		}
		if (typeof result.author === 'string') {

Domain

Subdomains

Called By

Frequently Asked Questions

What does generateRSS() do?
generateRSS() is a function in the astro codebase, defined in packages/astro-rss/src/index.ts.
Where is generateRSS() defined?
generateRSS() is defined in packages/astro-rss/src/index.ts at line 165.
What calls generateRSS()?
generateRSS() is called by 1 function(s): getRssString.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free