11import assert from "node:assert/strict" ;
2- import fs from "node:fs " ;
2+ import { createRequire } from "node:module " ;
33import test from "node:test" ;
44import {
55 COOKBOOK_PATH ,
@@ -8,12 +8,29 @@ import {
88 legacyCookbookRedirects ,
99} from "../../src/components/cookbook/LegacyCookbookRedirect/legacyCookbookRedirects.mjs" ;
1010
11- const manifest = JSON . parse (
12- fs . readFileSync (
13- new URL ( "../../src/data/cookbook-manifest.json" , import . meta. url ) ,
14- "utf8" ,
15- ) ,
16- ) ;
11+ const require = createRequire ( import . meta. url ) ;
12+ const sidebars = require ( "../../sidebars.js" ) ;
13+ const recipePaths = new Set ( ) ;
14+ const sectionPaths = new Set ( ) ;
15+
16+ function collectCookbookPaths ( node ) {
17+ if ( Array . isArray ( node ) ) {
18+ for ( const child of node ) collectCookbookPaths ( child ) ;
19+ } else if ( typeof node === "string" ) {
20+ const route = node . startsWith ( "/" ) ? node : `/${ node } ` ;
21+ const match = route . match (
22+ new RegExp ( `^${ COOKBOOK_PATH } /([^/]+)/[^/]+$` ) ,
23+ ) ;
24+ if ( match ) {
25+ recipePaths . add ( route ) ;
26+ sectionPaths . add ( `${ COOKBOOK_PATH } #${ match [ 1 ] } ` ) ;
27+ }
28+ } else if ( node && typeof node === "object" ) {
29+ for ( const value of Object . values ( node ) ) collectCookbookPaths ( value ) ;
30+ }
31+ }
32+
33+ collectCookbookPaths ( sidebars . docs ) ;
1734
1835test ( "maps every legacy cookbook anchor exactly once" , ( ) => {
1936 const groupedAnchors = legacyCookbookRedirectGroups . flatMap (
@@ -26,13 +43,6 @@ test("maps every legacy cookbook anchor exactly once", () => {
2643} ) ;
2744
2845test ( "points every legacy anchor at an existing recipe or cookbook section" , ( ) => {
29- const recipePaths = new Set (
30- manifest . flatMap ( ( { recipes } ) => recipes . map ( ( { href } ) => href ) ) ,
31- ) ;
32- const sectionPaths = new Set (
33- manifest . map ( ( { id } ) => `${ COOKBOOK_PATH } #${ id } ` ) ,
34- ) ;
35-
3646 for ( const [ anchor , target ] of Object . entries ( legacyCookbookRedirects ) ) {
3747 assert . ok (
3848 target === COOKBOOK_PATH ||
0 commit comments