From 6f3fa1d3fcf68824748bcb2cf22328dc7788d493 Mon Sep 17 00:00:00 2001 From: Volker Andres Date: Thu, 21 Oct 2021 19:37:29 +0200 Subject: [PATCH] implemented `lastmodDateOnly` for `SitemapIndexStream`, fixes #356 --- README.md | 1 + lib/sitemap-index-stream.ts | 9 ++++++++- tests/sitemap-index.test.ts | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5c3d6257..9433bed7 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ const { const sms = new SitemapAndIndexStream({ limit: 50000, // defaults to 45k + lastmodDateOnly: false, // print date not time // SitemapAndIndexStream will call this user provided function every time // it needs to create a new sitemap file. You merely need to return a stream // for it to write the sitemap urls to and the expected url where that sitemap will be hosted diff --git a/lib/sitemap-index-stream.ts b/lib/sitemap-index-stream.ts index 3bf16564..8843d8a9 100644 --- a/lib/sitemap-index-stream.ts +++ b/lib/sitemap-index-stream.ts @@ -17,11 +17,13 @@ const sitemapIndexTagStart = const closetag = ''; export interface SitemapIndexStreamOptions extends TransformOptions { + lastmodDateOnly?: boolean; level?: ErrorLevel; xslUrl?: string; } const defaultStreamOpts: SitemapIndexStreamOptions = {}; export class SitemapIndexStream extends Transform { + lastmodDateOnly: boolean; level: ErrorLevel; xslUrl?: string; private hasHeadOutput: boolean; @@ -29,6 +31,7 @@ export class SitemapIndexStream extends Transform { opts.objectMode = true; super(opts); this.hasHeadOutput = false; + this.lastmodDateOnly = opts.lastmodDateOnly || false; this.level = opts.level ?? ErrorLevel.WARN; this.xslUrl = opts.xslUrl; } @@ -52,8 +55,12 @@ export class SitemapIndexStream extends Transform { } else { this.push(element(IndexTagNames.loc, item.url)); if (item.lastmod) { + const lastmod: string = new Date(item.lastmod).toISOString(); this.push( - element(IndexTagNames.lastmod, new Date(item.lastmod).toISOString()) + element( + IndexTagNames.lastmod, + this.lastmodDateOnly ? lastmod.slice(0, 10) : lastmod + ) ); } } diff --git a/tests/sitemap-index.test.ts b/tests/sitemap-index.test.ts index 81d31ced..64937b74 100644 --- a/tests/sitemap-index.test.ts +++ b/tests/sitemap-index.test.ts @@ -81,6 +81,42 @@ describe('sitemapIndex', () => { expect(result.toString()).toBe(expectedResult); }); + + it('build sitemap index with lastmodDateOnly', async () => { + const expectedResult = + xmlDef + + '' + + '' + + 'https://test.com/s1.xml' + + '2018-11-26' + + '' + + '' + + 'https://test.com/s2.xml' + + '2018-11-27' + + '' + + '' + + 'https://test.com/s3.xml' + + '' + + ''; + const smis = new SitemapIndexStream({ lastmodDateOnly: true }); + smis.write({ + url: 'https://test.com/s1.xml', + lastmod: '2018-11-26T00:00:00.000Z', + }); + + smis.write({ + url: 'https://test.com/s2.xml', + lastmod: '2018-11-27T00:00:00.000Z', + }); + + smis.write({ + url: 'https://test.com/s3.xml', + }); + smis.end(); + const result = await streamToPromise(smis); + + expect(result.toString()).toBe(expectedResult); + }); }); describe('sitemapAndIndex', () => {