UNPKG

14.6 kBMarkdownView Raw
1# API
2
3- [API](#api)
4 - [SitemapStream](#sitemapstream)
5 - [XMLToSitemapItemStream](#xmltositemapitemstream)
6 - [sitemapAndIndexStream](#sitemapandindexstream)
7 - [createSitemapsAndIndex](#createsitemapsandindex)
8 - [SitemapIndexStream](#sitemapindexstream)
9 - [xmlLint](#xmllint)
10 - [parseSitemap](#parsesitemap)
11 - [lineSeparatedURLsToSitemapOptions](#lineseparatedurlstositemapoptions)
12 - [streamToPromise](#streamtopromise)
13 - [ObjectStreamToJSON](#objectstreamtojson)
14 - [SitemapItemStream](#sitemapitemstream)
15 - [Sitemap Item Options](#sitemap-item-options)
16 - [SitemapImage](#sitemapimage)
17 - [VideoItem](#videoitem)
18 - [ILinkItem](#ilinkitem)
19 - [NewsItem](#newsitem)
20
21## SitemapStream
22
23A [Transform](https://nodejs.org/api/stream.html#stream_implementing_a_transform_stream) for turning a [Readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of either [SitemapItemOptions](#sitemap-item-options) or url strings into a Sitemap. The readable stream it transforms **must** be in object mode.
24
25```javascript
26const { SitemapStream } = require('sitemap')
27const sms = new SitemapStream({
28 hostname: 'https://example.com', // optional only necessary if your paths are relative
29 lastmodDateOnly: false // defaults to false, flip to true for baidu
30 xmlns: { // XML namespaces to turn on - all by default
31 news: true,
32 xhtml: true,
33 image: true,
34 video: true,
35 // custom: ['xmlns:custom="https://example.com"']
36 },
37 errorHandler: undefined // defaults to a standard errorLogger that logs to console or throws if the errorLevel is set to throw
38})
39const readable = // a readable stream of objects
40readable.pipe(sms).pipe(process.stdout)
41```
42
43## XMLToSitemapItemStream
44
45Takes a stream of xml and transforms it into a stream of SitemapOptions.
46Use this to parse existing sitemaps into config options compatible with this library
47
48```javascript
49const { createReadStream, createWriteStream } = require('fs');
50const { XMLToSitemapItemStream, ObjectStreamToJSON } = require('sitemap');
51
52createReadStream('./some/sitemap.xml')
53// turn the xml into sitemap option item options
54.pipe(new XMLToSitemapItemStream({
55 // optional
56 level: ErrorLevel.Warn // default is WARN pass Silent to silence
57 logger: false // default is console log, pass false as another way to silence or your own custom logger
58}))
59// convert the object stream to JSON
60.pipe(new ObjectStreamToJSON())
61// write the library compatible options to disk
62.pipe(createWriteStream('./sitemapOptions.json'))
63```
64
65## sitemapAndIndexStream
66
67Use this to take a stream which may go over the max of 50000 items and split it into an index and sitemaps.
68SitemapAndIndexStream consumes a stream of urls and streams out index entries while writing individual urls to the streams you give it.
69Provide it with a function which when provided with a index returns a url where the sitemap will ultimately be hosted and a stream to write the current sitemap to. This function will be called everytime the next item in the stream would exceed the provided limit.
70
71```js
72const { createReadStream, createWriteStream } = require('fs');
73const { resolve } = require('path');
74const { createGzip } = require('zlib')
75const {
76 SitemapAndIndexStream,
77 SitemapStream,
78 lineSeparatedURLsToSitemapOptions
79} = require('sitemap')
80
81const sms = new SitemapAndIndexStream({
82 limit: 10000, // defaults to 45k
83 // SitemapAndIndexStream will call this user provided function every time
84 // it needs to create a new sitemap file. You merely need to return a stream
85 // for it to write the sitemap urls to and the expected url where that sitemap will be hosted
86 getSitemapStream: (i) => {
87 const sitemapStream = new SitemapStream();
88 const path = `./sitemap-${i}.xml`;
89
90 const ws = sitemapStream
91 .pipe(createGzip()) // compress the output of the sitemap
92 .pipe(createWriteStream(resolve(path + '.gz'))); // write it to sitemap-NUMBER.xml
93
94 return [new URL(path, 'https://example.com/subdir/').toString(), sitemapStream, ws];
95 },
96});
97
98lineSeparatedURLsToSitemapOptions(
99 createReadStream('./your-data.json.txt')
100)
101.pipe(sms)
102.pipe(createGzip())
103.pipe(createWriteStream(resolve('./sitemap-index.xml.gz')));
104```
105
106## SitemapIndexStream
107
108Writes a sitemap index when given a stream urls.
109
110```js
111/**
112 * writes the following
113 * <?xml version="1.0" encoding="UTF-8"?>
114 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
115 <sitemap>
116 <loc>https://example.com/</loc>
117 </sitemap>
118 <sitemap>
119 <loc>https://example.com/2</loc>
120 </sitemap>
121 */
122const smis = new SitemapIndexStream({level: 'warn'})
123smis.write({url: 'https://example.com/'})
124smis.write({url: 'https://example.com/2'})
125smis.pipe(writestream)
126smis.end()
127```
128
129## xmlLint
130
131Resolve or reject depending on whether the passed in xml is a valid sitemap.
132This is just a wrapper around the xmlLint command line tool and thus requires
133xmlLint.
134
135```js
136const { createReadStream } = require('fs')
137const { xmlLint } = require('sitemap')
138xmlLint(createReadStream('./example.xml')).then(
139 () => console.log('xml is valid'),
140 ([err, stderr]) => console.error('xml is invalid', stderr)
141)
142```
143
144## parseSitemap
145
146Read xml and resolve with the configuration that would produce it or reject with
147an error
148
149```js
150const { createReadStream } = require('fs')
151const { parseSitemap, createSitemap } = require('sitemap')
152parseSitemap(createReadStream('./example.xml')).then(
153 // produces the same xml
154 // you can, of course, more practically modify it or store it
155 (xmlConfig) => console.log(createSitemap(xmlConfig).toString()),
156 (err) => console.log(err)
157)
158```
159
160## lineSeparatedURLsToSitemapOptions
161
162Takes a stream of urls or sitemapoptions likely from fs.createReadStream('./path') and returns an object stream of sitemap items.
163
164## streamToPromise
165
166Takes a stream returns a promise that resolves when stream emits finish.
167
168```javascript
169const { streamToPromise, SitemapStream } = require('sitemap')
170const sitemap = new SitemapStream({ hostname: 'http://example.com' });
171sitemap.write({ url: '/page-1/', changefreq: 'daily', priority: 0.3 })
172sitemap.end()
173streamToPromise(sitemap).then(buffer => console.log(buffer.toString())) // emits the full sitemap
174```
175
176## ObjectStreamToJSON
177
178A Transform that converts a stream of objects into a JSON Array or a line separated stringified JSON.
179
180- @param [lineSeparated=false] whether to separate entries by a new line or comma
181
182```javascript
183const stream = Readable.from([{a: 'b'}])
184 .pipe(new ObjectStreamToJSON())
185 .pipe(process.stdout)
186stream.end()
187// prints {"a":"b"}
188```
189
190## SitemapItemStream
191
192Takes a stream of SitemapItemOptions and spits out xml for each
193
194```js
195// writes <url><loc>https://example.com</loc><url><url><loc>https://example.com/2</loc><url>
196const smis = new SitemapItemStream({level: 'warn'})
197smis.pipe(writestream)
198smis.write({url: 'https://example.com', img: [], video: [], links: []})
199smis.write({url: 'https://example.com/2', img: [], video: [], links: []})
200smis.end()
201```
202
203## Sitemap Item Options
204
205|Option|Type|eg|Description|
206|------|----|--|-----------|
207|url|string|`http://example.com/some/path`|The only required property for every sitemap entry|
208|lastmod|string|'2019-07-29' or '2019-07-22T05:58:37.037Z'|When the page we as last modified use the W3C Datetime ISO8601 subset <https://www.sitemaps.org/protocol.html#xmlTagDefinitions>|
209|changefreq|string|'weekly'|How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page. Please note that the value of this tag is considered a hint and not a command. See <https://www.sitemaps.org/protocol.html#xmlTagDefinitions> for the acceptable values|
210|priority|number|0.6|The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0. This value does not affect how your pages are compared to pages on other sites—it only lets the search engines know which pages you deem most important for the crawlers. The default priority of a page is 0.5. <https://www.sitemaps.org/protocol.html#xmlTagDefinitions>|
211|img|object[]|see [#ISitemapImage](#ISitemapImage)|<https://support.google.com/webmasters/answer/178636?hl=en&ref_topic=4581190>|
212|video|object[]|see [#IVideoItem](#IVideoItem)|<https://support.google.com/webmasters/answer/80471?hl=en&ref_topic=4581190>|
213|links|object[]|see [#ILinkItem](#ILinkItem)|Tell search engines about localized versions <https://support.google.com/webmasters/answer/189077>|
214|news|object|see [#INewsItem](#INewsItem)|<https://support.google.com/webmasters/answer/74288?hl=en&ref_topic=4581190>|
215|ampLink|string|`http://ampproject.org/article.amp.html`||
216|cdata|boolean|true|wrap url in cdata xml escape|
217
218## SitemapImage
219
220Sitemap image
221<https://support.google.com/webmasters/answer/178636?hl=en&ref_topic=4581190>
222
223|Option|Type|eg|Description|
224|------|----|--|-----------|
225|url|string|`http://example.com/image.jpg`|The URL of the image.|
226|caption|string - optional|'Here we did the stuff'|The caption of the image.|
227|title|string - optional|'Star Wars EP IV'|The title of the image.|
228|geoLocation|string - optional|'Limerick, Ireland'|The geographic location of the image.|
229|license|string - optional|`http://example.com/license.txt`|A URL to the license of the image.|
230
231## VideoItem
232
233Sitemap video. <https://support.google.com/webmasters/answer/80471?hl=en&ref_topic=4581190>
234
235|Option|Type|eg|Description|
236|------|----|--|-----------|
237|thumbnail_loc|string|`"https://rtv3-img-roosterteeth.akamaized.net/store/0e841100-289b-4184-ae30-b6a16736960a.jpg/sm/thumb3.jpg"`|A URL pointing to the video thumbnail image file|
238|title|string|'2018:E6 - GoldenEye: Source'|The title of the video. |
239|description|string|'We play gun game in GoldenEye: Source with a good friend of ours. His name is Gruchy. Dan Gruchy.'|A description of the video. Maximum 2048 characters. |
240|content_loc|string - optional|`"http://streamserver.example.com/video123.mp4"`|A URL pointing to the actual video media file. Should be one of the supported formats. HTML is not a supported format. Flash is allowed, but no longer supported on most mobile platforms, and so may be indexed less well. Must not be the same as the `<loc>` URL.|
241|player_loc|string - optional|`"https://roosterteeth.com/embed/rouletsplay-2018-goldeneye-source"`|A URL pointing to a player for a specific video. Usually this is the information in the src element of an `<embed>` tag. Must not be the same as the `<loc>` URL|
242|'player_loc:autoplay'|string - optional|'ap=1'|a string the search engine can append as a query param to enable automatic playback|
243|'player_loc:allow_embed'|boolean - optional|'yes'|Whether the search engine can embed the video in search results. Allowed values are yes or no.|
244|duration|number - optional| 600| duration of video in seconds|
245|expiration_date| string - optional|"2012-07-16T19:20:30+08:00"|The date after which the video will no longer be available|
246|view_count|number - optional|'21000000000'|The number of times the video has been viewed.|
247|publication_date| string - optional|"2018-04-27T17:00:00.000Z"|The date the video was first published, in W3C format.|
248|category|string - optional|"Baking"|A short description of the broad category that the video belongs to. This is a string no longer than 256 characters.|
249|restriction|string - optional|"IE GB US CA"|Whether to show or hide your video in search results from specific countries.|
250|restriction:relationship| string - optional|"deny"||
251|gallery_loc| string - optional|`"https://roosterteeth.com/series/awhu"`|Currently not used.|
252|gallery_loc:title|string - optional|"awhu series page"|Currently not used.|
253|price|string - optional|"1.99"|The price to download or view the video. Omit this tag for free videos.|
254|price:resolution|string - optional|"HD"|Specifies the resolution of the purchased version. Supported values are hd and sd.|
255|price:currency| string - optional|"USD"|currency [Required] Specifies the currency in ISO 4217 format.|
256|price:type|string - optional|"rent"|type [Optional] Specifies the purchase option. Supported values are rent and own. |
257|uploader|string - optional|"GrillyMcGrillerson"|The video uploader's name. Only one <video:uploader> is allowed per video. String value, max 255 characters.|
258|platform|string - optional|"tv"|Whether to show or hide your video in search results on specified platform types. This is a list of space-delimited platform types. See <https://support.google.com/webmasters/answer/80471?hl=en&ref_topic=4581190> for more detail|
259|platform:relationship|string 'Allow'\|'Deny' - optional|'Allow'||
260|id|string - optional|||
261|tag|string[] - optional|['Baking']|An arbitrary string tag describing the video. Tags are generally very short descriptions of key concepts associated with a video or piece of content.|
262|rating|number - optional|2.5|The rating of the video. Supported values are float numbers|
263|family_friendly|string 'YES'\|'NO' - optional|'YES'||
264|requires_subscription|string 'YES'\|'NO' - optional|'YES'|Indicates whether a subscription (either paid or free) is required to view the video. Allowed values are yes or no.|
265|live|string 'YES'\|'NO' - optional|'NO'|Indicates whether the video is a live stream. Supported values are yes or no.|
266
267## ILinkItem
268
269<https://support.google.com/webmasters/answer/189077>
270
271|Option|Type|eg|Description|
272|------|----|--|-----------|
273|lang|string|'en'||
274|url|string|`'http://example.com/en/'`||
275
276## NewsItem
277
278<https://support.google.com/webmasters/answer/74288?hl=en&ref_topic=4581190>
279
280|Option|Type|eg|Description|
281|------|----|--|-----------|
282|access|string - 'Registration' \| 'Subscription'| 'Registration' - optional||
283|publication| object|see following options||
284|publication['name']| string|'The Example Times'|The `<name>` is the name of the news publication. It must exactly match the name as it appears on your articles on news.google.com, except for anything in parentheses.|
285|publication['language']|string|'en'|The `<language>` is the language of your publication. Use an ISO 639 language code (2 or 3 letters).|
286|genres|string - optional|'PressRelease, Blog'||
287|publication_date|string|'2008-12-23'|Article publication date in W3C format, using either the "complete date" (YYYY-MM-DD) format or the "complete date plus hours, minutes, and seconds"|
288|title|string|'Companies A, B in Merger Talks'|The title of the news article.|
289|keywords|string - optional|"business, merger, acquisition, A, B"||
290|stock_tickers|string - optional|"NASDAQ:A, NASDAQ:B"||