@bliztek/feed-generator
Version:
A simple and lightweight Node.js library for generating RSS 2.0, Atom, and JSON Feed formats.
131 lines (130 loc) • 3.29 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FeedBuilder = void 0;
const index_js_1 = require("./index.js");
/**
* Fluent builder for constructing feeds incrementally.
*
* @example
* ```ts
* const rss = new FeedBuilder()
* .title("My Blog")
* .link("https://example.com")
* .description("Latest posts.")
* .addItem({ id: "1", title: "Hello", content: "<p>World</p>" })
* .generate("rss");
* ```
*/
class FeedBuilder {
constructor() {
this.feed = { items: [] };
this.opts = {};
}
title(title) {
this.feed.title = title;
return this;
}
link(link) {
this.feed.link = link;
return this;
}
description(description) {
this.feed.description = description;
return this;
}
id(id) {
this.feed.id = id;
return this;
}
language(language) {
this.feed.language = language;
return this;
}
copyright(copyright) {
this.feed.copyright = copyright;
return this;
}
updated(updated) {
this.feed.updated = updated;
return this;
}
generator(generator) {
this.feed.generator = generator;
return this;
}
image(image) {
this.feed.image = image;
return this;
}
favicon(favicon) {
this.feed.favicon = favicon;
return this;
}
author(author) {
var _a;
this.feed.authors = [...((_a = this.feed.authors) !== null && _a !== void 0 ? _a : []), author];
return this;
}
category(category) {
var _a;
this.feed.categories = [...((_a = this.feed.categories) !== null && _a !== void 0 ? _a : []), category];
return this;
}
nextUrl(nextUrl) {
this.feed.nextUrl = nextUrl;
return this;
}
podcast(podcast) {
this.feed.podcast = podcast;
return this;
}
ttl(ttl) {
this.feed.ttl = ttl;
return this;
}
skipHours(hours) {
this.feed.skipHours = hours;
return this;
}
skipDays(days) {
this.feed.skipDays = days;
return this;
}
feedLinks(links) {
this.feed.feedLinks = links;
return this;
}
hub(url) {
this.feed.feedLinks = Object.assign(Object.assign({}, this.feed.feedLinks), { hub: url });
return this;
}
extensions(ext) {
this.feed.extensions = ext;
return this;
}
stylesheet(url) {
this.opts.stylesheet = url;
return this;
}
addItem(item) {
this.feed.items.push(item);
return this;
}
addItems(items) {
this.feed.items.push(...items);
return this;
}
/** Build the feed data object without rendering. */
build() {
return this.feed;
}
/** Render the feed in a single format. */
generate(format, options) {
return (0, index_js_1.generateFeed)(this.feed, format, Object.assign(Object.assign({}, this.opts), options));
}
/** Render all three formats at once. */
generateAll(options) {
return (0, index_js_1.generateFeeds)(this.feed, Object.assign(Object.assign({}, this.opts), options));
}
}
exports.FeedBuilder = FeedBuilder;