Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): the broken anchor checker should not be sensitive pathname trailing slashes #10130

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/docusaurus/src/server/__tests__/brokenLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,32 @@ describe('handleBrokenLinks', () => {
});
});

it('accepts valid non-strict link with anchor', async () => {
await testBrokenLinks({
routes: [{path: '/page1', strict: false}, {path: '/page2/'}],
collectedLinks: {
'/page1': {
links: [
'/page1#page1anchor',
'/page1/#page1anchor',
'/page2#page2anchor',
'/page2/#page2anchor',
],
anchors: ['page1anchor'],
},
'/page2/': {
links: [
'/page1#page1anchor',
'/page1/#page1anchor',
'/page2#page2anchor',
'/page2/#page2anchor',
],
anchors: ['page2anchor'],
},
},
});
});

it('accepts valid links and anchors, sparse arrays', async () => {
await testBrokenLinks({
routes: [{path: '/page1'}, {path: '/page2'}],
Expand Down
10 changes: 9 additions & 1 deletion packages/docusaurus/src/server/brokenLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,15 @@ function createBrokenLinksHelper({
return false;
}
const targetPage =
collectedLinks.get(pathname) || collectedLinks.get(decodeURI(pathname));
collectedLinks.get(pathname) ??
collectedLinks.get(decodeURI(pathname)) ??
// The broken link checker should not care about a trailing slash
// Those are already covered by the broken pathname checker
// See https://github.com/facebook/docusaurus/issues/10116
collectedLinks.get(addTrailingSlash(pathname)) ??
collectedLinks.get(addTrailingSlash(decodeURI(pathname))) ??
collectedLinks.get(removeTrailingSlash(pathname)) ??
collectedLinks.get(removeTrailingSlash(decodeURI(pathname)));
// link with anchor to a page that does not exist (or did not collect any
// link/anchor) is considered as a broken anchor
if (!targetPage) {
Expand Down
Loading