Skip to content

Commit 48b2ac2

Browse files
authored
Merge pull request #4065 from raspberrypi/reduce-depth-of-toc
Don't include subsections in site-wide table of contents
2 parents 0181f34 + 989484a commit 48b2ac2

File tree

3 files changed

+72
-218
lines changed

3 files changed

+72
-218
lines changed

jekyll-assets/_layouts/docs.html

Lines changed: 44 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,14 @@ <h1 id="docs-header-title">
2525
</div>
2626
<div class="toc">
2727
{% for subdir in site.data.nav %}
28-
{% if subdir.toc[0] %}
2928
<ul class="sectlevel1">
30-
<input class="toc-toggle-box" type="checkbox" id="{{ subdir.path }}"
31-
{% if page.url contains subdir.path %}
32-
checked=true
33-
{% endif %}
34-
/>
29+
<input class="toc-toggle-box" type="checkbox" id="{{ subdir.path }}" {% if page.url contains subdir.path %}checked{% endif %} />
3530
<li>
3631
<span>
3732
<label class="toc-toggle" for="{{ subdir.path }}" onclick="event.stopPropagation()">
3833
<div class="toc-toggle-container">
3934
<a class="level1" href="{{ site.baseurl }}{{ subdir.path }}">
40-
<a title="expand {{ item.title | markdownify }}"
41-
{% if page.url contains subdir.path %}
42-
class="bold toc-item"
43-
{% else %}
44-
class="toc-item"
45-
{% endif %}
46-
>
35+
<a title="expand {{ item.title | markdownify }}" class="{% if page.url contains subdir.path %}bold {% endif %}toc-item">
4736
{{ subdir.title | markdownify }}
4837
</a>
4938
</a>
@@ -52,126 +41,55 @@ <h1 id="docs-header-title">
5241
</span>
5342
</li>
5443
{% for item in subdir.toc %}
55-
<div class="itemcontents">
56-
<ul class="sectlevel1">
57-
<input class="toc-toggle-box" type="checkbox" id="{{ item.path }}"
58-
{% if page.url contains item.path %}
59-
checked=true
60-
{% endif %}
61-
/>
62-
<li>
63-
<span>
64-
<label class="toc-toggle" for="{{ item.path }}">
65-
<div class="toc-toggle-container">
66-
<a class="level2" href="{{ site.baseurl }}{{ item.path }}">
67-
<a title="expand {{ item.title | strip_html }}"
68-
{% if page.url contains item.path %}
69-
class="bold toc-item"
70-
{% else %}
71-
class="toc-item"
72-
{% endif %}
73-
>
74-
{{ item.title | markdownify }}
75-
</a>
76-
</a>
77-
</div>
78-
</label>
79-
</span>
80-
</li>
81-
{% if item.sections[0] %}
82-
{% for entry in item.sections %}
83-
<div class="itemcontents">
84-
<ul class="sectlevel1">
85-
<input class="toc-toggle-box" type="checkbox" id="{{ item.path }}#{{ entry.anchor }}"
86-
{% if endcapture contains entry.anchor %}
87-
checked=true
88-
{% endif %}
89-
/>
90-
<li>
91-
<span>
92-
<label class="toc-toggle"
93-
{% if entry.subsections[0] %}
94-
for="{{ item.path }}#{{ entry.anchor }}"
95-
{% else %}
96-
{% endif %}>
97-
<div class="toc-toggle-container">
98-
{% if entry.subsections[0] %}
99-
<a class="level3">
100-
<a
101-
id="{{ item.path }}#{{entry.anchor}}-toc"
102-
href="{{ site.baseurl }}{{ item.path }}#{{ entry.anchor }}"
103-
class="toc-item"
104-
onclick="document.getElementById('mobile-toggle').checked = false; updateCurrentToc('{{ item.path }}#{{ entry.anchor }}-toc', '{{ item.path }}#{{ entry.anchor }}');"
105-
title="expand {{ entry.heading | strip_html }}"
106-
>
107-
<script type="text/javascript">if (window.location.hash == '#{{ entry.anchor }}' && window.location.href.includes('{{ item.path }}')) initialiseCurrentToc('{{ item.path }}#{{entry.anchor}}-toc', '{{ item.path }}#{{ entry.anchor }}');
108-
</script>
109-
{{ entry.heading | markdownify }}
110-
</a>
44+
<div class="itemcontents">
45+
<ul class="sectlevel1">
46+
<input class="toc-toggle-box" type="checkbox" id="{{ item.path }}" {% if page.url contains item.path %}checked{% endif %} />
47+
<li>
48+
<span>
49+
<label class="toc-toggle" for="{{ item.path }}">
50+
<div class="toc-toggle-container">
51+
<a class="level2" href="{{ site.baseurl }}{{ item.path }}">
52+
<a title="expand {{ item.title | strip_html }}" class="{% if page.url contains item.path %}bold {% endif %}toc-item">
53+
{{ item.title | markdownify }}
11154
</a>
112-
{% else %}
113-
<a class="level3">
114-
<a id="{{ item.path }}#{{entry.anchor}}-toc"
115-
title="expand {{ entry.heading | strip_html }}"
116-
href="{{ site.baseurl }}{{ item.path }}#{{ entry.anchor }}"
117-
onclick="document.getElementById('mobile-toggle').checked = false; updateCurrentToc('{{ item.path }}#{{ entry.anchor }}-toc');"
118-
class="toc-item no-dropdown"
119-
>
120-
<script type="text/javascript">if (window.location.hash == '#{{ entry.anchor }}' && window.location.href.includes('{{ item.path }}'))
121-
initialiseCurrentToc('{{ item.path }}#{{entry.anchor}}-toc');
122-
</script>
123-
{{ entry.heading | markdownify }}
55+
</a>
56+
</div>
57+
</label>
58+
</span>
59+
</li>
60+
{% for entry in item.sections %}
61+
<div class="itemcontents">
62+
<ul class="sectlevel1">
63+
<li>
64+
<span>
65+
<label class="toc-toggle">
66+
<div class="toc-toggle-container">
67+
<a class="level3">
68+
<a
69+
href="{{ site.baseurl }}{{ item.path }}#{{ entry.anchor }}"
70+
onclick="document.getElementById('mobile-toggle').checked = false"
71+
class="toc-item no-dropdown"
72+
>
73+
{{ entry.heading | markdownify }}
74+
</a>
12475
</a>
125-
</a>
126-
{% endif %}
127-
</div>
128-
</label>
129-
</span>
130-
</li>
131-
{% if entry.subsections[0] %}
132-
{% for subentry in entry.subsections %}
133-
<div class="itemcontents">
134-
<ul class="sectlevel1">
135-
<li>
136-
<span>
137-
<label class="toc-toggle">
138-
<div class="toc-toggle-container">
139-
<a class="level4">
140-
<a class="toc-item no-dropdown"
141-
href="{{ site.baseurl }}{{ item.path }}#{{ subentry.anchor }}"
142-
onclick="document.getElementById('mobile-toggle').checked = false; updateCurrentToc('{{ item.path }}#{{ entry.anchor }}-toc', '{{ item.path }}#{{ entry.anchor }}', '{{ item.path }}#{{ subentry.anchor }}-toc');"
143-
id="{{ item.path }}#{{subentry.anchor}}-toc"
144-
>
145-
<script type="text/javascript">if (window.location.hash == '#{{ subentry.anchor }}' && window.location.href.includes('{{ item.path }}')) initialiseCurrentToc('{{ item.path }}#{{ entry.anchor }}-toc', '{{ item.path }}#{{ entry.anchor }}', '{{ item.path }}#{{ subentry.anchor }}-toc');
146-
</script>
147-
{{ subentry.heading | markdownify }}
148-
</a>
149-
</a>
150-
</div>
151-
</label>
152-
</span>
153-
</li>
154-
</ul>
155-
</div>
156-
{% endfor %}
157-
{% endif %}
158-
</ul>
159-
</div>
76+
</div>
77+
</label>
78+
</span>
79+
</li>
80+
</ul>
81+
</div>
16082
{% endfor %}
161-
{% endif %}
16283
</ul>
16384
</div>
16485
{% endfor %}
16586
</ul>
166-
{% endif %}
167-
{% endfor %}
168-
87+
{% endfor %}
88+
16989
<ul class="sectlevel1">
170-
<input class="toc-toggle-box" type="checkbox" id="datasheets"
171-
/>
17290
<li>
17391
<span>
174-
<label class="toc-toggle" for="{{ subdir.path }}">
92+
<label class="toc-toggle">
17593
<div class="toc-toggle-container">
17694
<a class="level1">
17795
<a title="PDF documentation" class="toc-item no-dropdown" href="https://datasheets.raspberrypi.com/" target="_blank">
@@ -185,11 +103,9 @@ <h1 id="docs-header-title">
185103
</ul>
186104

187105
<ul class="sectlevel1">
188-
<input class="toc-toggle-box" type="checkbox" id="pip"
189-
/>
190106
<li>
191107
<span>
192-
<label class="toc-toggle" for="{{ subdir.path }}">
108+
<label class="toc-toggle">
193109
<div class="toc-toggle-container">
194110
<a class="level1">
195111
<a title="Raspberry Pi compliance documents" class="toc-item no-dropdown" href="https://pip.raspberrypi.com/" target="_blank">
@@ -203,11 +119,9 @@ <h1 id="docs-header-title">
203119
</ul>
204120

205121
<ul class="sectlevel1">
206-
<input class="toc-toggle-box" type="checkbox" id="tutorials"
207-
/>
208122
<li>
209123
<span>
210-
<label class="toc-toggle" for="{{ subdir.path }}">
124+
<label class="toc-toggle">
211125
<div class="toc-toggle-container">
212126
<a class="level1">
213127
<a title="Hands-on hardware and software tutorials" class="toc-item no-dropdown" href="https://www.raspberrypi.com/tutorials/" target="_blank">
@@ -221,11 +135,9 @@ <h1 id="docs-header-title">
221135
</ul>
222136

223137
<ul class="sectlevel1">
224-
<input class="toc-toggle-box" type="checkbox" id="forums"
225-
/>
226138
<li>
227139
<span>
228-
<label class="toc-toggle" for="{{ subdir.path }}">
140+
<label class="toc-toggle">
229141
<div class="toc-toggle-container">
230142
<a class="level1">
231143
<a title="User and product support forums" class="toc-item no-dropdown" href="https://forums.raspberrypi.com" target="_blank">

jekyll-assets/scripts/toc.js

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -34,65 +34,3 @@ function makeToc() {
3434
handleTocOnResize();
3535
}
3636
}
37-
38-
var currentParentID;
39-
var currentParentInputID;
40-
var currentChildID;
41-
42-
function initialiseCurrentToc(parentID, parentInputID = null, childID = null) {
43-
// highlight the new parent
44-
var newParent = document.getElementById(parentID);
45-
newParent.setAttribute('style', 'font-weight:bold');
46-
currentParentID = parentID;
47-
48-
// if a parent input id is specified, expand the parent element
49-
if (parentInputID) {
50-
var newParentInput = document.getElementById(parentInputID);
51-
newParentInput.click();
52-
currentParentInputID = parentInputID;
53-
}
54-
55-
// if a child is specified, highlight the child
56-
if (childID) {
57-
var newChild = document.getElementById(childID);
58-
newChild.setAttribute('style', 'font-weight:bold');
59-
currentChildID = childID;
60-
}
61-
}
62-
63-
function updateCurrentToc(parentID, parentInputID = null, childID = null) {
64-
if (currentParentID == null && currentChildID == null) {
65-
initialiseCurrentToc(parentID, parentInputID, childID)
66-
} else {
67-
// if a parent input id is specified, but no child, expand the parent element
68-
// why no child? because if someone clicks the child within a parent, they don't want the parent section to collapse!
69-
if (parentInputID && childID == null) {
70-
var newParentInput = document.getElementById(parentInputID);
71-
newParentInput.click();
72-
currentParentInputID = parentInputID;
73-
}
74-
75-
// if this is a new parent id, de-highlight the old parent and highlight the new one
76-
if (currentParentID != parentID) {
77-
var oldParent = document.getElementById(currentParentID);
78-
oldParent.style.removeProperty('font-weight');
79-
var newParent = document.getElementById(parentID);
80-
newParent.setAttribute('style', 'font-weight:bold');
81-
currentParentID = parentID;
82-
}
83-
84-
// if there is an old child highlighted, un-highlight it
85-
if (currentChildID) {
86-
var oldChild = document.getElementById(currentChildID);
87-
oldChild.style.removeProperty('font-weight');
88-
}
89-
90-
// if there is a new child, highlight it
91-
if (childID != null) {
92-
var newChild = document.getElementById(childID);
93-
94-
newChild.setAttribute('style', 'font-weight:bold');
95-
}
96-
currentChildID = childID;
97-
}
98-
}

scripts/create_nav.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,33 +122,37 @@ def read_file_with_includes(filepath, filelevel, mainfile, output_dir=None):
122122
if m:
123123
header_id = m.group(1)
124124
else:
125-
m = re.match(r'^\[(.*)\]\s*$', line)
125+
m = re.match(r'^\[#(.+?)(?:,.*?)\]\s*$', line)
126126
if m:
127-
attrs = m.group(1).split(',')
128-
last_line_was_discrete = 'discrete' in attrs
129-
header_id = None
127+
header_id = m.group(1)
130128
else:
131-
m = re.match(r'^(=+)\s+(.+?)\s*$', line)
129+
m = re.match(r'^\[(.*)\]\s*$', line)
132130
if m:
133-
newlevel = len(m.group(1))
134-
# Need to compute anchors for *every* header (updates file_headings)
135-
heading = strip_adoc(m.group(2))
136-
heading = re.sub(r"(\[\.contexttag )(\S+)(\]\*\S+\*)", "<strong class=\"contexttag \\2\">\\2</strong>", heading)
137-
anchor = heading_to_anchor(top_level_file, heading, header_id)
138-
if anchor in available_anchors[fullpath]:
139-
raise Exception("Anchor {} appears twice in {}".format(anchor, fullpath))
140-
available_anchors[fullpath].add(anchor)
141-
if min_level <= newlevel <= max_level and not last_line_was_discrete:
142-
entry = {'heading': heading, 'anchor': anchor}
143-
if newlevel > level:
144-
nav[-1]['sections'][-1]['subsections'] = []
145-
level = newlevel
146-
if level == 2:
147-
nav[-1]['sections'].append(entry)
148-
elif level == 3:
149-
nav[-1]['sections'][-1]['subsections'].append(entry)
150-
last_line_was_discrete = False
151-
header_id = None
131+
attrs = m.group(1).split(',')
132+
last_line_was_discrete = 'discrete' in attrs
133+
header_id = None
134+
else:
135+
m = re.match(r'^(=+)\s+(.+?)\s*$', line)
136+
if m:
137+
newlevel = len(m.group(1))
138+
# Need to compute anchors for *every* header (updates file_headings)
139+
heading = strip_adoc(m.group(2))
140+
heading = re.sub(r"(\[\.contexttag )(\S+)(\]\*\S+\*)", "<strong class=\"contexttag \\2\">\\2</strong>", heading)
141+
anchor = heading_to_anchor(top_level_file, heading, header_id)
142+
if anchor in available_anchors[fullpath]:
143+
raise Exception("Anchor {} appears twice in {}".format(anchor, fullpath))
144+
available_anchors[fullpath].add(anchor)
145+
if min_level <= newlevel <= max_level and not last_line_was_discrete:
146+
entry = {'heading': heading, 'anchor': anchor}
147+
if newlevel > level:
148+
nav[-1]['sections'][-1]['subsections'] = []
149+
level = newlevel
150+
if level == 2:
151+
nav[-1]['sections'].append(entry)
152+
elif level == 3:
153+
nav[-1]['sections'][-1]['subsections'].append(entry)
154+
last_line_was_discrete = False
155+
header_id = None
152156
elif 'from_json' in tab:
153157
tab_dir = os.path.join(adoc_dir, tab['directory'])
154158
if os.path.exists(tab_dir):

0 commit comments

Comments
 (0)