Skip to content

Commit eda0a50

Browse files
0x6bazu
authored andcommitted
fix: preserve the fragment portion (hash) of the URL while redirecting (#108)
* fix: preserve the fragment portion (hash) of the URL while redirecting Closes #107. * refactor: extract hash as a variable for reuse
1 parent 9f66948 commit eda0a50

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/no-dead-link.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { isAbsolute } from 'path';
77
import { getURLOrigin } from 'get-url-origin';
88

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

86+
const { hash } = URL.parse(uri);
8687
return {
8788
ok: finalRes.ok,
8889
redirected: true,
89-
redirectTo: finalRes.url,
90+
redirectTo: hash !== null ? `${finalRes.url}${hash}` : finalRes.url,
9091
message: `${res.status} ${res.statusText}`,
9192
};
9293
}

test/no-dead-link.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ tester.run('no-dead-link', rule, {
8686
ignoreRedirects: true,
8787
},
8888
},
89+
{
90+
text:
91+
'should preserve hash while ignoring redirect: [BDD](http://mochajs.org/#bdd)',
92+
output:
93+
'should preserve hash while ignoring redirect: [BDD](http://mochajs.org/#bdd)',
94+
options: {
95+
ignoreRedirects: true,
96+
},
97+
},
8998
],
9099
invalid: [
91100
{
@@ -187,5 +196,20 @@ tester.run('no-dead-link', rule, {
187196
},
188197
],
189198
},
199+
{
200+
text:
201+
'should preserve hash while redirecting: [BDD](http://mochajs.org/#bdd)',
202+
output:
203+
'should preserve hash while redirecting: [BDD](https://mochajs.org/#bdd)',
204+
errors: [
205+
{
206+
message:
207+
'http://mochajs.org/#bdd is redirected to https://mochajs.org/#bdd. (301 Moved Permanently)',
208+
index: 46,
209+
line: 1,
210+
column: 47,
211+
},
212+
],
213+
},
190214
],
191215
});

0 commit comments

Comments
 (0)