Skip to content

Commit 535c138

Browse files
Allow to pass multiple sitemaps
1 parent 2a72e1c commit 535c138

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ checkHttpStatus({
3939
'Accept': 'text/html',
4040
},
4141
},
42-
'sitemap': 'https://www.trunkcode.com/page-sitemap.xml',
42+
'sitemap': [
43+
'https://www.trunkcode.com/page-sitemap.xml',
44+
'https://www.trunkcode.com/post-sitemap.xml'
45+
],
4346
'skip200': true, // Do not report the URLs having HTTP code 200.
4447
});
4548
```

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const consoleColumns = require('./lib/console-columns');
4-
const fetchFromSitemap = require('./lib/fetch-from-sitemap');
4+
const fetchAllSitemaps = require('./lib/fetch-from-sitemap');
55
const fs = require('fs');
66
const generateExcel = require('./lib/generate-excel');
77
const httpList = require('./lib/http-list');
@@ -30,7 +30,7 @@ async function checkHttpStatus(config) {
3030
}
3131

3232
if (config.sitemap) {
33-
urlsList = await fetchFromSitemap(config.sitemap);
33+
urlsList = await fetchAllSitemaps(config.sitemap);
3434
} else if (config.urls && Array.isArray(config.urls)) {
3535
urlsList = config.urls;
3636
}

lib/fetch-from-sitemap.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const xml2js = require('xml2js');
77
* Fetch Sitemap URL and dumb all the URLs from the XML to a variable.
88
*/
99
function fetchFromSitemap(sitemapUrl) {
10-
var errorMessage = 'Error: Sitemap URL returns with status code ';
10+
var errorMessage = 'Error: ' + sitemapUrl + ' returns with status code ';
1111
var fetchedUrls = [];
1212

1313
return new Promise((resolve) => {
@@ -42,4 +42,27 @@ function fetchFromSitemap(sitemapUrl) {
4242
});
4343
}
4444

45-
module.exports = fetchFromSitemap;
45+
async function fetchAllSitemaps(sitemapUrls) {
46+
const sitemapUrlsLists = [];
47+
const urlsList = [];
48+
49+
var mergeLists = [];
50+
for (const sitemapUrl of sitemapUrls) {
51+
sitemapUrlsLists.push(fetchFromSitemap(sitemapUrl));
52+
}
53+
54+
const allUrls = await Promise.all(sitemapUrlsLists);
55+
for (const singleList of allUrls) {
56+
mergeLists = mergeLists.concat(singleList);
57+
}
58+
59+
for (const singleUrl of mergeLists) {
60+
if (!urlsList.includes(singleUrl)) {
61+
urlsList.push(singleUrl);
62+
}
63+
}
64+
65+
return urlsList;
66+
}
67+
68+
module.exports = fetchAllSitemaps;

test/sitemap.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ checkHttpStatus({
1616
'Accept': 'text/html',
1717
},
1818
},
19-
'sitemap': 'https://www.trunkcode.com/page-sitemap.xml',
19+
'sitemap': [
20+
'https://www.trunkcode.com/page-sitemap.xml',
21+
'https://www.trunkcode.com/post-sitemap.xml'
22+
],
2023
'skip200': true,
2124
});

0 commit comments

Comments
 (0)