Skip to content

Commit fd07609

Browse files
committed
Adds support for "self" sidebar roots
1 parent 2e5ebc4 commit fd07609

File tree

5 files changed

+49
-23
lines changed

5 files changed

+49
-23
lines changed

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.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: 25 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,21 @@ 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 sidebar root
47+
- Otherwise, walk up ancestors to find any section with `sidebar_root_for`
48+
(either `"self"` or `"children"`)
49+
- Use the closest match as sidebar root
4550
- Use sidebar_root section's permalink as cache key
4651
- Warn if `sidebar_root_for` is set on a top-level section
4752
- Pass `sidebarRoot` to `sidebar-tree.html` as parameter
4853

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

5161
The current implementation of the sidebar tree, already has the sidebar tree
@@ -102,19 +112,19 @@ sidebar_root_for: children
102112
---
103113
```
104114

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.
115+
**With `sidebar_root_for: children`:**
116+
- Viewing `/docs/adding-content/` index → shows **full** docs navigation
117+
- Viewing `/docs/adding-content/content/` → shows **rooted** sidebar (only
118+
"Content and Customization" and its children)
110119

111-
Note that viewing the index page of `/docs/adding-content/` will still show the
112-
full docs navigation tree.
120+
**With `sidebar_root_for: self`:**
121+
- Viewing `/docs/adding-content/` index → shows **rooted** sidebar
122+
- Viewing `/docs/adding-content/content/` → shows **rooted** sidebar
123+
- Both the section itself and descendants get the focused navigation
113124

114125
### To-dos
115126

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

0 commit comments

Comments
 (0)