Skip to content

Commit

Permalink
Merge pull request #373 from vandres/master
Browse files Browse the repository at this point in the history
implemented `lastmodDateOnly` for `SitemapIndexStream`, fixes #356
  • Loading branch information
derduher authored Dec 31, 2021
2 parents daa2a94 + 6f3fa1d commit 768f05b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion lib/sitemap-index-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ const sitemapIndexTagStart =
const closetag = '</sitemapindex>';

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;
constructor(opts = defaultStreamOpts) {
opts.objectMode = true;
super(opts);
this.hasHeadOutput = false;
this.lastmodDateOnly = opts.lastmodDateOnly || false;
this.level = opts.level ?? ErrorLevel.WARN;
this.xslUrl = opts.xslUrl;
}
Expand All @@ -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
)
);
}
}
Expand Down
36 changes: 36 additions & 0 deletions tests/sitemap-index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,42 @@ describe('sitemapIndex', () => {

expect(result.toString()).toBe(expectedResult);
});

it('build sitemap index with lastmodDateOnly', async () => {
const expectedResult =
xmlDef +
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' +
'<sitemap>' +
'<loc>https://test.com/s1.xml</loc>' +
'<lastmod>2018-11-26</lastmod>' +
'</sitemap>' +
'<sitemap>' +
'<loc>https://test.com/s2.xml</loc>' +
'<lastmod>2018-11-27</lastmod>' +
'</sitemap>' +
'<sitemap>' +
'<loc>https://test.com/s3.xml</loc>' +
'</sitemap>' +
'</sitemapindex>';
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', () => {
Expand Down

0 comments on commit 768f05b

Please sign in to comment.