Skip to content

Commit b5ffeb8

Browse files
committed
parse Liquid in titles in siteTree and use full titles in generic TOC but short titles in sidebar
1 parent c10bd26 commit b5ffeb8

4 files changed

Lines changed: 46 additions & 8 deletions

File tree

includes/sidebar-product.html

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
{% unless currentProductTree.page.hidden %}
1212

13-
{% if currentProductTree.page.shortTitle and currentProductTree.page.shortTitle != '' %}{% assign productTitle = currentProductTree.page.shortTitle %}{% else %}{% assign productTitle = currentProductTree.page.title %}{% endif %}
13+
{% if currentProductTree.renderedShortTitle %}{% assign productTitle = currentProductTree.renderedShortTitle %}{% else %}{% assign productTitle = currentProductTree.renderedFullTitle %}{% endif %}
1414

1515
<li title="" class="sidebar-product mb-2">
1616
<a href="{{currentProductTree.href}}" class="pl-4 pr-5 pb-1 f4">{{ productTitle }}</a>
@@ -20,14 +20,14 @@
2020
{% for childPage in currentProductTree.childPages %}
2121
{% if childPage.page.documentType == "article" %}{% assign standaloneCategory = true %}{% else %}{% assign standaloneCategory = false %}{% endif %}
2222
<li class="sidebar-category py-1 {% if currentPath contains childPage.href %}active {% if currentPath == childPage.href %}is-current-page {% endif %}{% endif %}{% if standaloneCategory %}standalone-category{% endif %}">
23-
{% if childPage.page.shortTitle and childPage.page.shortTitle != '' %}{% assign categoryTitle = childPage.page.shortTitle %}{% else %}{% assign categoryTitle = childPage.page.title %}{% endif %}
23+
{% if childPage.renderedShortTitle %}{% assign childTitle = childPage.renderedShortTitle %}{% else %}{% assign childTitle = childPage.renderedFullTitle %}{% endif %}
2424
{% if standaloneCategory %}
25-
<a href="{{childPage.href}}" class="pl-4 pr-2 py-2 f6 text-uppercase d-block flex-auto mr-3">{{ categoryTitle }}</a>
25+
<a href="{{childPage.href}}" class="pl-4 pr-2 py-2 f6 text-uppercase d-block flex-auto mr-3">{{ childTitle }}</a>
2626
{% else %}
2727
<details class="dropdown-withArrow details details-reset" {% if currentPath contains childPage.href or forloop.index < 4 %}open{% endif %}>
2828
<summary>
2929
<div class="d-flex flex-justify-between">
30-
<a href="{{childPage.href}}" class="pl-4 pr-2 py-2 f6 text-uppercase d-block flex-auto mr-3">{{ categoryTitle }}</a>
30+
<a href="{{childPage.href}}" class="pl-4 pr-2 py-2 f6 text-uppercase d-block flex-auto mr-3">{{ childTitle }}</a>
3131
{% if forloop.index < 4 %}
3232
<svg xmlns="http://www.w3.org/2000/svg" class="octicon flex-shrink-0 arrow mr-3" style="margin-top:7px" viewBox="0 0 16 16" width="16" height="16"> <path fill-rule="evenodd" clip-rule="evenodd" d="M12.7803 6.21967C13.0732 6.51256 13.0732 6.98744 12.7803 7.28033L8.53033 11.5303C8.23744 11.8232 7.76256 11.8232 7.46967 11.5303L3.21967 7.28033C2.92678 6.98744 2.92678 6.51256 3.21967 6.21967C3.51256 5.92678 3.98744 5.92678 4.28033 6.21967L8 9.93934L11.7197 6.21967C12.0126 5.92678 12.4874 5.92678 12.7803 6.21967Z"></path></svg>
3333
{% endif %}
@@ -39,12 +39,14 @@
3939
{% if childPage.childPages[0].page.documentType == "mapTopic" %}
4040
<ul class="sidebar-topics list-style-none position-relative">
4141
{% for grandchildPage in childPage.childPages %}
42+
{% if grandchildPage.renderedShortTitle %}{% assign grandchildTitle = grandchildPage.renderedShortTitle %}{% else %}{% assign grandchildTitle = grandchildPage.renderedFullTitle %}{% endif %}
4243
<li class="sidebar-maptopic {% if currentPath contains grandchildPage.href %}active {% if currentPath == grandchildPage.href %}is-current-page{% endif %}{% endif %}">
43-
<a href="{{grandchildPage.href}}" class="pl-4 pr-5 py-2">{{ grandchildPage.page.title }} </a>
44+
<a href="{{grandchildPage.href}}" class="pl-4 pr-5 py-2">{{ grandchildTitle }} </a>
4445
<ul class="sidebar-articles my-2">
4546
{% for greatgrandchildPage in grandchildPage.childPages %}
47+
{% if greatgrandchildPage.renderedShortTitle %}{% assign greatgrandchildTitle = greatgrandchildPage.renderedShortTitle %}{% else %}{% assign greatgrandchildTitle = greatgrandchildPage.renderedFullTitle %}{% endif %}
4648
<li class="sidebar-article {% if currentPath contains greatgrandchildPage.href %}active {% if currentPath == greatgrandchildPage.href %}is-current-page{% endif %}{% endif %}">
47-
<a href="{{greatgrandchildPage.href}}" class="pl-6 pr-5 py-1{% if forloop.last %} pb-2{% endif %}">{{ greatgrandchildPage.page.title }}</a>
49+
<a href="{{greatgrandchildPage.href}}" class="pl-6 pr-5 py-1{% if forloop.last %} pb-2{% endif %}">{{ greatgrandchildTitle }}</a>
4850
</li>
4951
{% endfor %}
5052
</ul>
@@ -55,8 +57,9 @@
5557
{% elsif childPage.childPages[0].page.documentType == "article" %}
5658
<ul class="sidebar-articles list-style-none">
5759
{% for grandchildPage in childPage.childPages %}
60+
{% if grandchildPage.renderedShortTitle %}{% assign grandchildTitle = grandchildPage.renderedShortTitle %}{% else %}{% assign grandchildTitle = grandchildPage.renderedFullTitle %}{% endif %}
5861
<li class="sidebar-article {% if currentPath contains grandchildPage.href %}active {% if currentPath == grandchildPage.href %}is-current-page{% endif %}{% endif %}">
59-
<a href="{{grandchildPage.href}}" class="pl-4 pr-5 py-1{% if forloop.last %} pb-2{% endif %}">{{ grandchildPage.page.title }}</a>
62+
<a href="{{grandchildPage.href}}" class="pl-4 pr-5 py-1{% if forloop.last %} pb-2{% endif %}">{{ grandchildTitle }}</a>
6063
</li>
6164
{% endfor %}
6265
</ul>

middleware/contextualizers/generic-toc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ async function getUnsortedTocItems (pageArray, context) {
3939
}
4040

4141
const fullPath = childPage.href
42-
const title = await childPage.page.renderTitle(context, { textOnly: true, encodeEntities: true })
42+
// Titles are already rendered by middleware/contextualizers/render-tree-titles.js.
43+
const title = childPage.renderedFullTitle
4344
const intro = await childPage.page.renderProp('intro', context, { unwrap: true })
4445

4546
if (!childPage.childPages) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { sortBy } = require('lodash')
2+
const renderOpts = { textOnly: true, encodeEntities: true }
3+
4+
module.exports = async function renderTreeTitles (req, res, next) {
5+
if (!req.context.page) return next()
6+
if (req.context.page.documentType === 'homepage') return next()
7+
8+
await renderLiquidInTitles(req.context.siteTree[req.context.currentLanguage][req.context.currentVersion], req.context)
9+
10+
return next()
11+
}
12+
13+
// Add new props to each siteTree page here...
14+
async function renderLiquidInTitles (pageInTree, context) {
15+
// We _only_ need to render the titles and shortTitles that contain Liquid.
16+
pageInTree.renderedFullTitle = pageInTree.page.title.includes('{')
17+
? await pageInTree.page.renderProp('title', context, renderOpts)
18+
: pageInTree.page.title
19+
20+
if (pageInTree.page.shortTitle) {
21+
pageInTree.renderedShortTitle = pageInTree.page.shortTitle.includes('{')
22+
? await pageInTree.page.renderProp('shortTitle', context, renderOpts)
23+
: pageInTree.page.shortTitle
24+
}
25+
26+
if (!pageInTree.childPages) return
27+
28+
pageInTree.page.childPages = sortBy(
29+
await Promise.all(pageInTree.childPages.map(async (childPage) => await renderLiquidInTitles(childPage, context))),
30+
// Sort by the ordered array of `children` in the frontmatter.
31+
pageInTree.page.children
32+
)
33+
}

middleware/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ module.exports = function (app) {
118118
}
119119

120120
if (process.env.FEATURE_NEW_SITETREE) {
121+
app.use(asyncMiddleware(instrument('./contextualizers/render-tree-titles')))
121122
app.use(instrument('./contextualizers/current-product-tree'))
122123
app.use(asyncMiddleware(instrument('./contextualizers/generic-toc')))
123124
app.use(asyncMiddleware(instrument('./contextualizers/breadcrumbs')))

0 commit comments

Comments
 (0)