@@ -52,8 +52,7 @@ function getPageBrokenLinks({
52
52
// @ts -expect-error: React router types RouteConfig with an actual React
53
53
// component, but we load route components with string paths.
54
54
// 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 ) ) ;
57
56
return matchedRoutes . length === 0 ;
58
57
}
59
58
@@ -78,10 +77,8 @@ function getAllBrokenLinks({
78
77
allCollectedLinks : { [ location : string ] : string [ ] } ;
79
78
routes : RouteConfig [ ] ;
80
79
} ) : { [ location : string ] : BrokenLink [ ] } {
81
- const filteredRoutes = filterIntermediateRoutes ( routes ) ;
82
-
83
80
const allBrokenLinks = _ . mapValues ( allCollectedLinks , ( pageLinks , pagePath ) =>
84
- getPageBrokenLinks ( { pageLinks, pagePath, routes : filteredRoutes } ) ,
81
+ getPageBrokenLinks ( { pageLinks, pagePath, routes} ) ,
85
82
) ;
86
83
87
84
return _ . pickBy ( allBrokenLinks , ( brokenLinks ) => brokenLinks . length > 0 ) ;
@@ -217,24 +214,29 @@ async function filterExistingFileLinks({
217
214
function findOrphanLinks ( {
218
215
allCollectedLinks,
219
216
orphanPages,
217
+ routes,
220
218
} : {
221
219
allCollectedLinks : { [ location : string ] : string [ ] } ;
222
220
orphanPages : DocusaurusConfig [ 'orphanPages' ] ;
221
+ routes : RouteConfig [ ] ;
223
222
} ) {
224
223
if ( ! orphanPages || orphanPages . onOrphanPage === 'ignore' ) {
225
224
return ;
226
225
}
227
226
const visited = new Set < string > ( ) ;
228
227
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 ) ) {
230
231
return ;
231
232
}
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
+ ) ;
234
237
}
235
238
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 ) ) ;
238
240
reportMessage (
239
241
logger . interpolate `Orphan pages found: url=${ Array . from ( orphaned ) } ` ,
240
242
orphanPages . onOrphanPage ,
@@ -244,7 +246,7 @@ function findOrphanLinks({
244
246
export async function handleBrokenLinks ( {
245
247
allCollectedLinks,
246
248
props : {
247
- routes,
249
+ routes : allRoutes ,
248
250
baseUrl,
249
251
outDir,
250
252
siteConfig : { onBrokenLinks, orphanPages} ,
@@ -253,7 +255,8 @@ export async function handleBrokenLinks({
253
255
allCollectedLinks : { [ location : string ] : string [ ] } ;
254
256
props : Props ;
255
257
} ) : Promise < void > {
256
- findOrphanLinks ( { allCollectedLinks, orphanPages} ) ;
258
+ const routes = filterIntermediateRoutes ( allRoutes ) ;
259
+ findOrphanLinks ( { allCollectedLinks, orphanPages, routes} ) ;
257
260
258
261
if ( onBrokenLinks === 'ignore' ) {
259
262
return ;
0 commit comments