Skip to content

Commit e260967

Browse files
ataylormedanreeves
authored andcommitted
Fix missing reference url slash (#26)
1 parent e6ecdd8 commit e260967

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

index.js

-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ if (cli.flags.limitSimilar) {
5050
}
5151

5252
if (cli.flags.referenceUrl) {
53-
// Remove a trailing slash so we don't end up with multiple later
54-
cli.flags.referenceUrl = cli.flags.referenceUrl.replace(/\/$/, '')
5553

5654
if (!validurl(cli.flags.referenceUrl)) {
5755
console.error(

lib/crawl.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const chalk = require('chalk');
99
const mkpath = require('mkpath');
1010
const jsonfile = require('jsonfile');
1111
const cheerio = require('cheerio');
12+
const normalurl = require('normalize-url');
1213
const limitSimilar = require('./limit-similar');
1314

1415
const dirname = path.dirname;
@@ -99,11 +100,22 @@ function crawl(url, flags) {
99100
// Clone the default scenario and adjust urls as needed
100101
const currentScenario = Object.assign({}, defaultScenario);
101102
currentScenario.label = queueItem.path;
103+
currentScenario.url = queueItem.url;
104+
102105
if (flags.referenceUrl) {
103-
currentScenario.referenceUrl = queueItem.url.replace(
104-
url,
105-
flags.referenceUrl
106+
// Normalize the URL to remove duplicate slashes. See #25
107+
currentScenario.referenceUrl = normalurl(
108+
queueItem.url.replace(
109+
url,
110+
flags.referenceUrl
111+
)
106112
);
113+
114+
// Add a trailing slash to referenceUrl if url has a trailing slash and referenceUrl does not
115+
if ( currentScenario.url.substr(-1) === '/' && currentScenario.referenceUrl.substr(-1) !== '/' ) {
116+
currentScenario.referenceUrl += '/';
117+
}
118+
107119
} else if (
108120
Object.prototype.hasOwnProperty.call(
109121
currentScenario,
@@ -112,7 +124,7 @@ function crawl(url, flags) {
112124
) {
113125
delete currentScenario.referenceUrl;
114126
}
115-
currentScenario.url = queueItem.url;
127+
116128
urls.push(currentScenario);
117129
}
118130
});

package-lock.json

+26-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"jsonfile": "^3.0.0",
5454
"meow": "3.7.0",
5555
"mkpath": "1.0.0",
56+
"normalize-url": "^1.9.1",
5657
"object.entries": "1.0.4",
5758
"ora": "^1.3.0",
5859
"simplecrawler": "^1.1.3",

0 commit comments

Comments
 (0)