Skip to content

Commit 2b8fd50

Browse files
gillkylesidharthachatterjee
authored andcommitted
feat(www): add ability to limit table of contents depth (#16818)
1 parent b31284b commit 2b8fd50

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

docs/docs/recipes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: Recipes
3+
tableOfContentsDepth: 2
34
---
45

56
<!-- Basic template for a Gatsby recipe:

www/src/components/docs-table-of-contents.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@ import {
99
transition,
1010
} from "../utils/presets"
1111

12-
function createItems(items, location) {
12+
function isUnderDepthLimit(depth, maxDepth) {
13+
if (maxDepth === null) {
14+
// if no maxDepth is passed in, continue to render more items
15+
return true
16+
} else {
17+
return depth < maxDepth
18+
}
19+
}
20+
21+
// depth and maxDepth are used to figure out how many bullets deep to render in the ToC sidebar, if no
22+
// max depth is set via the tableOfContentsDepth field in the frontmatter, all headings will be rendered
23+
function createItems(items, location, depth, maxDepth) {
1324
return (
1425
items &&
1526
items.map(item => (
@@ -49,13 +60,13 @@ function createItems(items, location) {
4960
>
5061
{item.title}
5162
</Link>
52-
{item.items && (
63+
{item.items && isUnderDepthLimit(depth, maxDepth) && (
5364
<ul
5465
css={{
5566
marginLeft: space[6],
5667
}}
5768
>
58-
{createItems(item.items, location)}
69+
{createItems(item.items, location, depth + 1, maxDepth)}
5970
</ul>
6071
)}
6172
</li>
@@ -85,7 +96,12 @@ function TableOfContents({ page, location }) {
8596
},
8697
}}
8798
>
88-
{createItems(page.tableOfContents.items, location)}
99+
{createItems(
100+
page.tableOfContents.items,
101+
location,
102+
1,
103+
page.frontmatter.tableOfContentsDepth
104+
)}
89105
</ul>
90106
</nav>
91107
) : null

www/src/templates/template-docs-markdown.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ export const pageQuery = graphql`
170170
overview
171171
issue
172172
disableTableOfContents
173+
tableOfContentsDepth
173174
}
174175
...MarkdownPageFooterMdx
175176
}

0 commit comments

Comments
 (0)