Skip to content

Commit

Permalink
fix: preserve the fragment portion (hash) of the URL while redirecting (
Browse files Browse the repository at this point in the history
#108)

* fix: preserve the fragment portion (hash) of the URL while redirecting

Closes #107.

* refactor: extract hash as a variable for reuse
  • Loading branch information
0x6b authored and azu committed Sep 24, 2018
1 parent 9f66948 commit eda0a50
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/no-dead-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { isAbsolute } from 'path';
import { getURLOrigin } from 'get-url-origin';

const DEFAULT_OPTIONS = {
checkRelative: true, // {boolean} `false` disables the checks for relative URIs
checkRelative: true, // {boolean} `false` disables the checks for relative URIs.
baseURI: null, // {String|null} a base URI to resolve relative URIs.
ignore: [], // {Array<String>} URIs to be skipped from availability checks.
preferGET: [], // {Array<String>} origins to prefer GET over HEAD.
Expand Down Expand Up @@ -83,10 +83,11 @@ async function isAliveURI(uri, method = 'HEAD') {
Object.assign({}, opts, { redirect: 'follow' }),
);

const { hash } = URL.parse(uri);
return {
ok: finalRes.ok,
redirected: true,
redirectTo: finalRes.url,
redirectTo: hash !== null ? `${finalRes.url}${hash}` : finalRes.url,
message: `${res.status} ${res.statusText}`,
};
}
Expand Down
24 changes: 24 additions & 0 deletions test/no-dead-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ tester.run('no-dead-link', rule, {
ignoreRedirects: true,
},
},
{
text:
'should preserve hash while ignoring redirect: [BDD](http://mochajs.org/#bdd)',
output:
'should preserve hash while ignoring redirect: [BDD](http://mochajs.org/#bdd)',
options: {
ignoreRedirects: true,
},
},
],
invalid: [
{
Expand Down Expand Up @@ -187,5 +196,20 @@ tester.run('no-dead-link', rule, {
},
],
},
{
text:
'should preserve hash while redirecting: [BDD](http://mochajs.org/#bdd)',
output:
'should preserve hash while redirecting: [BDD](https://mochajs.org/#bdd)',
errors: [
{
message:
'http://mochajs.org/#bdd is redirected to https://mochajs.org/#bdd. (301 Moved Permanently)',
index: 46,
line: 1,
column: 47,
},
],
},
],
});

0 comments on commit eda0a50

Please sign in to comment.