forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
breadcrumbs.js
101 lines (76 loc) · 3.13 KB
/
breadcrumbs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const path = require('path')
const { getPathWithoutLanguage } = require('../lib/path-utils')
const nonEnterpriseDefaultVersion = require('../lib/non-enterprise-default-version')
const removeFPTFromPath = require('../lib/remove-fpt-from-path')
module.exports = async function breadcrumbs (req, res, next) {
if (!req.context.page) return next()
if (req.context.page.hidden) return next()
req.context.breadcrumbs = {}
// Return an empty object on the landing page
if (req.context.page.relativePath === 'index.md') return next()
const rawPath = getPathWithoutLanguage(req.path)
const pathParts = rawPath.split('/')
// drop first '/'
pathParts.shift()
const productPath = path.posix.join('/', req.context.currentProduct)
if (!req.context.siteTree[req.language][req.context.currentVersion].products) {
return next()
}
const product = req.context.siteTree[req.language][req.context.currentVersion].products[req.context.currentProduct]
if (!product) {
return next()
}
req.context.breadcrumbs.product = {
href: removeFPTFromPath(path.posix.join('/', req.context.currentLanguage, req.context.currentVersion, productPath)),
title: product.title
}
// if this is not FPT, drop the version segment so pathParts now starts with /product
// if this is FPT, there is no version segment so pathParts already starts with /product
if (req.context.currentVersion !== nonEnterpriseDefaultVersion) {
pathParts.shift()
}
if (!pathParts[1]) return next()
// get category path
// e.g., `getting-started-with-github` in /free-pro-team@latest/github/getting-started-with-github
// or /enterprise-server@2.21/github/getting-started-with-github
const categoryPath = removeFPTFromPath(path.posix.join('/', req.context.currentLanguage, req.context.currentVersion, productPath, pathParts[1]))
const category = product.categories[categoryPath]
if (!category) return next()
req.context.breadcrumbs.category = {
href: categoryPath,
title: category.shortTitle || category.title
}
if (!pathParts[2]) return next()
// get maptopic path
// e.g., /github/getting-started-with-github/learning-about-github
let maptopic
if (req.context.page.mapTopic) {
const maptopicPath = req.path
maptopic = category.maptopics[maptopicPath]
if (!maptopic) return next()
req.context.breadcrumbs.maptopic = {
href: maptopicPath,
title: maptopic.shortTitle || maptopic.title
}
} else {
const articlePath = req.path
// find parent maptopic if one exists
// some categories don't have maptopics, e.g. site-policy
if (category.maptopics) {
maptopic = Object.values(category.maptopics).find(maptopic => maptopic.articles[articlePath])
if (maptopic) {
req.context.breadcrumbs.maptopic = {
href: maptopic.href,
title: maptopic.shortTitle || maptopic.title
}
}
}
const articlePage = req.context.page
const articleTitle = await articlePage.renderProp('shortTitle', req.context, { textOnly: true, encodeEntities: true })
req.context.breadcrumbs.article = {
href: articlePath,
title: articleTitle
}
}
return next()
}