Skip to content

Commit 394ce84

Browse files
authored
fix(core): the broken anchor checker should not be sensitive pathname trailing slashes (#10130)
1 parent 02e38d8 commit 394ce84

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/docusaurus/src/server/__tests__/brokenLinks.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,32 @@ describe('handleBrokenLinks', () => {
148148
});
149149
});
150150

151+
it('accepts valid non-strict link with anchor', async () => {
152+
await testBrokenLinks({
153+
routes: [{path: '/page1', strict: false}, {path: '/page2/'}],
154+
collectedLinks: {
155+
'/page1': {
156+
links: [
157+
'/page1#page1anchor',
158+
'/page1/#page1anchor',
159+
'/page2#page2anchor',
160+
'/page2/#page2anchor',
161+
],
162+
anchors: ['page1anchor'],
163+
},
164+
'/page2/': {
165+
links: [
166+
'/page1#page1anchor',
167+
'/page1/#page1anchor',
168+
'/page2#page2anchor',
169+
'/page2/#page2anchor',
170+
],
171+
anchors: ['page2anchor'],
172+
},
173+
},
174+
});
175+
});
176+
151177
it('accepts valid links and anchors, sparse arrays', async () => {
152178
await testBrokenLinks({
153179
routes: [{path: '/page1'}, {path: '/page2'}],

packages/docusaurus/src/server/brokenLinks.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,15 @@ function createBrokenLinksHelper({
125125
return false;
126126
}
127127
const targetPage =
128-
collectedLinks.get(pathname) || collectedLinks.get(decodeURI(pathname));
128+
collectedLinks.get(pathname) ??
129+
collectedLinks.get(decodeURI(pathname)) ??
130+
// The broken link checker should not care about a trailing slash
131+
// Those are already covered by the broken pathname checker
132+
// See https://github.com/facebook/docusaurus/issues/10116
133+
collectedLinks.get(addTrailingSlash(pathname)) ??
134+
collectedLinks.get(addTrailingSlash(decodeURI(pathname))) ??
135+
collectedLinks.get(removeTrailingSlash(pathname)) ??
136+
collectedLinks.get(removeTrailingSlash(decodeURI(pathname)));
129137
// link with anchor to a page that does not exist (or did not collect any
130138
// link/anchor) is considered as a broken anchor
131139
if (!targetPage) {

0 commit comments

Comments
 (0)