Skip to content

Commit 54a757d

Browse files
committed
Add configuration options to hide TOC or module navigation
1 parent 898e56b commit 54a757d

File tree

8 files changed

+109
-22
lines changed

8 files changed

+109
-22
lines changed

src/librustdoc/html/render/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
633633
title: "",
634634
is_crate: false,
635635
is_mod: false,
636+
parent_is_crate: false,
636637
blocks: vec![blocks],
637638
path: String::new(),
638639
};

src/librustdoc/html/render/sidebar.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub(super) struct Sidebar<'a> {
1919
pub(super) title_prefix: &'static str,
2020
pub(super) title: &'a str,
2121
pub(super) is_crate: bool,
22+
pub(super) parent_is_crate: bool,
2223
pub(super) is_mod: bool,
2324
pub(super) blocks: Vec<LinkBlock<'a>>,
2425
pub(super) path: String,
@@ -126,8 +127,15 @@ pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buf
126127
} else {
127128
"".into()
128129
};
129-
let sidebar =
130-
Sidebar { title_prefix, title, is_mod: it.is_mod(), is_crate: it.is_crate(), blocks, path };
130+
let sidebar = Sidebar {
131+
title_prefix,
132+
title,
133+
is_mod: it.is_mod(),
134+
is_crate: it.is_crate(),
135+
parent_is_crate: sidebar_path.len() == 1,
136+
blocks,
137+
path,
138+
};
131139
sidebar.render_into(buffer).unwrap();
132140
}
133141

@@ -155,7 +163,6 @@ fn docblock_toc<'a>(
155163
error_codes: cx.shared.codes,
156164
edition: cx.shared.edition(),
157165
playground: &cx.shared.playground,
158-
custom_code_classes_in_docs: cx.tcx().features().custom_code_classes_in_docs,
159166
}
160167
.into_parts();
161168
let links: Vec<Link<'_>> = toc
@@ -184,7 +191,7 @@ fn docblock_toc<'a>(
184191
if links.is_empty() {
185192
None
186193
} else {
187-
Some(LinkBlock::new(Link::new("#", "Sections"), "top-toc", links))
194+
Some(LinkBlock::new(Link::new("", "Sections"), "top-toc", links))
188195
}
189196
}
190197

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

+8
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,14 @@ ul.block, .block li, .block ul {
567567
margin-right: 0.25rem;
568568
}
569569

570+
.hide-toc #TOC, .hide-toc .in-crate {
571+
display: none;
572+
}
573+
574+
.hide-modnav #ModNav {
575+
display: none;
576+
}
577+
570578
.sidebar h2 {
571579
overflow-wrap: anywhere;
572580
padding: 0;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ function preLoadCss(cssUrl) {
493493
if (!window.SIDEBAR_ITEMS) {
494494
return;
495495
}
496-
const sidebar = document.getElementsByClassName("sidebar-elems")[0];
496+
const sidebar = document.getElementById("ModNav");
497497

498498
/**
499499
* Append to the sidebar a "block" of links - a heading along with a list (`<ul>`) of items.
@@ -878,7 +878,7 @@ function preLoadCss(cssUrl) {
878878
if (!window.ALL_CRATES) {
879879
return;
880880
}
881-
const sidebarElems = document.getElementsByClassName("sidebar-elems")[0];
881+
const sidebarElems = document.getElementById("ModNav");
882882
if (!sidebarElems) {
883883
return;
884884
}

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

+29
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

+9-4
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

+12-8
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}}</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}}</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}}</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}}</a></h2>
44+
<h2{% if parent_is_crate +%} class="in-crate"{% endif %}> {# #}
45+
<a href="{% if is_mod %}../{% endif %}index.html">In {{+ path}}</a> {# #}
46+
</h2> {# #}
4447
{% endif %}
48+
</div> {# #}
4549
</div>

tests/rustdoc-gui/sidebar.goml

+37-4
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ assert-count: (".sidebar .location", 1)
123123
// - Module name, followed by TOC for module headings
124124
// - "In crate [name]" parent pointer, followed by sibling navigation
125125
assert-count: (".sidebar h2", 3)
126-
assert-text: (".sidebar > .sidebar-elems > h2", "In crate lib2")
127-
assert-property: (".sidebar > .sidebar-elems > h2 > a", {
126+
assert-text: (".sidebar > .sidebar-elems > #ModNav > h2", "In crate lib2")
127+
assert-property: (".sidebar > .sidebar-elems > #ModNav > h2 > a", {
128128
"href": "/lib2/index.html",
129129
}, ENDS_WITH)
130130
// We check that we don't have the crate list.
@@ -134,8 +134,8 @@ go-to: "./sub_module/sub_sub_module/index.html"
134134
assert-property: (".sidebar", {"clientWidth": "200"})
135135
assert-text: (".sidebar > .sidebar-crate > h2 > a", "lib2")
136136
assert-text: (".sidebar .location", "Module sub_sub_module")
137-
assert-text: (".sidebar > .sidebar-elems > h2", "In lib2::module::sub_module")
138-
assert-property: (".sidebar > .sidebar-elems > h2 > a", {
137+
assert-text: (".sidebar > .sidebar-elems > #ModNav > h2", "In lib2::module::sub_module")
138+
assert-property: (".sidebar > .sidebar-elems > #ModNav > h2 > a", {
139139
"href": "/module/sub_module/index.html",
140140
}, ENDS_WITH)
141141
// We check that we don't have the crate list.
@@ -179,3 +179,36 @@ assert-property: (".sidebar .sidebar-crate h2 a", {
179179
"offsetTop": |index_sidebar_y|,
180180
"offsetLeft": |index_sidebar_x|,
181181
})
182+
183+
// Configuration option to show TOC in sidebar.
184+
set-local-storage: {"rustdoc-hide-toc": "true"}
185+
go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html"
186+
assert-css: ("#TOC", {"display": "none"})
187+
assert-css: (".sidebar .in-crate", {"display": "none"})
188+
set-local-storage: {"rustdoc-hide-toc": "false"}
189+
go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html"
190+
assert-css: ("#TOC", {"display": "block"})
191+
assert-css: (".sidebar .in-crate", {"display": "block"})
192+
193+
set-local-storage: {"rustdoc-hide-modnav": "true"}
194+
go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html"
195+
assert-css: ("#ModNav", {"display": "none"})
196+
set-local-storage: {"rustdoc-hide-modnav": "false"}
197+
go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html"
198+
assert-css: ("#ModNav", {"display": "block"})
199+
200+
set-local-storage: {"rustdoc-hide-toc": "true"}
201+
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
202+
assert-css: ("#TOC", {"display": "none"})
203+
assert-false: ".sidebar .in-crate"
204+
set-local-storage: {"rustdoc-hide-toc": "false"}
205+
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
206+
assert-css: ("#TOC", {"display": "block"})
207+
assert-false: ".sidebar .in-crate"
208+
209+
set-local-storage: {"rustdoc-hide-modnav": "true"}
210+
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
211+
assert-css: ("#ModNav", {"display": "none"})
212+
set-local-storage: {"rustdoc-hide-modnav": "false"}
213+
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
214+
assert-css: ("#ModNav", {"display": "block"})

0 commit comments

Comments
 (0)