UNPKG

9.33 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.SitemapItemStream = void 0;
4const stream_1 = require("stream");
5const errors_1 = require("./errors");
6const types_1 = require("./types");
7const sitemap_xml_1 = require("./sitemap-xml");
8function attrBuilder(conf, keys) {
9 if (typeof keys === 'string') {
10 keys = [keys];
11 }
12 const iv = {};
13 return keys.reduce((attrs, key) => {
14 // eslint-disable-next-line
15 if (conf[key] !== undefined) {
16 const keyAr = key.split(':');
17 if (keyAr.length !== 2) {
18 throw new errors_1.InvalidAttr(key);
19 }
20 attrs[keyAr[1]] = conf[key];
21 }
22 return attrs;
23 }, iv);
24}
25/**
26 * Takes a stream of SitemapItemOptions and spits out xml for each
27 * @example
28 * // writes <url><loc>https://example.com</loc><url><url><loc>https://example.com/2</loc><url>
29 * const smis = new SitemapItemStream({level: 'warn'})
30 * smis.pipe(writestream)
31 * smis.write({url: 'https://example.com', img: [], video: [], links: []})
32 * smis.write({url: 'https://example.com/2', img: [], video: [], links: []})
33 * smis.end()
34 * @param level - Error level
35 */
36class SitemapItemStream extends stream_1.Transform {
37 constructor(opts = { level: types_1.ErrorLevel.WARN }) {
38 opts.objectMode = true;
39 super(opts);
40 this.level = opts.level || types_1.ErrorLevel.WARN;
41 }
42 _transform(item, encoding, callback) {
43 this.push(sitemap_xml_1.otag(types_1.TagNames.url));
44 this.push(sitemap_xml_1.element(types_1.TagNames.loc, item.url));
45 if (item.lastmod) {
46 this.push(sitemap_xml_1.element(types_1.TagNames.lastmod, item.lastmod));
47 }
48 if (item.changefreq) {
49 this.push(sitemap_xml_1.element(types_1.TagNames.changefreq, item.changefreq));
50 }
51 if (item.priority !== undefined) {
52 if (item.fullPrecisionPriority) {
53 this.push(sitemap_xml_1.element(types_1.TagNames.priority, item.priority.toString()));
54 }
55 else {
56 this.push(sitemap_xml_1.element(types_1.TagNames.priority, item.priority.toFixed(1)));
57 }
58 }
59 item.video.forEach((video) => {
60 this.push(sitemap_xml_1.otag(types_1.TagNames['video:video']));
61 this.push(sitemap_xml_1.element(types_1.TagNames['video:thumbnail_loc'], video.thumbnail_loc));
62 this.push(sitemap_xml_1.element(types_1.TagNames['video:title'], video.title));
63 this.push(sitemap_xml_1.element(types_1.TagNames['video:description'], video.description));
64 if (video.content_loc) {
65 this.push(sitemap_xml_1.element(types_1.TagNames['video:content_loc'], video.content_loc));
66 }
67 if (video.player_loc) {
68 this.push(sitemap_xml_1.element(types_1.TagNames['video:player_loc'], attrBuilder(video, [
69 'player_loc:autoplay',
70 'player_loc:allow_embed',
71 ]), video.player_loc));
72 }
73 if (video.duration) {
74 this.push(sitemap_xml_1.element(types_1.TagNames['video:duration'], video.duration.toString()));
75 }
76 if (video.expiration_date) {
77 this.push(sitemap_xml_1.element(types_1.TagNames['video:expiration_date'], video.expiration_date));
78 }
79 if (video.rating !== undefined) {
80 this.push(sitemap_xml_1.element(types_1.TagNames['video:rating'], video.rating.toString()));
81 }
82 if (video.view_count !== undefined) {
83 this.push(sitemap_xml_1.element(types_1.TagNames['video:view_count'], video.view_count.toString()));
84 }
85 if (video.publication_date) {
86 this.push(sitemap_xml_1.element(types_1.TagNames['video:publication_date'], video.publication_date));
87 }
88 for (const tag of video.tag) {
89 this.push(sitemap_xml_1.element(types_1.TagNames['video:tag'], tag));
90 }
91 if (video.category) {
92 this.push(sitemap_xml_1.element(types_1.TagNames['video:category'], video.category));
93 }
94 if (video.family_friendly) {
95 this.push(sitemap_xml_1.element(types_1.TagNames['video:family_friendly'], video.family_friendly));
96 }
97 if (video.restriction) {
98 this.push(sitemap_xml_1.element(types_1.TagNames['video:restriction'], attrBuilder(video, 'restriction:relationship'), video.restriction));
99 }
100 if (video.gallery_loc) {
101 this.push(sitemap_xml_1.element(types_1.TagNames['video:gallery_loc'], { title: video['gallery_loc:title'] }, video.gallery_loc));
102 }
103 if (video.price) {
104 this.push(sitemap_xml_1.element(types_1.TagNames['video:price'], attrBuilder(video, [
105 'price:resolution',
106 'price:currency',
107 'price:type',
108 ]), video.price));
109 }
110 if (video.requires_subscription) {
111 this.push(sitemap_xml_1.element(types_1.TagNames['video:requires_subscription'], video.requires_subscription));
112 }
113 if (video.uploader) {
114 this.push(sitemap_xml_1.element(types_1.TagNames['video:uploader'], attrBuilder(video, 'uploader:info'), video.uploader));
115 }
116 if (video.platform) {
117 this.push(sitemap_xml_1.element(types_1.TagNames['video:platform'], attrBuilder(video, 'platform:relationship'), video.platform));
118 }
119 if (video.live) {
120 this.push(sitemap_xml_1.element(types_1.TagNames['video:live'], video.live));
121 }
122 if (video.id) {
123 this.push(sitemap_xml_1.element(types_1.TagNames['video:id'], { type: 'url' }, video.id));
124 }
125 this.push(sitemap_xml_1.ctag(types_1.TagNames['video:video']));
126 });
127 item.links.forEach((link) => {
128 this.push(sitemap_xml_1.element(types_1.TagNames['xhtml:link'], {
129 rel: 'alternate',
130 hreflang: link.lang || link.hreflang,
131 href: link.url,
132 }));
133 });
134 if (item.expires) {
135 this.push(sitemap_xml_1.element(types_1.TagNames.expires, new Date(item.expires).toISOString()));
136 }
137 if (item.androidLink) {
138 this.push(sitemap_xml_1.element(types_1.TagNames['xhtml:link'], {
139 rel: 'alternate',
140 href: item.androidLink,
141 }));
142 }
143 if (item.ampLink) {
144 this.push(sitemap_xml_1.element(types_1.TagNames['xhtml:link'], {
145 rel: 'amphtml',
146 href: item.ampLink,
147 }));
148 }
149 if (item.news) {
150 this.push(sitemap_xml_1.otag(types_1.TagNames['news:news']));
151 this.push(sitemap_xml_1.otag(types_1.TagNames['news:publication']));
152 this.push(sitemap_xml_1.element(types_1.TagNames['news:name'], item.news.publication.name));
153 this.push(sitemap_xml_1.element(types_1.TagNames['news:language'], item.news.publication.language));
154 this.push(sitemap_xml_1.ctag(types_1.TagNames['news:publication']));
155 if (item.news.access) {
156 this.push(sitemap_xml_1.element(types_1.TagNames['news:access'], item.news.access));
157 }
158 if (item.news.genres) {
159 this.push(sitemap_xml_1.element(types_1.TagNames['news:genres'], item.news.genres));
160 }
161 this.push(sitemap_xml_1.element(types_1.TagNames['news:publication_date'], item.news.publication_date));
162 this.push(sitemap_xml_1.element(types_1.TagNames['news:title'], item.news.title));
163 if (item.news.keywords) {
164 this.push(sitemap_xml_1.element(types_1.TagNames['news:keywords'], item.news.keywords));
165 }
166 if (item.news.stock_tickers) {
167 this.push(sitemap_xml_1.element(types_1.TagNames['news:stock_tickers'], item.news.stock_tickers));
168 }
169 this.push(sitemap_xml_1.ctag(types_1.TagNames['news:news']));
170 }
171 // Image handling
172 item.img.forEach((image) => {
173 this.push(sitemap_xml_1.otag(types_1.TagNames['image:image']));
174 this.push(sitemap_xml_1.element(types_1.TagNames['image:loc'], image.url));
175 if (image.caption) {
176 this.push(sitemap_xml_1.element(types_1.TagNames['image:caption'], image.caption));
177 }
178 if (image.geoLocation) {
179 this.push(sitemap_xml_1.element(types_1.TagNames['image:geo_location'], image.geoLocation));
180 }
181 if (image.title) {
182 this.push(sitemap_xml_1.element(types_1.TagNames['image:title'], image.title));
183 }
184 if (image.license) {
185 this.push(sitemap_xml_1.element(types_1.TagNames['image:license'], image.license));
186 }
187 this.push(sitemap_xml_1.ctag(types_1.TagNames['image:image']));
188 });
189 this.push(sitemap_xml_1.ctag(types_1.TagNames.url));
190 callback();
191 }
192}
193exports.SitemapItemStream = SitemapItemStream;