Skip to content

Commit 95297c3

Browse files
authored
Simplify getBreadcrumbs (github#26015)
* fix getBreadcrumbs * don't go too deep * remove commented out code
1 parent e18396c commit 95297c3

File tree

3 files changed

+31
-97
lines changed

3 files changed

+31
-97
lines changed

components/page-header/Breadcrumbs.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import styles from './Breadcrumbs.module.scss'
66

77
export type BreadcrumbT = {
88
title: string
9-
documentType?: string
109
href?: string
1110
}
1211

Lines changed: 31 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import liquid from '../../lib/render-content/liquid.js'
2+
13
export default async function breadcrumbs(req, res, next) {
24
if (!req.context.page) return next()
35
if (req.context.page.hidden) return next()
@@ -9,95 +11,42 @@ export default async function breadcrumbs(req, res, next) {
911
return next()
1012
}
1113

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)
2315

2416
return next()
2517
}
2618

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()
4831
}
32+
crumbs.reverse()
4933

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
7235
}
7336

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)
10145
}
102-
})
46+
return page.shortTitle
47+
}
48+
if (page.rawTitle.includes('{')) {
49+
return await liquid.parseAndRender(page.rawTitle, context)
50+
}
51+
return page.title
10352
}

tests/rendering/breadcrumbs.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ describe('breadcrumbs', () => {
120120
const breadcrumbs = await getJSON('/en/github?json=breadcrumbs')
121121
const expected = [
122122
{
123-
documentType: 'product',
124123
href: '/en/github',
125124
title: 'GitHub',
126125
},
@@ -134,17 +133,14 @@ describe('breadcrumbs', () => {
134133
)
135134
const expected = [
136135
{
137-
documentType: 'product',
138136
href: '/en/issues',
139137
title: 'GitHub Issues',
140138
},
141139
{
142-
documentType: 'category',
143140
href: '/en/issues/tracking-your-work-with-issues',
144141
title: 'Issues',
145142
},
146143
{
147-
documentType: 'article',
148144
href: '/en/issues/tracking-your-work-with-issues/quickstart',
149145
title: 'Quickstart for GitHub Issues',
150146
},
@@ -158,17 +154,14 @@ describe('breadcrumbs', () => {
158154
)
159155
const expected = [
160156
{
161-
documentType: 'product',
162157
href: '/en/account-and-profile',
163158
title: 'Account and profile',
164159
},
165160
{
166-
documentType: 'category',
167161
href: '/en/account-and-profile/setting-up-and-managing-your-github-user-account',
168162
title: 'User accounts',
169163
},
170164
{
171-
documentType: 'mapTopic',
172165
href: '/en/account-and-profile/setting-up-and-managing-your-github-user-account/managing-user-account-settings',
173166
title: 'User account settings',
174167
},
@@ -182,22 +175,18 @@ describe('breadcrumbs', () => {
182175
)
183176
const expected = [
184177
{
185-
documentType: 'product',
186178
href: '/en/account-and-profile',
187179
title: 'Account and profile',
188180
},
189181
{
190-
documentType: 'category',
191182
href: '/en/account-and-profile/setting-up-and-managing-your-github-user-account',
192183
title: 'User accounts',
193184
},
194185
{
195-
documentType: 'mapTopic',
196186
href: '/en/account-and-profile/setting-up-and-managing-your-github-user-account/managing-user-account-settings',
197187
title: 'User account settings',
198188
},
199189
{
200-
documentType: 'article',
201190
href: '/en/account-and-profile/setting-up-and-managing-your-github-user-account/managing-user-account-settings/about-your-personal-dashboard',
202191
title: 'Your personal dashboard',
203192
},
@@ -211,17 +200,14 @@ describe('breadcrumbs', () => {
211200
)
212201
const expected = [
213202
{
214-
documentType: 'product',
215203
href: '/en/github',
216204
title: 'GitHub',
217205
},
218206
{
219-
documentType: 'category',
220207
href: '/en/github/site-policy',
221208
title: 'Site policy',
222209
},
223210
{
224-
documentType: 'article',
225211
href: '/en/github/site-policy/github-privacy-statement',
226212
title: 'GitHub Privacy Statement',
227213
},

0 commit comments

Comments
 (0)