Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
seantomburke authored Feb 1, 2024
2 parents 432b7be + afa5741 commit a5d79fb
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ You can add options on the initial Sitemapper object when instantiating it.
+ `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
+ `proxyAgent`: (HttpProxyAgent|HttpsProxyAgent) - instance of npm "hpagent" HttpProxyAgent or HttpsProxyAgent to be passed to npm "got"
+ `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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sitemapper",
"version": "3.2.9",
"version": "3.2.10",
"description": "Parser for XML Sitemaps to be used with Robots.txt and web crawlers",
"keywords": [
"parse",
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
18 changes: 16 additions & 2 deletions src/assets/sitemapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default class Sitemapper {
this.rejectUnauthorized =
settings.rejectUnauthorized === false ? false : true;
this.proxyAgent = settings.proxyAgent || {};
this.fields = settings.fields || false;
}

/**
Expand Down Expand Up @@ -187,7 +188,7 @@ export default class Sitemapper {
https: {
rejectUnauthorized: this.rejectUnauthorized,
},
aget: this.proxyAgent
aget: this.proxyAgent,
};

try {
Expand Down Expand Up @@ -318,7 +319,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 a5d79fb

Please sign in to comment.