Skip to content

Commit ef937e4

Browse files
committed
fix test
1 parent 2da3433 commit ef937e4

File tree

2 files changed

+48
-29
lines changed

2 files changed

+48
-29
lines changed

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {jest} from '@jest/globals';
99
import path from 'path';
1010
import _ from 'lodash';
1111
import {handleBrokenLinks} from '../brokenLinks';
12-
import type {RouteConfig} from '@docusaurus/types';
12+
import type {DocusaurusConfig, Props, RouteConfig} from '@docusaurus/types';
1313

1414
describe('handleBrokenLinks', () => {
1515
const routes: RouteConfig[] = [
@@ -136,10 +136,14 @@ describe('handleBrokenLinks', () => {
136136
};
137137
await handleBrokenLinks({
138138
allCollectedLinks: allCollectedCorrectLinks,
139-
onBrokenLinks: 'error',
140-
routes,
141-
baseUrl: '/',
142-
outDir,
139+
props: {
140+
routes,
141+
baseUrl: '/',
142+
outDir,
143+
siteConfig: {
144+
onBrokenLinks: 'error',
145+
} as DocusaurusConfig,
146+
} as Props,
143147
});
144148
expect(consoleMock).toBeCalledTimes(0);
145149
});
@@ -148,10 +152,14 @@ describe('handleBrokenLinks', () => {
148152
await expect(() =>
149153
handleBrokenLinks({
150154
allCollectedLinks,
151-
onBrokenLinks: 'throw',
152-
routes,
153-
baseUrl: '/',
154-
outDir,
155+
props: {
156+
routes,
157+
baseUrl: '/',
158+
outDir,
159+
siteConfig: {
160+
onBrokenLinks: 'throw',
161+
} as DocusaurusConfig,
162+
} as Props,
155163
}),
156164
).rejects.toThrowErrorMatchingSnapshot();
157165
});
@@ -162,10 +170,14 @@ describe('handleBrokenLinks', () => {
162170
const lodashMock = jest.spyOn(_, 'mapValues');
163171
await handleBrokenLinks({
164172
allCollectedLinks,
165-
onBrokenLinks: 'ignore',
166-
routes,
167-
baseUrl: '/',
168-
outDir,
173+
props: {
174+
routes,
175+
baseUrl: '/',
176+
outDir,
177+
siteConfig: {
178+
onBrokenLinks: 'ignore',
179+
} as DocusaurusConfig,
180+
} as Props,
169181
});
170182
expect(lodashMock).toBeCalledTimes(0);
171183
lodashMock.mockRestore();
@@ -185,10 +197,14 @@ describe('handleBrokenLinks', () => {
185197
await expect(() =>
186198
handleBrokenLinks({
187199
allCollectedLinks,
188-
onBrokenLinks: 'throw',
189-
routes,
190-
baseUrl: '/',
191-
outDir,
200+
props: {
201+
routes,
202+
baseUrl: '/',
203+
outDir,
204+
siteConfig: {
205+
onBrokenLinks: 'throw',
206+
} as DocusaurusConfig,
207+
} as Props,
192208
}),
193209
).rejects.toThrowErrorMatchingSnapshot();
194210
});

packages/docusaurus/src/server/brokenLinks.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ function getPageBrokenLinks({
5252
// @ts-expect-error: React router types RouteConfig with an actual React
5353
// component, but we load route components with string paths.
5454
// We don't actually access component here, so it's fine.
55-
.map((l) => matchRoutes(routes, l))
56-
.flat();
55+
.flatMap((l) => matchRoutes(routes, l));
5756
return matchedRoutes.length === 0;
5857
}
5958

@@ -78,10 +77,8 @@ function getAllBrokenLinks({
7877
allCollectedLinks: {[location: string]: string[]};
7978
routes: RouteConfig[];
8079
}): {[location: string]: BrokenLink[]} {
81-
const filteredRoutes = filterIntermediateRoutes(routes);
82-
8380
const allBrokenLinks = _.mapValues(allCollectedLinks, (pageLinks, pagePath) =>
84-
getPageBrokenLinks({pageLinks, pagePath, routes: filteredRoutes}),
81+
getPageBrokenLinks({pageLinks, pagePath, routes}),
8582
);
8683

8784
return _.pickBy(allBrokenLinks, (brokenLinks) => brokenLinks.length > 0);
@@ -217,24 +214,29 @@ async function filterExistingFileLinks({
217214
function findOrphanLinks({
218215
allCollectedLinks,
219216
orphanPages,
217+
routes,
220218
}: {
221219
allCollectedLinks: {[location: string]: string[]};
222220
orphanPages: DocusaurusConfig['orphanPages'];
221+
routes: RouteConfig[];
223222
}) {
224223
if (!orphanPages || orphanPages.onOrphanPage === 'ignore') {
225224
return;
226225
}
227226
const visited = new Set<string>();
228227
function dfs(link: string) {
229-
if (visited.has(link)) {
228+
// @ts-expect-error: see comment above
229+
const normalLink = matchRoutes(routes, link)[0]?.match.path;
230+
if (!normalLink || visited.has(normalLink)) {
230231
return;
231232
}
232-
visited.add(link);
233-
allCollectedLinks[link]?.forEach((l) => dfs(resolvePathname(l, link)));
233+
visited.add(normalLink);
234+
allCollectedLinks[normalLink]?.forEach((l) =>
235+
dfs(resolvePathname(l, link)),
236+
);
234237
}
235238
orphanPages.entryPoints.forEach(dfs);
236-
const orphaned = new Set(Object.keys(allCollectedLinks));
237-
visited.forEach((l) => orphaned.delete(l));
239+
const orphaned = routes.map((r) => r.path).filter((l) => !visited.has(l));
238240
reportMessage(
239241
logger.interpolate`Orphan pages found: url=${Array.from(orphaned)}`,
240242
orphanPages.onOrphanPage,
@@ -244,7 +246,7 @@ function findOrphanLinks({
244246
export async function handleBrokenLinks({
245247
allCollectedLinks,
246248
props: {
247-
routes,
249+
routes: allRoutes,
248250
baseUrl,
249251
outDir,
250252
siteConfig: {onBrokenLinks, orphanPages},
@@ -253,7 +255,8 @@ export async function handleBrokenLinks({
253255
allCollectedLinks: {[location: string]: string[]};
254256
props: Props;
255257
}): Promise<void> {
256-
findOrphanLinks({allCollectedLinks, orphanPages});
258+
const routes = filterIntermediateRoutes(allRoutes);
259+
findOrphanLinks({allCollectedLinks, orphanPages, routes});
257260

258261
if (onBrokenLinks === 'ignore') {
259262
return;

0 commit comments

Comments
 (0)