Skip to content

Commit cf3a3d7

Browse files
authored
Add support for "self" sidebar roots (#2341)
1 parent 2e5ebc4 commit cf3a3d7

File tree

8 files changed

+61
-24
lines changed

8 files changed

+61
-24
lines changed

docsy.dev/content/en/docs/adding-content/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
title: Content and Customization
33
weight: 3
44
description: How to add content to and customize your Docsy site.
5-
sidebar_root_for: children
5+
sidebar_root_for: self
66
---

docsy.dev/content/en/docs/best-practices/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: "Best Practices"
33
weight: 9
44
description: >
55
Optional guidance and recommendations about organizing, authoring, and managing your technical documentation.
6+
sidebar_root_for: children
67
---
78

89
Use this section to learn about some of the best practices around creating technical documentation with Docsy.

docsy.dev/content/en/docs/get-started/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ date: 2018-07-30
77
description:
88
Learn how to get started with Docsy, including the available options for
99
installing and using the Docsy theme.
10+
sidebar_root_for: self
1011
---
1112

1213
As you saw in our introduction, Docsy is a [Hugo](https://gohugo.io) theme, which means that if you want to use Docsy, you need to set up your website source so that the Hugo static site generator can find and use the Docsy theme files when building your site. The simplest way to do this is to copy and edit our example site, though we also provide instructions for adding the Docsy theme manually to new or existing sites.

docsy.dev/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"check:links": "npm run _check:links",
2121
"clean": "rm -Rf public",
2222
"fix:format": "npm run _check:format -- --write",
23+
"fix": "npm run fix:format && npm run _refcache:prune",
2324
"make:public": "git init -b main public",
2425
"postbuild:preview": "npm run _check:links--warn",
2526
"postbuild:production": "npm run _check:links--warn",

layouts/_partials/sidebar-tree.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@
110110
$s.Params.manualLinkTitle
111111
$s.Title
112112
-}}
113+
{{ if and $treeRoot (eq $s.Params.sidebar_root_for "self") -}}
114+
{{ with $s.Parent -}}
115+
{{ $manualLink = .RelPermalink -}}
116+
{{ end -}}
117+
{{ end -}}
113118
<li class="td-sidebar-nav__section-title td-sidebar-nav__section
114119
{{- if $withChild }} with-child{{ else }} without-child{{ end -}}
115120
{{ if $activePath }} active-path{{ end -}}

layouts/_partials/sidebar.html

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,30 @@
77
{{ $sidebarRoot := .FirstSection -}}
88
{{ $navRoot := cond (and (ne .Params.toc_root true) (eq .Site.Home.Type "docs")) .Site.Home .FirstSection -}}
99
{{ if .Site.Params.ui.sidebar_root_enabled -}}
10-
{{ range .Ancestors.Reverse -}}
11-
{{ if not (and .IsSection (eq .Params.sidebar_root_for "children")) -}}
12-
{{ continue -}}
13-
{{ end -}}
10+
{{ if and .IsSection (eq .Params.sidebar_root_for "self") -}}
1411
{{ $sidebarRoot = . -}}
15-
{{/* Warn if sidebar_root_for is set on a top-level section */ -}}
16-
{{ if or (eq . $.Site.Home) (eq . $navRoot) -}}
17-
{{ warnf "sidebar_root_for is set on a top-level section (%s). This parameter is intended for nested sections only." .Path -}}
12+
{{ else -}}
13+
{{/* Check ancestors for sidebar_root_for (any value) */ -}}
14+
{{ range .Ancestors.Reverse -}}
15+
{{ if not .IsSection -}}
16+
{{ continue -}}
17+
{{ end -}}
18+
{{ $rootFor := .Params.sidebar_root_for -}}
19+
{{ if not $rootFor -}}
20+
{{ continue -}}
21+
{{ end -}}
22+
{{ if not (or (eq $rootFor "self") (eq $rootFor "children")) -}}
23+
{{ warnf "%s: sidebar_root_for should be 'self' or 'children'. Invalid value: '%s'." .Path $rootFor -}}
24+
{{ continue -}}
25+
{{ end -}}
26+
{{ $sidebarRoot = . -}}
27+
{{ break -}}
1828
{{ end -}}
1929
{{ end -}}
30+
{{/* Warn if sidebar_root_for is set on a top-level section */ -}}
31+
{{ if and $sidebarRoot.Params.sidebar_root_for (eq $sidebarRoot $navRoot) -}}
32+
{{ warnf "%s: sidebar_root_for is set on a top-level section. This parameter is intended for nested sections only." $sidebarRoot.Path -}}
33+
{{ end -}}
2034
{{ end -}}
2135
{{ $sidebarRootID := $sidebarRoot.RelPermalink -}}
2236
{{ $args := dict

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"ci:prepare": "npm run docsy.dev-install && npm run _prepare && npm run _diff:check",
2424
"docsy.dev-install": "npm run _cd:docsy.dev -- npm install",
2525
"fix:format": "npm run _check:format -- --write && npm run cd:docsy.dev fix:format",
26-
"fix": "npm run fix:format",
26+
"fix": "npm run fix:format && npm run cd:docsy.dev fix",
2727
"get:hugo-modules": "node scripts/getHugoModules/index.mjs",
2828
"postinstall": "npm run _mkdir:hugo-mod",
2929
"test:all": "npm run ci:prepare && npm run check && npm run cd:docsy.dev test && npm run ci:post",

tasks/sidebar-root-feature.plan.md

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ deeply nested documentation sections.
1717

1818
## Feature characteristics
1919

20-
- New `sidebar_root_for` parameter set in section `_index.md`. Values:
21-
`children` (current implementation) and `self` (planned).
22-
- When `sidebar_root_for: children`, descendant pages (but not the section
23-
itself) show the rooted sidebar. When `sidebar_root_for: self`, both the
24-
section and its descendants show the rooted sidebar.
20+
- New `sidebar_root_for` parameter set in section `_index.md` with two values:
21+
- `children`: Rooted sidebar shown only for descendant pages
22+
- `self`: Rooted sidebar shown for the section itself and all descendants
23+
- Nested sidebar_root_for sections are supported: descendant pages use the
24+
closest ancestor with `sidebar_root_for` set
2525
- Include navigation out of a sidebar-root section.
2626
- Work alongside existing `toc_root` feature (not replace it)
2727
- Warn if `sidebar_root_for` is set on a top-level section (including site home
@@ -41,11 +41,23 @@ interaction handling is required.
4141

4242
### 1. Update `layouts/_partials/sidebar.html` - Find sidebar root and update cache key
4343

44-
- Walk up page ancestors to find section with `sidebar_root_for: children`
44+
**Support both `children` and `self` values:**
45+
46+
- If current page is a section with `sidebar_root_for: "self"`, use it as
47+
sidebar root
48+
- Otherwise, walk up ancestors to find any section with `sidebar_root_for`
49+
(either `"self"` or `"children"`)
50+
- Use the closest match as sidebar root
4551
- Use sidebar_root section's permalink as cache key
4652
- Warn if `sidebar_root_for` is set on a top-level section
4753
- Pass `sidebarRoot` to `sidebar-tree.html` as parameter
4854

55+
**Logic:**
56+
57+
1. Check if current page has `sidebar_root_for: "self"` → use it
58+
2. Else check ancestors for `sidebar_root_for` (any value) → use first match
59+
3. Result: `self` applies to section itself, `children` only to descendants
60+
4961
### 2. Add link back to sidebar-root section index page
5062

5163
The current implementation of the sidebar tree, already has the sidebar tree
@@ -102,19 +114,22 @@ sidebar_root_for: children
102114
---
103115
```
104116

105-
When viewing any page under `/docs/adding-content/` (such as
106-
`/docs/adding-content/content/`), the sidebar will show only the "Content and
107-
Customization" section and its children, instead of the full docs navigation
108-
tree. This makes the sidebar more focused for users working within this
109-
subsection.
117+
**With `sidebar_root_for: children`:**
118+
119+
- Viewing `/docs/adding-content/` index → shows **full** docs navigation
120+
- Viewing `/docs/adding-content/content/` → shows **rooted** sidebar (only
121+
"Content and Customization" and its children)
122+
123+
**With `sidebar_root_for: self`:**
110124

111-
Note that viewing the index page of `/docs/adding-content/` will still show the
112-
full docs navigation tree.
125+
- Viewing `/docs/adding-content/` index → shows **rooted** sidebar
126+
- Viewing `/docs/adding-content/content/` → shows **rooted** sidebar
127+
- Both the section itself and descendants get the focused navigation
113128

114129
### To-dos
115130

116-
- [x] Step 1: Implement sidebar_root_for lookup and cache key logic in
117-
sidebar.html
131+
- [ ] Step 1: Update sidebar_root_for lookup to support both "self" and
132+
"children" values
118133
- [ ] Step 2: Add link back to site root section index page
119134
- [ ] Step 3: Add breadcrumb navigation UI (OPTIONAL/FUTURE)
120135
- [x] Step 4: Use sidebar_root_for for $navRoot calculation in sidebar-tree.html

0 commit comments

Comments
 (0)