forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender-tree-titles.js
33 lines (26 loc) · 1.26 KB
/
render-tree-titles.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
const { sortBy } = require('lodash')
const renderOpts = { textOnly: true, encodeEntities: true }
module.exports = async function renderTreeTitles (req, res, next) {
if (!req.context.page) return next()
if (req.context.page.documentType === 'homepage') return next()
await renderLiquidInTitles(req.context.siteTree[req.context.currentLanguage][req.context.currentVersion], req.context)
return next()
}
// Add new props to each siteTree page here...
async function renderLiquidInTitles (pageInTree, context) {
// We _only_ need to render the titles and shortTitles that contain Liquid.
pageInTree.renderedFullTitle = pageInTree.page.title.includes('{')
? await pageInTree.page.renderProp('title', context, renderOpts)
: pageInTree.page.title
if (pageInTree.page.shortTitle) {
pageInTree.renderedShortTitle = pageInTree.page.shortTitle.includes('{')
? await pageInTree.page.renderProp('shortTitle', context, renderOpts)
: pageInTree.page.shortTitle
}
if (!pageInTree.childPages) return
pageInTree.page.childPages = sortBy(
await Promise.all(pageInTree.childPages.map(async (childPage) => await renderLiquidInTitles(childPage, context))),
// Sort by the ordered array of `children` in the frontmatter.
pageInTree.page.children
)
}