Skip to content

Commit 5d03537

Browse files
committed
differentiate between TOCs with grandchildren vs. only children
1 parent 05f20c2 commit 5d03537

4 files changed

Lines changed: 54 additions & 24 deletions

File tree

includes/generic-toc-items.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{% if tocItems %}
2+
3+
{% for tocItem in tocItems %}
4+
{% assign title = tocItem.title %}
5+
{% assign fullPath = tocItem.fullPath %}
6+
{% assign intro = tocItem.intro %}
7+
8+
{% if tocItem.childTocItems %}
9+
<ul>
10+
<li>{% include liquid-tags/link %}
11+
<ul>
12+
{% for childItem in tocItem.childTocItems %}
13+
{% assign title = childItem.title %}
14+
{% assign fullPath = childItem.fullPath %}
15+
<li>{% include liquid-tags/link %}</li>
16+
{% endfor %}
17+
</ul>
18+
</li>
19+
</ul>
20+
{% else %}
21+
22+
{% include liquid-tags/link-with-intro %}
23+
24+
{% endif %}
25+
{% endfor %}
26+
{% endif %}

includes/generic-toc.html

Lines changed: 0 additions & 8 deletions
This file was deleted.

layouts/generic-toc.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ <h1 class="border-bottom-0">{{ page.title }}</h1>
4444
{% if featuredLinks.gettingStarted and featuredLinks.popular %}
4545
{% include featured-links %}
4646
{% endif %}
47-
48-
{% include generic-toc %}
47+
48+
{% include generic-toc-items %}
4949
</div>
5050
</div>
5151
</article>

middleware/contextualizers/generic-toc.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,8 @@ module.exports = async function genericToc (req, res, next) {
1010
// Find the array of child pages that start with the requested path.
1111
const currentPageInSiteTree = findPageInSiteTree(currentSiteTree.childPages, req.context.currentPath)
1212

13-
const unsortedTocItems = await Promise.all(currentPageInSiteTree.childPages.map(async (childPage) => {
14-
// return an empty string if it's a hidden link on a non-hidden page (hidden links on hidden pages are OK)
15-
if (childPage.page.hidden && !req.context.page.hidden) {
16-
return ''
17-
}
18-
19-
const fullPath = childPage.href
20-
const title = await childPage.page.renderTitle(req.context, { textOnly: true, encodeEntities: true })
21-
const intro = await childPage.page.renderProp('intro', req.context, { unwrap: true })
22-
23-
return { fullPath, title, intro }
24-
}))
25-
2613
req.context.tocItems = sortBy(
27-
unsortedTocItems,
14+
await getUnsortedTocItems(currentPageInSiteTree.childPages, req.context),
2815
// Sort by the ordered array of `children` in the frontmatter.
2916
currentPageInSiteTree.page.children
3017
)
@@ -43,3 +30,28 @@ function findPageInSiteTree (pageArray, currentPath) {
4330

4431
return findPageInSiteTree(childPage.childPages, currentPath)
4532
}
33+
34+
async function getUnsortedTocItems (pageArray, context) {
35+
return Promise.all(pageArray.map(async (childPage) => {
36+
// return an empty string if it's a hidden link on a non-hidden page (hidden links on hidden pages are OK)
37+
if (childPage.page.hidden && !context.page.hidden) {
38+
return ''
39+
}
40+
41+
const fullPath = childPage.href
42+
const title = await childPage.page.renderTitle(context, { textOnly: true, encodeEntities: true })
43+
const intro = await childPage.page.renderProp('intro', context, { unwrap: true })
44+
45+
if (!childPage.childPages) {
46+
return { fullPath, title, intro }
47+
}
48+
49+
const childTocItems = sortBy(
50+
await getUnsortedTocItems(childPage.childPages, context),
51+
// Sort by the ordered array of `children` in the frontmatter.
52+
childPage.page.children
53+
)
54+
55+
return { fullPath, title, intro, childTocItems }
56+
}))
57+
}

0 commit comments

Comments
 (0)