@bliztek/feed-generator
Version:
A simple and lightweight Node.js library for generating RSS 2.0, Atom, and JSON Feed formats.
69 lines (68 loc) • 2.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.nodes = exports.el = exports.xmlDeclaration = exports.renderXml = exports.escapeXml = void 0;
const escapeXml = (str) => str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
exports.escapeXml = escapeXml;
const renderAttributes = (attrs) => {
if (!attrs)
return "";
return Object.entries(attrs)
.filter(([, v]) => v !== undefined && v !== false)
.map(([k, v]) => ` ${k}="${(0, exports.escapeXml)(String(v))}"`)
.join("");
};
const renderXml = (xmlNodes, depth = 0, compact = false) => {
const ind = compact ? "" : " ".repeat(depth);
const nl = compact ? "" : "\n";
return xmlNodes
.map((node) => {
var _a;
if (typeof node === "string") {
return `${ind}${node}`;
}
const attrs = renderAttributes(node.attributes);
if (node.selfClosing) {
return `${ind}<${node.tag}${attrs} />`;
}
// Leaf node with text content (single string child)
if (((_a = node.children) === null || _a === void 0 ? void 0 : _a.length) === 1 &&
typeof node.children[0] === "string") {
const text = node.children[0];
if (node.cdata) {
// Escape ]]> sequences to prevent CDATA injection
const safe = text.replace(/\]\]>/g, "]]]]><![CDATA[>");
return `${ind}<${node.tag}${attrs}><![CDATA[${safe}]]></${node.tag}>`;
}
return `${ind}<${node.tag}${attrs}>${(0, exports.escapeXml)(text)}</${node.tag}>`;
}
// No children
if (!node.children || node.children.length === 0) {
return `${ind}<${node.tag}${attrs}></${node.tag}>`;
}
// Nested children
const inner = (0, exports.renderXml)(node.children, depth + 1, compact);
return `${ind}<${node.tag}${attrs}>${nl}${inner}${nl}${ind}</${node.tag}>`;
})
.join(nl);
};
exports.renderXml = renderXml;
const xmlDeclaration = () => `<?xml version="1.0" encoding="UTF-8"?>`;
exports.xmlDeclaration = xmlDeclaration;
// Helper to create a simple text element
const el = (tag, value, opts) => value !== undefined
? {
tag,
children: [value],
cdata: opts === null || opts === void 0 ? void 0 : opts.cdata,
attributes: opts === null || opts === void 0 ? void 0 : opts.attributes,
}
: null;
exports.el = el;
// Filter out nulls from a node list
const nodes = (...items) => items.filter((n) => n != null);
exports.nodes = nodes;