1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var convert = require("xml-js");
|
4 | var config_1 = require("./config");
|
5 | var utils_1 = require("./utils");
|
6 | exports.default = (function (ins) {
|
7 | var options = ins.options;
|
8 | var base = {
|
9 | _declaration: { _attributes: { version: "1.0", encoding: "utf-8" } },
|
10 | feed: {
|
11 | _attributes: { xmlns: "http://www.w3.org/2005/Atom" },
|
12 | id: options.id,
|
13 | title: options.title,
|
14 | updated: options.updated ? options.updated.toISOString() : new Date().toISOString(),
|
15 | generator: utils_1.sanitize(options.generator || config_1.generator)
|
16 | }
|
17 | };
|
18 | if (options.author) {
|
19 | base.feed.author = formatAuthor(options.author);
|
20 | }
|
21 | base.feed.link = [];
|
22 | if (options.link) {
|
23 | base.feed.link.push({ _attributes: { rel: "alternate", href: utils_1.sanitize(options.link) } });
|
24 | }
|
25 | var atomLink = utils_1.sanitize(options.feed || (options.feedLinks && options.feedLinks.atom));
|
26 | if (atomLink) {
|
27 | base.feed.link.push({ _attributes: { rel: "self", href: utils_1.sanitize(atomLink) } });
|
28 | }
|
29 | if (options.hub) {
|
30 | base.feed.link.push({ _attributes: { rel: "hub", href: utils_1.sanitize(options.hub) } });
|
31 | }
|
32 | if (options.description) {
|
33 | base.feed.subtitle = options.description;
|
34 | }
|
35 | if (options.image) {
|
36 | base.feed.logo = options.image;
|
37 | }
|
38 | if (options.favicon) {
|
39 | base.feed.icon = options.favicon;
|
40 | }
|
41 | if (options.copyright) {
|
42 | base.feed.rights = options.copyright;
|
43 | }
|
44 | base.feed.category = [];
|
45 | ins.categories.map(function (category) {
|
46 | base.feed.category.push({ _attributes: { term: category } });
|
47 | });
|
48 | base.feed.contributor = [];
|
49 | ins.contributors.map(function (contributor) {
|
50 | base.feed.contributor.push(formatAuthor(contributor));
|
51 | });
|
52 | base.feed.entry = [];
|
53 | ins.items.map(function (item) {
|
54 | var entry = {
|
55 | title: { _attributes: { type: "html" }, _cdata: item.title },
|
56 | id: utils_1.sanitize(item.id || item.link),
|
57 | link: [{ _attributes: { href: utils_1.sanitize(item.link) } }],
|
58 | updated: item.date.toISOString()
|
59 | };
|
60 | if (item.description) {
|
61 | entry.summary = {
|
62 | _attributes: { type: "html" },
|
63 | _cdata: item.description,
|
64 | };
|
65 | }
|
66 | if (item.content) {
|
67 | entry.content = {
|
68 | _attributes: { type: "html" },
|
69 | _cdata: item.content,
|
70 | };
|
71 | }
|
72 | if (Array.isArray(item.author)) {
|
73 | entry.author = [];
|
74 | item.author.map(function (author) {
|
75 | entry.author.push(formatAuthor(author));
|
76 | });
|
77 | }
|
78 | if (Array.isArray(item.category)) {
|
79 | entry.category = [];
|
80 | item.category.map(function (category) {
|
81 | entry.category.push(formatCategory(category));
|
82 | });
|
83 | }
|
84 | if (item.contributor && Array.isArray(item.contributor)) {
|
85 | entry.contributor = [];
|
86 | item.contributor.map(function (contributor) {
|
87 | entry.contributor.push(formatAuthor(contributor));
|
88 | });
|
89 | }
|
90 | if (item.published) {
|
91 | entry.published = item.published.toISOString();
|
92 | }
|
93 | if (item.copyright) {
|
94 | entry.rights = item.copyright;
|
95 | }
|
96 | base.feed.entry.push(entry);
|
97 | });
|
98 | return convert.js2xml(base, { compact: true, ignoreComment: true, spaces: 4 });
|
99 | });
|
100 | var formatAuthor = function (author) {
|
101 | var name = author.name, email = author.email, link = author.link;
|
102 | var out = { name: name };
|
103 | if (email) {
|
104 | out.email = email;
|
105 | }
|
106 | if (link) {
|
107 | out.uri = utils_1.sanitize(link);
|
108 | }
|
109 | return out;
|
110 | };
|
111 | var formatCategory = function (category) {
|
112 | var name = category.name, scheme = category.scheme, term = category.term;
|
113 | return {
|
114 | _attributes: {
|
115 | label: name,
|
116 | scheme: scheme,
|
117 | term: term,
|
118 | },
|
119 | };
|
120 | };
|
121 |
|
\ | No newline at end of file |