@@ -10,21 +10,8 @@ module.exports = async function genericToc (req, res, next) {
1010 // Find the array of child pages that start with the requested path.
1111 const currentPageInSiteTree = findPageInSiteTree ( currentSiteTree . childPages , req . context . currentPath )
1212
13- const unsortedTocItems = await Promise . all ( currentPageInSiteTree . childPages . map ( async ( childPage ) => {
14- // return an empty string if it's a hidden link on a non-hidden page (hidden links on hidden pages are OK)
15- if ( childPage . page . hidden && ! req . context . page . hidden ) {
16- return ''
17- }
18-
19- const fullPath = childPage . href
20- const title = await childPage . page . renderTitle ( req . context , { textOnly : true , encodeEntities : true } )
21- const intro = await childPage . page . renderProp ( 'intro' , req . context , { unwrap : true } )
22-
23- return { fullPath, title, intro }
24- } ) )
25-
2613 req . context . tocItems = sortBy (
27- unsortedTocItems ,
14+ await getUnsortedTocItems ( currentPageInSiteTree . childPages , req . context ) ,
2815 // Sort by the ordered array of `children` in the frontmatter.
2916 currentPageInSiteTree . page . children
3017 )
@@ -43,3 +30,28 @@ function findPageInSiteTree (pageArray, currentPath) {
4330
4431 return findPageInSiteTree ( childPage . childPages , currentPath )
4532}
33+
34+ async function getUnsortedTocItems ( pageArray , context ) {
35+ return Promise . all ( pageArray . map ( async ( childPage ) => {
36+ // return an empty string if it's a hidden link on a non-hidden page (hidden links on hidden pages are OK)
37+ if ( childPage . page . hidden && ! context . page . hidden ) {
38+ return ''
39+ }
40+
41+ const fullPath = childPage . href
42+ const title = await childPage . page . renderTitle ( context , { textOnly : true , encodeEntities : true } )
43+ const intro = await childPage . page . renderProp ( 'intro' , context , { unwrap : true } )
44+
45+ if ( ! childPage . childPages ) {
46+ return { fullPath, title, intro }
47+ }
48+
49+ const childTocItems = sortBy (
50+ await getUnsortedTocItems ( childPage . childPages , context ) ,
51+ // Sort by the ordered array of `children` in the frontmatter.
52+ childPage . page . children
53+ )
54+
55+ return { fullPath, title, intro, childTocItems }
56+ } ) )
57+ }
0 commit comments