forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric-toc.js
72 lines (57 loc) · 2.59 KB
/
generic-toc.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
const { sortBy } = require('lodash')
module.exports = async function genericToc (req, res, next) {
if (!req.context.page) return next()
if (req.context.currentLayoutName !== 'default') return next()
if (req.context.page.documentType === 'homepage' || req.context.page.documentType === 'article') return next()
const currentSiteTree = req.context.siteTree[req.context.currentLanguage][req.context.currentVersion]
// Find the array of child pages that start with the requested path.
const currentPageInSiteTree = findPageInSiteTree(currentSiteTree.childPages, req.context.currentPath, req.context.currentLanguage)
if (!currentPageInSiteTree) return next()
req.context.tocItems = sortBy(
await getUnsortedTocItems(currentPageInSiteTree.childPages, req.context),
// Sort by the ordered array of `children` in the frontmatter.
currentPageInSiteTree.page.children
)
return next()
}
// Recursively loop through the siteTree until we reach the point where the
// current siteTree page is the same as the requested page. Then stop.
function findPageInSiteTree (pageArray, currentPath, currentLanguage) {
const childPage = pageArray.find(page => {
// Find a page that matches at least an initial part of the current path
const regex = new RegExp(`^${page.href}($|/)`, 'm')
return regex.test(currentPath)
})
// Fallback for outdated translations
if (!childPage && currentLanguage !== 'en') {
return findPageInSiteTree(pageArray, currentPath.replace(`/${currentLanguage}`, '/en'), 'en')
}
if (!childPage && currentLanguage === 'en') {
return
}
if (childPage.href === currentPath) {
return childPage
}
return findPageInSiteTree(childPage.childPages, currentPath)
}
async function getUnsortedTocItems (pageArray, context) {
return Promise.all(pageArray.map(async (childPage) => {
// return an empty string if it's a hidden link on a non-hidden page (hidden links on hidden pages are OK)
if (childPage.page.hidden && !context.page.hidden) {
return ''
}
const fullPath = childPage.href
// Titles are already rendered by middleware/contextualizers/render-tree-titles.js.
const title = childPage.renderedFullTitle
const intro = await childPage.page.renderProp('intro', context, { unwrap: true })
if (!childPage.childPages) {
return { fullPath, title, intro }
}
const childTocItems = sortBy(
await getUnsortedTocItems(childPage.childPages, context),
// Sort by the ordered array of `children` in the frontmatter.
childPage.page.children
)
return { fullPath, title, intro, childTocItems }
}))
}