Skip to content

Commit

Permalink
Fix missing reference url slash (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
ataylorme authored and danreeves committed Nov 17, 2017
1 parent e6ecdd8 commit e260967
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ if (cli.flags.limitSimilar) {
}

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

if (!validurl(cli.flags.referenceUrl)) {
console.error(
Expand Down
20 changes: 16 additions & 4 deletions lib/crawl.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const chalk = require('chalk');
const mkpath = require('mkpath');
const jsonfile = require('jsonfile');
const cheerio = require('cheerio');
const normalurl = require('normalize-url');
const limitSimilar = require('./limit-similar');

const dirname = path.dirname;
Expand Down Expand Up @@ -99,11 +100,22 @@ function crawl(url, flags) {
// Clone the default scenario and adjust urls as needed
const currentScenario = Object.assign({}, defaultScenario);
currentScenario.label = queueItem.path;
currentScenario.url = queueItem.url;

if (flags.referenceUrl) {
currentScenario.referenceUrl = queueItem.url.replace(
url,
flags.referenceUrl
// Normalize the URL to remove duplicate slashes. See #25
currentScenario.referenceUrl = normalurl(
queueItem.url.replace(
url,
flags.referenceUrl
)
);

// Add a trailing slash to referenceUrl if url has a trailing slash and referenceUrl does not
if ( currentScenario.url.substr(-1) === '/' && currentScenario.referenceUrl.substr(-1) !== '/' ) {
currentScenario.referenceUrl += '/';
}

} else if (
Object.prototype.hasOwnProperty.call(
currentScenario,
Expand All @@ -112,7 +124,7 @@ function crawl(url, flags) {
) {
delete currentScenario.referenceUrl;
}
currentScenario.url = queueItem.url;

urls.push(currentScenario);
}
});
Expand Down
29 changes: 26 additions & 3 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"jsonfile": "^3.0.0",
"meow": "3.7.0",
"mkpath": "1.0.0",
"normalize-url": "^1.9.1",
"object.entries": "1.0.4",
"ora": "^1.3.0",
"simplecrawler": "^1.1.3",
Expand Down

0 comments on commit e260967

Please sign in to comment.