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 Feb 7, 2024
1 parent 0d4df6c commit a1c1c10
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 44 deletions.
5 changes: 4 additions & 1 deletion src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,10 @@ pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> Strin
s
}

pub(crate) fn plain_text_from_events<'a>(events: impl Iterator<Item=pulldown_cmark::Event<'a>>, s: &mut String) {
pub(crate) fn plain_text_from_events<'a>(
events: impl Iterator<Item = pulldown_cmark::Event<'a>>,
s: &mut String,
) {
for event in events {
match &event {
Event::Text(text) => s.push_str(text),
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,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
59 changes: 35 additions & 24 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 @@ -156,26 +164,32 @@ fn docblock_toc<'a>(
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.entries.into_iter().map(|entry| {
Link {
name: entry.name.into(),
href: entry.id.into(),
children: entry.children.entries.into_iter().map(|entry| Link {
}
.into_parts();
let links: Vec<Link<'_>> = toc
.entries
.into_iter()
.map(|entry| {
Link {
name: entry.name.into(),
href: entry.id.into(),
// Only a single level of nesting is shown here.
// Going the full six could break the layout,
// so we have to cut it off somewhere.
children: vec![],
}).collect()
}
}).collect();
if links.is_empty() {
None
} else {
Some(LinkBlock::new(Link::new("#", "Sections"), "top-toc", links))
}
children: entry
.children
.entries
.into_iter()
.map(|entry| Link {
name: entry.name.into(),
href: entry.id.into(),
// Only a single level of nesting is shown here.
// Going the full six could break the layout,
// so we have to cut it off somewhere.
children: vec![],
})
.collect(),
}
})
.collect();
if links.is_empty() { None } else { Some(LinkBlock::new(Link::new("", ""), "top-toc", links)) }
}

fn sidebar_struct<'a>(
Expand Down Expand Up @@ -505,10 +519,7 @@ pub(crate) fn sidebar_module_like(
LinkBlock::new(Link::empty(), "", item_sections)
}

fn sidebar_module(
items: &[clean::Item],
ids: &mut IdMap,
) -> LinkBlock<'static> {
fn sidebar_module(items: &[clean::Item], ids: &mut IdMap) -> LinkBlock<'static> {
let item_sections_in_use: FxHashSet<_> = items
.iter()
.filter(|it| {
Expand Down
8 changes: 8 additions & 0 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,14 @@ ul.block, .block li, .block ul {
margin-right: 0.25rem;
}

.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 @@ -460,7 +460,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 @@ -845,7 +845,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 @@ -128,8 +128,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 @@ -139,8 +139,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 @@ -184,3 +184,36 @@ assert-property: (".sidebar .sidebar-crate h2 a", {
"offsetTop": |index_sidebar_y|,
"offsetLeft": |index_sidebar_x|,
})

// 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"})
2 changes: 2 additions & 0 deletions tests/rustdoc/sidebar/top-toc-html.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore-tidy-linelength

#![crate_name = "foo"]
#![feature(lazy_type_alias)]
#![allow(incomplete_features)]
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc/sidebar/top-toc-idmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![allow(incomplete_features)]

//! # Structs
//!
//!
//! This header has the same name as a built-in header,
//! and we need to make sure they're disambiguated with
//! suffixes.
Expand Down

0 comments on commit a1c1c10

Please sign in to comment.