Skip to content

Commit

Permalink
Use FileRedirect relations for the pretty feature
Browse files Browse the repository at this point in the history
Prevents an already visited asset from receiving a -1 suffix because
it's moved out of the way.
  • Loading branch information
papandreou committed Jul 22, 2020
1 parent d07ea7c commit d06f0c2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
25 changes: 16 additions & 9 deletions lib/index.js
Expand Up @@ -481,20 +481,27 @@ async function hyperlink(
) {
const originalUrl = asset.url;

try {
asset.fileName += '.html';
await asset.load();

reportTest({
...loadReport,
ok: true,
const prettyUrl = asset.url.replace(/(\?|#|$)/, '.html$1');
let prettyAsset = ag.findAssets({ url: prettyUrl })[0];
if (!prettyAsset) {
prettyAsset = ag.addAsset({
url: prettyUrl,
});
asset.url = originalUrl;
}
try {
await prettyAsset.load();
asset.isRedirect = true;
asset.fileRedirectTargetUrl = prettyUrl;
} catch (err) {
reportTest(failedLoadReport);
asset.url = originalUrl;
return;
}

reportTest({
...loadReport,
ok: true,
});
asset.url = originalUrl;
} else {
reportTest(failedLoadReport);
return;
Expand Down
28 changes: 26 additions & 2 deletions test/index.js
Expand Up @@ -2893,8 +2893,8 @@ describe('hyperlink', function () {
);

expect(t.close(), 'to satisfy', {
count: 5,
pass: 5,
count: 7,
pass: 7,
fail: 0,
skip: 0,
todo: 0,
Expand Down Expand Up @@ -2932,6 +2932,30 @@ describe('hyperlink', function () {
});
});
});

// Regression test for https://github.com/Munter/hyperlink/issues/182
it('should not break when discovering a second pretty link to a page that has already been processed', async function () {
const t = new TapRender();
sinon.spy(t, 'push');
await hyperlink(
{
recursive: true,
root: pathModule.resolve(
__dirname,
'..',
'testdata',
'prettyUrlIssue182'
),
inputUrls: ['index.html'],
pretty: true,
},
t
);

expect(t.close(), 'to satisfy', {
fail: 0,
});
});
});

it('should resolve local srcset images as internal', async function () {
Expand Down
8 changes: 8 additions & 0 deletions testdata/prettyUrlIssue182/index.html
@@ -0,0 +1,8 @@
<!doctype html>
<html>
<head>
</head>
<body>
<a href="/set-designer">Set Designer</a>
</body>
</html>
8 changes: 8 additions & 0 deletions testdata/prettyUrlIssue182/set-designer.html
@@ -0,0 +1,8 @@
<!doctype html>
<html>
<head>
</head>
<body>
<a href="/set-designer">Set Designer</a>
</body>
</html>

0 comments on commit d06f0c2

Please sign in to comment.