Skip to content

Commit

Permalink
Feature/handle GitHub readme fragments (#159)
Browse files Browse the repository at this point in the history
Feature/handle GitHub readme fragments
  • Loading branch information
Munter authored Aug 13, 2019
2 parents ff93e20 + ff51942 commit 66bc6ba
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,11 +708,24 @@ async function hyperlink(
actual: fragmentReport.expected
});
} else {
reportTest({
...fragmentReport,
ok: false,
actual: null
});
// Github does some weird things with mangling fragments and reversing it with runtime js
if (
asset.origin === 'https://github.com' &&
asset.ids &&
asset.ids.has(`user-content-${fragment.substr(1)}`)
) {
reportTest({
...fragmentReport,
ok: true,
actual: `id="user-content-${fragment.substr(1)}"`
});
} else {
reportTest({
...fragmentReport,
ok: false,
actual: null
});
}
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,47 @@ describe('hyperlink', function() {
});
});

it('should retry failed fragment links to Github urls with a prepended "user-content-"', async function() {
httpception({
request: 'GET https://github.com/assetgraph/assetgraph',
response: {
headers: {
'content-type': 'text/html'
},
body:
'<a id="user-content-tools-built-with-assetgraph" href="#tools-built-with-assetgraph"></a>'
}
});

const t = new TapRender();
// t.pipe(process.stderr);
sinon.spy(t, 'push');
await hyperlink(
{
root: 'https://github.com/assetgraph/assetgraph',
inputUrls: ['https://github.com/assetgraph/assetgraph']
},
t
);

expect(spyTapCalls(t.push), 'to satisfy', [
{
operator: 'load',
name: 'load https://github.com/assetgraph/assetgraph',
ok: true
},
{
operator: 'fragment-check',
name:
'fragment-check https://github.com/assetgraph/assetgraph --> #tools-built-with-assetgraph',
ok: true,
expected: 'id="tools-built-with-assetgraph"',
actual: 'id="user-content-tools-built-with-assetgraph"'
}
]);
expect(t.close(), 'to satisfy', { pass: 2, fail: 0 });
});

describe('with internalOnly true', () => {
it('should not follow external links', async () => {
httpception([
Expand Down

0 comments on commit 66bc6ba

Please sign in to comment.