Skip to content

Commit a7aea5d

Browse files
committed
Add configuration options to hide TOC or module navigation
1 parent 1aebff9 commit a7aea5d

File tree

8 files changed

+111
-24
lines changed

8 files changed

+111
-24
lines changed

src/librustdoc/html/render/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
623623
title: "",
624624
is_crate: false,
625625
is_mod: false,
626+
parent_is_crate: false,
626627
blocks: vec![blocks],
627628
path: String::new(),
628629
};

src/librustdoc/html/render/sidebar.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub(super) struct Sidebar<'a> {
2121
pub(super) title_prefix: &'static str,
2222
pub(super) title: &'a str,
2323
pub(super) is_crate: bool,
24+
pub(super) parent_is_crate: bool,
2425
pub(super) is_mod: bool,
2526
pub(super) blocks: Vec<LinkBlock<'a>>,
2627
pub(super) path: String,
@@ -144,8 +145,15 @@ pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buf
144145
} else {
145146
"".into()
146147
};
147-
let sidebar =
148-
Sidebar { title_prefix, title, is_mod: it.is_mod(), is_crate: it.is_crate(), blocks, path };
148+
let sidebar = Sidebar {
149+
title_prefix,
150+
title,
151+
is_mod: it.is_mod(),
152+
is_crate: it.is_crate(),
153+
parent_is_crate: sidebar_path.len() == 1,
154+
blocks,
155+
path,
156+
};
149157
sidebar.render_into(buffer).unwrap();
150158
}
151159

@@ -173,7 +181,6 @@ fn docblock_toc<'a>(
173181
error_codes: cx.shared.codes,
174182
edition: cx.shared.edition(),
175183
playground: &cx.shared.playground,
176-
custom_code_classes_in_docs: cx.tcx().features().custom_code_classes_in_docs,
177184
}
178185
.into_parts();
179186
let links: Vec<Link<'_>> = toc
@@ -202,7 +209,7 @@ fn docblock_toc<'a>(
202209
if links.is_empty() {
203210
None
204211
} else {
205-
Some(LinkBlock::new(Link::new("#", "Sections"), "top-toc", links))
212+
Some(LinkBlock::new(Link::new("", "Sections"), "top-toc", links))
206213
}
207214
}
208215

src/librustdoc/html/static/css/rustdoc.css

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,8 @@ ul.block, .block li, .block ul {
574574
list-style: none;
575575
}
576576

577-
.block ul {
578-
margin-left: 12px;
577+
.block ul a {
578+
padding-left: 1rem;
579579
}
580580

581581
.sidebar-elems a,
@@ -589,6 +589,14 @@ ul.block, .block li, .block ul {
589589
background-clip: border-box;
590590
}
591591

592+
.hide-toc #TOC, .hide-toc .in-crate {
593+
display: none;
594+
}
595+
596+
.hide-modnav #ModNav {
597+
display: none;
598+
}
599+
592600
.sidebar h2 {
593601
text-wrap: balance;
594602
overflow-wrap: anywhere;

src/librustdoc/html/static/js/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ function preLoadCss(cssUrl) {
499499
if (!window.SIDEBAR_ITEMS) {
500500
return;
501501
}
502-
const sidebar = document.getElementsByClassName("sidebar-elems")[0];
502+
const sidebar = document.getElementById("ModNav");
503503

504504
/**
505505
* Append to the sidebar a "block" of links - a heading along with a list (`<ul>`) of items.
@@ -885,7 +885,7 @@ function preLoadCss(cssUrl) {
885885
if (!window.ALL_CRATES) {
886886
return;
887887
}
888-
const sidebarElems = document.getElementsByClassName("sidebar-elems")[0];
888+
const sidebarElems = document.getElementById("ModNav");
889889
if (!sidebarElems) {
890890
return;
891891
}

src/librustdoc/html/static/js/settings.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@
3636
removeClass(document.documentElement, "hide-sidebar");
3737
}
3838
break;
39+
case "hide-toc":
40+
if (value === true) {
41+
addClass(document.documentElement, "hide-toc");
42+
} else {
43+
removeClass(document.documentElement, "hide-toc");
44+
}
45+
break;
46+
case "hide-modnav":
47+
if (value === true) {
48+
addClass(document.documentElement, "hide-modnav");
49+
} else {
50+
removeClass(document.documentElement, "hide-modnav");
51+
}
52+
break;
3953
}
4054
}
4155

@@ -102,6 +116,11 @@
102116
let output = "";
103117

104118
for (const setting of settings) {
119+
if (setting === "hr") {
120+
output += "<hr>";
121+
continue;
122+
}
123+
105124
const js_data_name = setting["js_name"];
106125
const setting_name = setting["name"];
107126

@@ -198,6 +217,16 @@
198217
"js_name": "hide-sidebar",
199218
"default": false,
200219
},
220+
{
221+
"name": "Hide table of contents",
222+
"js_name": "hide-toc",
223+
"default": false,
224+
},
225+
{
226+
"name": "Hide module navigation",
227+
"js_name": "hide-modnav",
228+
"default": false,
229+
},
201230
{
202231
"name": "Disable keyboard shortcuts",
203232
"js_name": "disable-shortcuts",

src/librustdoc/html/static/js/storage.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,21 @@ updateTheme();
196196
// This needs to be done here because this JS is render-blocking,
197197
// so that the sidebar doesn't "jump" after appearing on screen.
198198
// The user interaction to change this is set up in main.js.
199+
//
200+
// At this point in page load, `document.body` is not available yet.
201+
// Set a class on the `<html>` element instead.
199202
if (getSettingValue("source-sidebar-show") === "true") {
200-
// At this point in page load, `document.body` is not available yet.
201-
// Set a class on the `<html>` element instead.
202203
addClass(document.documentElement, "src-sidebar-expanded");
203204
}
204205
if (getSettingValue("hide-sidebar") === "true") {
205-
// At this point in page load, `document.body` is not available yet.
206-
// Set a class on the `<html>` element instead.
207206
addClass(document.documentElement, "hide-sidebar");
208207
}
208+
if (getSettingValue("hide-toc") === "true") {
209+
addClass(document.documentElement, "hide-toc");
210+
}
211+
if (getSettingValue("hide-modnav") === "true") {
212+
addClass(document.documentElement, "hide-modnav");
213+
}
209214
function updateSidebarWidth() {
210215
const desktopSidebarWidth = getSettingValue("desktop-sidebar-width");
211216
if (desktopSidebarWidth && desktopSidebarWidth !== "null") {

src/librustdoc/html/templates/sidebar.html

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
{% if !title.is_empty() %}
2-
<h2 class="location"> {# #}
3-
<a href="#">{{title_prefix}}{{title|wrapped|safe}}</a> {# #}
4-
</h2>
5-
{% endif %}
61
<div class="sidebar-elems">
72
{% if is_crate %}
83
<ul class="block"> {# #}
@@ -11,11 +6,16 @@ <h2 class="location"> {# #}
116
{% endif %}
127

138
{% if self.should_render_blocks() %}
14-
<section>
9+
<section id="TOC">
10+
{% if !title.is_empty() %}
11+
<h2 class="location"> {# #}
12+
<a href="#">{{title_prefix}}{{title|wrapped|safe}}</a> {# #}
13+
</h2>
14+
{% endif %}
1515
{% for block in blocks %}
1616
{% if block.should_render() %}
1717
{% if !block.heading.name.is_empty() %}
18-
<h3{% if !block.class.is_empty() +%} class="{{block.class}}"{% endif %}> {# #}
18+
<h3> {# #}
1919
<a href="#{{block.heading.href|safe}}">{{block.heading.name|wrapped|safe}}</a> {# #}
2020
</h3> {# #}
2121
{% endif %}
@@ -39,7 +39,11 @@ <h2 class="location"> {# #}
3939
{% endfor %}
4040
</section>
4141
{% endif %}
42+
<div id="ModNav">
4243
{% if !path.is_empty() %}
43-
<h2><a href="{% if is_mod %}../{% endif %}index.html">In {{+ path|wrapped|safe}}</a></h2>
44+
<h2{% if parent_is_crate +%} class="in-crate"{% endif %}> {# #}
45+
<a href="{% if is_mod %}../{% endif %}index.html">In {{+ path|wrapped|safe}}</a> {# #}
46+
</h2> {# #}
4447
{% endif %}
48+
</div> {# #}
4549
</div>

tests/rustdoc-gui/sidebar.goml

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ assert-text: (".sidebar-elems ul.block > li.current > a", "module")
126126
// - Module name, followed by TOC for module headings
127127
// - "In crate [name]" parent pointer, followed by sibling navigation
128128
assert-count: (".sidebar h2", 3)
129-
assert-text: (".sidebar > .sidebar-elems > h2", "In crate lib2")
130-
assert-property: (".sidebar > .sidebar-elems > h2 > a", {
129+
assert-text: (".sidebar > .sidebar-elems > #ModNav > h2", "In crate lib2")
130+
assert-property: (".sidebar > .sidebar-elems > #ModNav > h2 > a", {
131131
"href": "/lib2/index.html",
132132
}, ENDS_WITH)
133133
// We check that we don't have the crate list.
@@ -137,8 +137,8 @@ go-to: "./sub_module/sub_sub_module/index.html"
137137
assert-property: (".sidebar", {"clientWidth": "200"})
138138
assert-text: (".sidebar > .sidebar-crate > h2 > a", "lib2")
139139
assert-text: (".sidebar .location", "Module sub_sub_module")
140-
assert-text: (".sidebar > .sidebar-elems > h2", "In lib2::module::sub_module")
141-
assert-property: (".sidebar > .sidebar-elems > h2 > a", {
140+
assert-text: (".sidebar > .sidebar-elems > #ModNav > h2", "In lib2::module::sub_module")
141+
assert-property: (".sidebar > .sidebar-elems > #ModNav > h2 > a", {
142142
"href": "/module/sub_module/index.html",
143143
}, ENDS_WITH)
144144
assert-text: (".sidebar-elems ul.block > li.current > a", "sub_sub_module")
@@ -198,3 +198,36 @@ assert-position-false: (".sidebar-crate > h2 > a", {"x": -3})
198198
// when line-wrapped, see that it becomes flush-left again
199199
drag-and-drop: ((205, 100), (108, 100))
200200
assert-position: (".sidebar-crate > h2 > a", {"x": -3})
201+
202+
// Configuration option to show TOC in sidebar.
203+
set-local-storage: {"rustdoc-hide-toc": "true"}
204+
go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html"
205+
assert-css: ("#TOC", {"display": "none"})
206+
assert-css: (".sidebar .in-crate", {"display": "none"})
207+
set-local-storage: {"rustdoc-hide-toc": "false"}
208+
go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html"
209+
assert-css: ("#TOC", {"display": "block"})
210+
assert-css: (".sidebar .in-crate", {"display": "block"})
211+
212+
set-local-storage: {"rustdoc-hide-modnav": "true"}
213+
go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html"
214+
assert-css: ("#ModNav", {"display": "none"})
215+
set-local-storage: {"rustdoc-hide-modnav": "false"}
216+
go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html"
217+
assert-css: ("#ModNav", {"display": "block"})
218+
219+
set-local-storage: {"rustdoc-hide-toc": "true"}
220+
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
221+
assert-css: ("#TOC", {"display": "none"})
222+
assert-false: ".sidebar .in-crate"
223+
set-local-storage: {"rustdoc-hide-toc": "false"}
224+
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
225+
assert-css: ("#TOC", {"display": "block"})
226+
assert-false: ".sidebar .in-crate"
227+
228+
set-local-storage: {"rustdoc-hide-modnav": "true"}
229+
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
230+
assert-css: ("#ModNav", {"display": "none"})
231+
set-local-storage: {"rustdoc-hide-modnav": "false"}
232+
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
233+
assert-css: ("#ModNav", {"display": "block"})

0 commit comments

Comments
 (0)