Skip to content

Commit

Permalink
Ignore text fragment directive in link comparison
Browse files Browse the repository at this point in the history
close #811
  • Loading branch information
dontcallmedom committed Oct 31, 2024
1 parent 3e5b0ab commit 8bd472b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/lib/study-backrefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,14 @@ function studyLinks(spec, links, report, edResults, trResults, htmlFragments) {

// Check anchors
const anchors = links[link].anchors || [];
for (const anchor of anchors) {
for (let anchor of anchors) {
if (anchor.startsWith(':~:text=')) {
// links using only text fragments are inherently fragile
recordAnomaly(spec, 'frailLinks', link + '#' + anchor);
continue;
}
// We remove the fragment directive for comparison
anchor = anchor.split(':~:')[0];
const baseLink = (sourceSpec.nightly?.url === link || sourceSpec.nightly?.pages?.includes(link)) ? link : sourceSpec.nightly?.url;
const matchFullNightlyLink = matchAnchor(baseLink, anchor);
const matchFullReleaseLink = matchAnchor((sourceSpec.release || sourceSpec.nightly).url, anchor);
Expand Down Expand Up @@ -426,10 +433,6 @@ function studyLinks(spec, links, report, edResults, trResults, htmlFragments) {
// the multipage version of HTML
recordAnomaly(spec, 'frailLinks', link + '#' + anchor);
}
else if (anchor.startsWith(':~:text=')) {
// links using text fragments are inherently fragile
recordAnomaly(spec, 'frailLinks', link + '#' + anchor);
}
else {
recordAnomaly(spec, 'brokenLinks', link + '#' + anchor);
}
Expand All @@ -445,4 +448,4 @@ function studyLinks(spec, links, report, edResults, trResults, htmlFragments) {
});
}

export default studyBackrefs;
export default studyBackrefs;
9 changes: 8 additions & 1 deletion test/study-backrefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('The links analyser', () => {
});

it('reports a broken link', async () => {
const ids = ['validid'];
const ids = ['invalidid'];
const crawlResult = toCrawlResults([], ids);
const report = await study(crawlResult.ed, { htmlFragments: {} });
assertNbAnomalies(report, 1);
Expand All @@ -78,6 +78,13 @@ describe('The links analyser', () => {
});
});


it('removes fragment directives when checking for broken links', async () => {
const crawlResult = toCrawlResults(['validid'], ['validid:~:text=foo']);
const report = await study(crawlResult.ed, { htmlFragments: {} });
assertNbAnomalies(report, 0);
});

/* TOTEST
"datedUrls",
"evolvingLinks",
Expand Down

0 comments on commit 8bd472b

Please sign in to comment.