Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented lastmodDateOnly for SitemapIndexStream, fixes #356 #373

Merged
merged 1 commit into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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