forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite-tree-titles.js
70 lines (58 loc) · 2.32 KB
/
site-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
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
const flat = require('flat')
const renderContent = require('./render-content')
const delimiter = '#'
// render localized and product-version-aware page title or shortTitle
module.exports = async function siteTreeTitles (siteTree, siteData) {
// use a non-period delimiter because versions contain periods (like 2.19)
const flatTree = flat(siteTree, { delimiter: delimiter })
const titlesWithLiquid = Object.entries(flatTree)
.filter(([path, shortOrLongTitle]) => path.endsWith('itle') && shortOrLongTitle && shortOrLongTitle.includes('{'))
await Promise.all(titlesWithLiquid.map(async ([path, shortOrLongTitle]) => {
const isShortTitle = /shortTitle$/.test(path)
path = path.replace(`${delimiter}(shortT|t)itle`, '') // ignore the `title` path part for now
// derive values from path parts
const [
languageCode,
version,
products,
product,
categories,
category,
maptopics,
maptopic,
articles,
article
] = path.split(delimiter)
// create context object for rendering of dynamic liquid data in page titles
const ctx = {
currentVersion: version,
site: siteData[languageCode].site
}
const renderedShortOrLongTitle = await renderContent(shortOrLongTitle, ctx, { textOnly: true })
// no product titles have liquid because we get them from lib/all-products.js
// so we can assume all titles processed here will be either a category, maptopic, or article
// we can also assume a category value will exist for any of these
const currentCategory = siteTree[languageCode][version][products][product][categories][category]
if (!maptopic) {
isShortTitle
? currentCategory.shortTitle = renderedShortOrLongTitle
: currentCategory.title = renderedShortOrLongTitle
}
let currentMaptopic
if (maptopic) {
currentMaptopic = currentCategory[maptopics][maptopic]
if (!article) {
isShortTitle
? currentMaptopic.shortTitle = renderedShortOrLongTitle
: currentMaptopic.title = renderedShortOrLongTitle
}
}
let currentArticle
if (article) {
currentArticle = currentMaptopic[articles][article]
isShortTitle
? currentArticle.shortTitle = renderedShortOrLongTitle
: currentArticle.title = renderedShortOrLongTitle
}
}))
}