import * as convert from "xml-js"; import { generator } from "./config"; import type { Feed } from "./feed"; import type { Author, Category, Enclosure, Extension, Item } from "./typings"; import { sanitizeUrl } from "./utils"; export default (ins: Feed) => { const { options, extensions } = ins; let needsAtomNamespace = false; let needsContentNamespace = false; const base: convert.ElementCompact = { _declaration: { _attributes: { version: "1.0", encoding: "utf-8" } }, _instruction: {}, rss: { _attributes: { version: "2.0" }, channel: { title: { _text: options.title }, link: { _text: sanitizeUrl(options.link) }, description: { _text: options.description ?? "" }, lastBuildDate: { _text: options.updated ? options.updated.toUTCString() : new Date().toUTCString() }, docs: { _text: options.docs ?? "https://validator.w3.org/feed/docs/rss2.html" }, ...(options.generator === false ? {} : { generator: { _text: options.generator ?? generator } }), }, }, }; if (options.stylesheet) { base._instruction = { "xml-stylesheet": { _attributes: { href: options.stylesheet, type: "text/xsl", }, }, }; } else { delete base._instruction; } if (options.language) { base.rss.channel.language = { _text: options.language }; } if (options.ttl) { base.rss.channel.ttl = { _text: options.ttl }; } if (options.image) { base.rss.channel.image = { title: { _text: options.title }, url: { _text: options.image }, link: { _text: sanitizeUrl(options.link) }, }; } if (options.copyright) { base.rss.channel.copyright = { _text: options.copyright }; } ins.categories.forEach((category) => { if (!base.rss.channel.category) { base.rss.channel.category = []; } base.rss.channel.category.push({ _text: category }); }); const atomLink = options.feed ?? options.feedLinks?.rss; if (atomLink) { needsAtomNamespace = true; if (!base.rss.channel["atom:link"]) { base.rss.channel["atom:link"] = []; } base.rss.channel["atom:link"].push({ _attributes: { href: sanitizeUrl(atomLink), rel: "self", type: "application/rss+xml", }, }); } if (options.hub) { needsAtomNamespace = true; if (!base.rss.channel["atom:link"]) { base.rss.channel["atom:link"] = []; } base.rss.channel["atom:link"].push({ _attributes: { href: sanitizeUrl(options.hub), rel: "hub", }, }); } base.rss.channel.item = []; ins.items.forEach((entry: Item) => { const item: convert.ElementCompact = {}; if (entry.title) { item.title = { _cdata: entry.title }; } if (entry.link) { item.link = { _text: sanitizeUrl(entry.link) }; } if (entry.guid) { item.guid = { _text: entry.guid, _attributes: { isPermaLink: false } }; } else if (entry.id) { item.guid = { _text: entry.id, _attributes: { isPermaLink: false } }; } else if (entry.link) { item.guid = { _text: sanitizeUrl(entry.link), _attributes: { isPermaLink: true } }; } if (entry.date) { item.pubDate = { _text: entry.date.toUTCString() }; } if (entry.published) { item.pubDate = { _text: entry.published.toUTCString() }; } if (entry.description) { item.description = { _cdata: entry.description }; } if (entry.content) { needsContentNamespace = true; item["content:encoded"] = { _cdata: entry.content }; } if (Array.isArray(entry.author)) { item.author = []; entry.author.forEach((author: Author) => { if (author.email && author.name) { item.author.push({ _text: `${author.email} (${author.name})` }); } else if (author.name) { item.author.push({ _text: author.name }); } }); } if (Array.isArray(entry.category)) { item.category = []; entry.category.forEach((category: Category) => { item.category.push(formatCategory(category)); }); } if (entry.enclosure) { item.enclosure = formatEnclosure(entry.enclosure); } if (entry.image) { item.enclosure = formatEnclosure(entry.image, "image"); } if (entry.audio) { const duration = options.podcast && typeof entry.audio !== "string" && entry.audio.duration ? entry.audio.duration : undefined; if (duration) { entry.audio.duration = undefined; } item.enclosure = formatEnclosure(entry.audio, "audio"); if (duration) { item["itunes:duration"] = formatDuration(duration); } } if (entry.video) { item.enclosure = formatEnclosure(entry.video, "video"); } if (entry.extensions) { entry.extensions.forEach((extension: Extension) => { item[extension.name] = extension.objects; }); } base.rss.channel.item.push(item); }); if (needsContentNamespace) { base.rss._attributes["xmlns:dc"] = "http://purl.org/dc/elements/1.1/"; base.rss._attributes["xmlns:content"] = "http://purl.org/rss/1.0/modules/content/"; } if (extensions) extensions.forEach((e: Extension) => { base.rss.channel[e.name] = e.objects; }); if (needsAtomNamespace) { base.rss._attributes["xmlns:atom"] = "http://www.w3.org/2005/Atom"; } if (options.podcast) { base.rss._attributes["xmlns:googleplay"] = "http://www.google.com/schemas/play-podcasts/1.0"; base.rss._attributes["xmlns:itunes"] = "http://www.itunes.com/dtds/podcast-1.0.dtd"; if (options.category) { base.rss.channel["googleplay:category"] = options.category; base.rss.channel["itunes:category"] = options.category; } if (options.author?.email) { base.rss.channel["googleplay:owner"] = options.author.email; base.rss.channel["itunes:owner"] = { "itunes:email": options.author.email, }; } if (options.author?.name) { base.rss.channel["googleplay:author"] = options.author.name; base.rss.channel["itunes:author"] = options.author.name; } if (options.image) { base.rss.channel["googleplay:image"] = { _attributes: { href: sanitizeUrl(options.image) }, }; } } return convert.js2xml(base, { compact: true, ignoreComment: true, spaces: 4 }); }; const formatEnclosure = (enclosure: string | Enclosure, mimeCategory = "image") => { if (typeof enclosure === "string") { const sanitizedUrl = sanitizeUrl(enclosure); const type = new URL(sanitizedUrl).pathname.split(".").slice(-1)[0]; return { _attributes: { url: sanitizedUrl, length: 0, type: `${mimeCategory}/${type}` } }; } const sanitizedUrl = sanitizeUrl(enclosure.url); const type = new URL(sanitizedUrl).pathname.split(".").slice(-1)[0]; return { _attributes: { length: 0, type: `${mimeCategory}/${type}`, ...enclosure, url: sanitizedUrl } }; }; const formatCategory = (category: Category) => { const { name, domain } = category; return { _text: name, _attributes: { domain, }, }; }; const formatDuration = (duration: number) => { const seconds = duration % 60; const totalMinutes = Math.floor(duration / 60); const minutes = totalMinutes % 60; const hours = Math.floor(totalMinutes / 60); const notHours = `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`; return hours > 0 ? `${hours}:${notHours}` : notHours; };