Skip to content

Commit

Permalink
Resolves request: Add option to parse lastmod and changefreq and prio…
Browse files Browse the repository at this point in the history
…rity (#135)
  • Loading branch information
tijevlam committed Feb 1, 2024
1 parent b7b07e5 commit 0ddd7e0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ You can add options on the initial Sitemapper object when instantiating it.
+ `retries`: (Number) - Sets the maximum number of retries to attempt in case of an error response (e.g. 404 or Timeout). Default: 0
+ `rejectUnauthorized`: (Boolean) - If true, it will throw on invalid certificates, such as expired or self-signed ones. Default: True
+ `lastmod`: (Number) - Timestamp of the minimum lastmod value allowed for returned urls
+ `field` : (Object) - An object of fields to be returned from the sitemap. For Example: `{ loc: true, lastmod: true, changefreq: true, priority: true }`. Leaving a field out has the same effect as `field: false`. If not specified sitemapper defaults to returning the 'classic' array of urls.

```javascript

Expand Down
1 change: 1 addition & 0 deletions sitemapper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface SitemapperOptions {
retries?: number;
timeout?: number;
url?: string;
fields?: {[name: string]: boolean};
}

declare class Sitemapper {
Expand Down
16 changes: 15 additions & 1 deletion src/assets/sitemapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default class Sitemapper {
this.retries = settings.retries || 0;
this.rejectUnauthorized =
settings.rejectUnauthorized === false ? false : true;
this.fields = settings.fields || false;
}

/**
Expand Down Expand Up @@ -315,7 +316,20 @@ export default class Sitemapper {

return modified >= this.lastmod;
})
.map((site) => site.loc && site.loc[0]);
.map((site) => {
if( !this.fields) {
return site.loc && site.loc[0];
} else {
let fields = {};
for (const [field, active] of Object.entries(this.fields)) {
if(active){
fields[field] = site[field][0]
}
}
return fields;
}
});

return {
sites,
errors: [],
Expand Down
39 changes: 39 additions & 0 deletions src/tests/test.ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ describe('Sitemapper', function () {
sitemapper.url = 1000;
sitemapper.url.should.equal(1000);
});

it('should construct with specific fields', () => {
sitemapper = new Sitemapper({
fields: { "loc": true,
"lastmod": true,
"priority": true,
"changefreq": true
}
});
sitemapper.fields.should.be.Object && sitemapper.fields.should.have.keys('loc', 'lastmod', 'priority', 'changefreq');
});

});

describe('fetch Method resolves sites to array', function () {
Expand Down Expand Up @@ -107,6 +119,33 @@ describe('Sitemapper', function () {
});
});

it('https://www.channable.com/sitemap.xml sitemaps should contain extra fields', function (done) {
this.timeout(30000);
const url = 'https://www.channable.com/sitemap.xml';
sitemapper = new Sitemapper({
fields: { "loc": true,
"lastmod": true,
"priority": true,
"changefreq": true
}
});
sitemapper.fetch(url)
.then(data => {
data.sites.should.be.Array;
data.url.should.equal(url);
data.sites.length.should.be.above(2);
data.sites[0].loc.should.be.String;
data.sites[0].lastmod.should.be.String;
data.sites[0].priority.should.be.String;
data.sites[0].changefreq.should.be.String;
done();
})
.catch(error => {
console.error('Test failed');
done(error);
});
});

it('https://www.golinks.io/sitemap.xml sitemaps should be an array', function (done) {
this.timeout(30000);
const url = 'https://www.golinks.io/sitemap.xml';
Expand Down

0 comments on commit 0ddd7e0

Please sign in to comment.