Skip to content

Commit

Permalink
Add configuration options to hide TOC or module navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
notriddle committed Jul 18, 2024
1 parent f862bd8 commit ef32d5b
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
title: "",
is_crate: false,
is_mod: false,
parent_is_crate: false,
blocks: vec![blocks],
path: String::new(),
};
Expand Down
15 changes: 11 additions & 4 deletions src/librustdoc/html/render/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub(super) struct Sidebar<'a> {
pub(super) title_prefix: &'static str,
pub(super) title: &'a str,
pub(super) is_crate: bool,
pub(super) parent_is_crate: bool,
pub(super) is_mod: bool,
pub(super) blocks: Vec<LinkBlock<'a>>,
pub(super) path: String,
Expand Down Expand Up @@ -126,8 +127,15 @@ pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buf
} else {
"".into()
};
let sidebar =
Sidebar { title_prefix, title, is_mod: it.is_mod(), is_crate: it.is_crate(), blocks, path };
let sidebar = Sidebar {
title_prefix,
title,
is_mod: it.is_mod(),
is_crate: it.is_crate(),
parent_is_crate: sidebar_path.len() == 1,
blocks,
path,
};
sidebar.render_into(buffer).unwrap();
}

Expand Down Expand Up @@ -155,7 +163,6 @@ fn docblock_toc<'a>(
error_codes: cx.shared.codes,
edition: cx.shared.edition(),
playground: &cx.shared.playground,
custom_code_classes_in_docs: cx.tcx().features().custom_code_classes_in_docs,
}
.into_parts();
let links: Vec<Link<'_>> = toc
Expand Down Expand Up @@ -184,7 +191,7 @@ fn docblock_toc<'a>(
if links.is_empty() {
None
} else {
Some(LinkBlock::new(Link::new("#", "Sections"), "top-toc", links))
Some(LinkBlock::new(Link::new("", "Sections"), "top-toc", links))
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,8 @@ ul.block, .block li, .block ul {
list-style: none;
}

.block ul {
margin-left: 12px;
.block ul a {
padding-left: 1rem;
}

.sidebar-elems a,
Expand All @@ -571,6 +571,14 @@ ul.block, .block li, .block ul {
background-clip: border-box;
}

.hide-toc #TOC, .hide-toc .in-crate {
display: none;
}

.hide-modnav #ModNav {
display: none;
}

.sidebar h2 {
overflow-wrap: anywhere;
padding: 0;
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ function preLoadCss(cssUrl) {
if (!window.SIDEBAR_ITEMS) {
return;
}
const sidebar = document.getElementsByClassName("sidebar-elems")[0];
const sidebar = document.getElementById("ModNav");

/**
* Append to the sidebar a "block" of links - a heading along with a list (`<ul>`) of items.
Expand Down Expand Up @@ -878,7 +878,7 @@ function preLoadCss(cssUrl) {
if (!window.ALL_CRATES) {
return;
}
const sidebarElems = document.getElementsByClassName("sidebar-elems")[0];
const sidebarElems = document.getElementById("ModNav");
if (!sidebarElems) {
return;
}
Expand Down
29 changes: 29 additions & 0 deletions src/librustdoc/html/static/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@
removeClass(document.documentElement, "hide-sidebar");
}
break;
case "hide-toc":
if (value === true) {
addClass(document.documentElement, "hide-toc");
} else {
removeClass(document.documentElement, "hide-toc");
}
break;
case "hide-modnav":
if (value === true) {
addClass(document.documentElement, "hide-modnav");
} else {
removeClass(document.documentElement, "hide-modnav");
}
break;
}
}

Expand Down Expand Up @@ -102,6 +116,11 @@
let output = "";

for (const setting of settings) {
if (setting === "hr") {
output += "<hr>";
continue;
}

const js_data_name = setting["js_name"];
const setting_name = setting["name"];

Expand Down Expand Up @@ -198,6 +217,16 @@
"js_name": "hide-sidebar",
"default": false,
},
{
"name": "Hide table of contents",
"js_name": "hide-toc",
"default": false,
},
{
"name": "Hide module navigation",
"js_name": "hide-modnav",
"default": false,
},
{
"name": "Disable keyboard shortcuts",
"js_name": "disable-shortcuts",
Expand Down
13 changes: 9 additions & 4 deletions src/librustdoc/html/static/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,21 @@ updateTheme();
// This needs to be done here because this JS is render-blocking,
// so that the sidebar doesn't "jump" after appearing on screen.
// The user interaction to change this is set up in main.js.
//
// At this point in page load, `document.body` is not available yet.
// Set a class on the `<html>` element instead.
if (getSettingValue("source-sidebar-show") === "true") {
// At this point in page load, `document.body` is not available yet.
// Set a class on the `<html>` element instead.
addClass(document.documentElement, "src-sidebar-expanded");
}
if (getSettingValue("hide-sidebar") === "true") {
// At this point in page load, `document.body` is not available yet.
// Set a class on the `<html>` element instead.
addClass(document.documentElement, "hide-sidebar");
}
if (getSettingValue("hide-toc") === "true") {
addClass(document.documentElement, "hide-toc");
}
if (getSettingValue("hide-modnav") === "true") {
addClass(document.documentElement, "hide-modnav");
}
function updateSidebarWidth() {
const desktopSidebarWidth = getSettingValue("desktop-sidebar-width");
if (desktopSidebarWidth && desktopSidebarWidth !== "null") {
Expand Down
20 changes: 12 additions & 8 deletions src/librustdoc/html/templates/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
{% if !title.is_empty() %}
<h2 class="location"> {# #}
<a href="#">{{title_prefix}}{{title}}</a> {# #}
</h2>
{% endif %}
<div class="sidebar-elems">
{% if is_crate %}
<ul class="block"> {# #}
Expand All @@ -11,11 +6,16 @@ <h2 class="location"> {# #}
{% endif %}

{% if self.should_render_blocks() %}
<section>
<section id="TOC">
{% if !title.is_empty() %}
<h2 class="location"> {# #}
<a href="#">{{title_prefix}}{{title}}</a> {# #}
</h2>
{% endif %}
{% for block in blocks %}
{% if block.should_render() %}
{% if !block.heading.name.is_empty() %}
<h3{% if !block.class.is_empty() +%} class="{{block.class}}"{% endif %}> {# #}
<h3> {# #}
<a href="#{{block.heading.href|safe}}">{{block.heading.name}}</a> {# #}
</h3> {# #}
{% endif %}
Expand All @@ -39,7 +39,11 @@ <h2 class="location"> {# #}
{% endfor %}
</section>
{% endif %}
<div id="ModNav">
{% if !path.is_empty() %}
<h2><a href="{% if is_mod %}../{% endif %}index.html">In {{+ path}}</a></h2>
<h2{% if parent_is_crate +%} class="in-crate"{% endif %}> {# #}
<a href="{% if is_mod %}../{% endif %}index.html">In {{+ path}}</a> {# #}
</h2> {# #}
{% endif %}
</div> {# #}
</div>
41 changes: 37 additions & 4 deletions tests/rustdoc-gui/sidebar.goml
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ assert-count: (".sidebar .location", 1)
// - Module name, followed by TOC for module headings
// - "In crate [name]" parent pointer, followed by sibling navigation
assert-count: (".sidebar h2", 3)
assert-text: (".sidebar > .sidebar-elems > h2", "In crate lib2")
assert-property: (".sidebar > .sidebar-elems > h2 > a", {
assert-text: (".sidebar > .sidebar-elems > #ModNav > h2", "In crate lib2")
assert-property: (".sidebar > .sidebar-elems > #ModNav > h2 > a", {
"href": "/lib2/index.html",
}, ENDS_WITH)
// We check that we don't have the crate list.
Expand All @@ -134,8 +134,8 @@ go-to: "./sub_module/sub_sub_module/index.html"
assert-property: (".sidebar", {"clientWidth": "200"})
assert-text: (".sidebar > .sidebar-crate > h2 > a", "lib2")
assert-text: (".sidebar .location", "Module sub_sub_module")
assert-text: (".sidebar > .sidebar-elems > h2", "In lib2::module::sub_module")
assert-property: (".sidebar > .sidebar-elems > h2 > a", {
assert-text: (".sidebar > .sidebar-elems > #ModNav > h2", "In lib2::module::sub_module")
assert-property: (".sidebar > .sidebar-elems > #ModNav > h2 > a", {
"href": "/module/sub_module/index.html",
}, ENDS_WITH)
// We check that we don't have the crate list.
Expand Down Expand Up @@ -194,3 +194,36 @@ assert-position-false: (".sidebar-crate > h2 > a", {"x": -3})
// when line-wrapped, see that it becomes flush-left again
drag-and-drop: ((205, 100), (108, 100))
assert-position: (".sidebar-crate > h2 > a", {"x": -3})

// Configuration option to show TOC in sidebar.
set-local-storage: {"rustdoc-hide-toc": "true"}
go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html"
assert-css: ("#TOC", {"display": "none"})
assert-css: (".sidebar .in-crate", {"display": "none"})
set-local-storage: {"rustdoc-hide-toc": "false"}
go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html"
assert-css: ("#TOC", {"display": "block"})
assert-css: (".sidebar .in-crate", {"display": "block"})

set-local-storage: {"rustdoc-hide-modnav": "true"}
go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html"
assert-css: ("#ModNav", {"display": "none"})
set-local-storage: {"rustdoc-hide-modnav": "false"}
go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html"
assert-css: ("#ModNav", {"display": "block"})

set-local-storage: {"rustdoc-hide-toc": "true"}
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
assert-css: ("#TOC", {"display": "none"})
assert-false: ".sidebar .in-crate"
set-local-storage: {"rustdoc-hide-toc": "false"}
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
assert-css: ("#TOC", {"display": "block"})
assert-false: ".sidebar .in-crate"

set-local-storage: {"rustdoc-hide-modnav": "true"}
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
assert-css: ("#ModNav", {"display": "none"})
set-local-storage: {"rustdoc-hide-modnav": "false"}
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
assert-css: ("#ModNav", {"display": "block"})

0 comments on commit ef32d5b

Please sign in to comment.