1
+ import liquid from '../../lib/render-content/liquid.js'
2
+
1
3
export default async function breadcrumbs ( req , res , next ) {
2
4
if ( ! req . context . page ) return next ( )
3
5
if ( req . context . page . hidden ) return next ( )
@@ -9,95 +11,42 @@ export default async function breadcrumbs(req, res, next) {
9
11
return next ( )
10
12
}
11
13
12
- const currentSiteTree =
13
- req . context . siteTree [ req . context . currentLanguage ] [ req . context . currentVersion ]
14
- const fallbackSiteTree = req . context . siteTree . en [ req . context . currentVersion ]
15
-
16
- req . context . breadcrumbs = await getBreadcrumbs (
17
- // Array of child pages on the root, i.e., the product level.
18
- currentSiteTree . childPages ,
19
- fallbackSiteTree . childPages ,
20
- req . context . currentPath . slice ( 3 ) ,
21
- req . context . currentLanguage
22
- )
14
+ req . context . breadcrumbs = await getBreadcrumbs ( req )
23
15
24
16
return next ( )
25
17
}
26
18
27
- async function getBreadcrumbs (
28
- pageArray ,
29
- fallbackPageArray ,
30
- currentPathWithoutLanguage ,
31
- intendedLanguage
32
- ) {
33
- // Find the page that starts with the requested path
34
- let childPage = findPageWithPath ( currentPathWithoutLanguage , pageArray )
35
-
36
- // Find the page in the fallback page array (likely the English sub-tree)
37
- const fallbackChildPage =
38
- findPageWithPath ( currentPathWithoutLanguage , fallbackPageArray || [ ] ) || childPage
39
-
40
- // No matches, we bail
41
- if ( ! childPage && ! fallbackChildPage ) {
42
- return [ ]
43
- }
44
-
45
- // Didn't find the intended page, but found the fallback
46
- if ( ! childPage ) {
47
- childPage = fallbackChildPage
19
+ async function getBreadcrumbs ( req ) {
20
+ const crumbs = [ ]
21
+ const { currentPath, currentVersion } = req . context
22
+ const split = currentPath . split ( '/' )
23
+ while ( split . length > 2 && split [ split . length - 1 ] !== currentVersion ) {
24
+ const href = split . join ( '/' )
25
+ const page = req . context . pages [ href ]
26
+ crumbs . push ( {
27
+ href,
28
+ title : await getShortTitle ( page , req . context ) ,
29
+ } )
30
+ split . pop ( )
48
31
}
32
+ crumbs . reverse ( )
49
33
50
- const breadcrumb = {
51
- documentType : childPage . page . documentType ,
52
- // give the breadcrumb the intendedLanguage, so nav through breadcrumbs doesn't inadvertantly change the user's selected language
53
- href : `/${ intendedLanguage } /${ childPage . href . slice ( 4 ) } ` ,
54
- title : childPage . renderedShortTitle || childPage . renderedFullTitle ,
55
- }
56
-
57
- // Recursively loop through the childPages and create each breadcrumb, until we reach the
58
- // point where the current siteTree page is the same as the requested page. Then stop.
59
- if ( childPage . childPages && currentPathWithoutLanguage !== childPage . href . slice ( 3 ) ) {
60
- return [
61
- breadcrumb ,
62
- ...( await getBreadcrumbs (
63
- childPage . childPages ,
64
- fallbackChildPage . childPages ,
65
- currentPathWithoutLanguage ,
66
- intendedLanguage
67
- ) ) ,
68
- ]
69
- } else {
70
- return [ breadcrumb ]
71
- }
34
+ return crumbs
72
35
}
73
36
74
- // Finds the page that starts with or equals the requested path in the array of
75
- // pages e.g. if the current page is /actions/learn-github-actions/understanding-github-actions,
76
- // depending on the pages in the pageArray agrument, would find:
77
- //
78
- // * /actions
79
- // * /actions/learn-github-actions
80
- // * /actions/learn-github-actions/understanding-github-actions
81
- function findPageWithPath ( pageToFind , pageArray ) {
82
- return pageArray . find ( ( page ) => {
83
- const pageWithoutLanguage = page . href . slice ( 3 )
84
- const numPathSegments = pageWithoutLanguage . split ( '/' ) . length
85
- const pageToFindNumPathSegments = pageToFind . split ( '/' ) . length
86
-
87
- if ( pageToFindNumPathSegments > numPathSegments ) {
88
- // if the current page to find has more path segments, add a trailing
89
- // slash to the page comparison to avoid an overlap like:
90
- //
91
- // * /github-cli/github-cli/about-github-cli with /github
92
- return pageToFind . startsWith ( `${ pageWithoutLanguage } /` )
93
- } else if ( pageToFindNumPathSegments === numPathSegments ) {
94
- // if the current page has the same number of path segments, only match
95
- // if the paths are the same to avoid an overlap like:
96
- //
97
- // * /get-started/using-github with /get-started/using-git
98
- return pageToFind === pageWithoutLanguage
99
- } else {
100
- return false
37
+ async function getShortTitle ( page , context ) {
38
+ if ( page . rawShortTitle ) {
39
+ if ( page . rawShortTitle . includes ( '{' ) ) {
40
+ // Can't easily cache this because the `page` is reused for multiple
41
+ // permalinks. We could do what the `Page.render()` method does which
42
+ // specifically caches based on the `context.currentPath` but at
43
+ // this point it's probably not worth it.
44
+ return await liquid . parseAndRender ( page . rawShortTitle , context )
101
45
}
102
- } )
46
+ return page . shortTitle
47
+ }
48
+ if ( page . rawTitle . includes ( '{' ) ) {
49
+ return await liquid . parseAndRender ( page . rawTitle , context )
50
+ }
51
+ return page . title
103
52
}
0 commit comments