UNPKG

@bliztek/feed-generator

Version:

A simple and lightweight Node.js library for generating RSS 2.0, Atom, and JSON Feed formats.

21 lines (20 loc) 2.25 kB
const toIso = (date) => new Date(date).toISOString(); const clean = (obj) => Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== undefined && v !== null)); export const renderJSON = (feed, compact = false) => { var _a, _b, _c, _d, _e, _f; const result = clean(Object.assign({ version: "https://jsonfeed.org/version/1.1", title: feed.title, home_page_url: feed.link, feed_url: (_a = feed.feedLinks) === null || _a === void 0 ? void 0 : _a.json, next_url: feed.nextUrl, description: feed.description, icon: (_b = feed.image) === null || _b === void 0 ? void 0 : _b.url, favicon: feed.favicon, language: feed.language, authors: (_c = feed.authors) === null || _c === void 0 ? void 0 : _c.map((a) => clean({ name: a.name, url: a.url, avatar: a.avatar })), hubs: ((_d = feed.feedLinks) === null || _d === void 0 ? void 0 : _d.hub) ? [{ type: "WebSub", url: feed.feedLinks.hub }] : undefined, items: feed.items.map((item) => { var _a, _b, _c, _d, _e; return clean(Object.assign({ id: item.id, url: item.link, title: item.title, content_html: item.content, content_text: item.contentText, summary: item.summary, image: item.image, date_published: item.date ? toIso(item.date) : undefined, date_modified: item.updated ? toIso(item.updated) : undefined, authors: (_a = item.authors) === null || _a === void 0 ? void 0 : _a.map((a) => clean({ name: a.name, url: a.url, avatar: a.avatar })), tags: (_b = item.categories) === null || _b === void 0 ? void 0 : _b.map((c) => c.name), language: item.language, attachments: (_c = item.enclosures) === null || _c === void 0 ? void 0 : _c.map((e) => clean({ url: e.url, mime_type: e.type, title: e.title, size_in_bytes: e.length, duration_in_seconds: e.duration, })) }, ((_e = (_d = item.extensions) === null || _d === void 0 ? void 0 : _d.jsonProperties) !== null && _e !== void 0 ? _e : {}))); }) }, ((_f = (_e = feed.extensions) === null || _e === void 0 ? void 0 : _e.jsonProperties) !== null && _f !== void 0 ? _f : {}))); return compact ? JSON.stringify(result) : JSON.stringify(result, null, 2); };