Skip to content

Commit 9b97045

Browse files
Rework doc blocks headings by not turning them into links anymore and putting an anchor to their left side
1 parent b8967b0 commit 9b97045

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -560,14 +560,10 @@ impl<'a, 'b, 'ids, I: Iterator<Item = SpannedEvent<'a>>> Iterator
560560

561561
let level =
562562
std::cmp::min(level as u32 + (self.heading_offset as u32), MAX_HEADER_LEVEL);
563-
self.buf.push_back((Event::Html(format!("</a></h{}>", level).into()), 0..0));
564-
565-
let start_tags = format!(
566-
"<h{level} id=\"{id}\">\
567-
<a href=\"#{id}\">",
568-
id = id,
569-
level = level
570-
);
563+
self.buf.push_back((Event::Html(format!("</h{level}>").into()), 0..0));
564+
565+
let start_tags =
566+
format!("<h{level} id=\"{id}\"><a class=\"doc-anchor\" href=\"#{id}\">§</a>");
571567
return Some((Event::Html(start_tags.into()), 0..0));
572568
}
573569
event

src/librustdoc/html/markdown/tests.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,24 +159,29 @@ fn test_header() {
159159
assert_eq!(output, expect, "original: {}", input);
160160
}
161161

162-
t("# Foo bar", "<h2 id=\"foo-bar\"><a href=\"#foo-bar\">Foo bar</a></h2>");
162+
t(
163+
"# Foo bar",
164+
"<h2 id=\"foo-bar\"><a class=\"doc-anchor\" href=\"#foo-bar\">§</a>Foo bar</h2>",
165+
);
163166
t(
164167
"## Foo-bar_baz qux",
165168
"<h3 id=\"foo-bar_baz-qux\">\
166-
<a href=\"#foo-bar_baz-qux\">Foo-bar_baz qux</a></h3>",
169+
<a class=\"doc-anchor\" href=\"#foo-bar_baz-qux\">§</a>\
170+
Foo-bar_baz qux\
171+
</h3>",
167172
);
168173
t(
169174
"### **Foo** *bar* baz!?!& -_qux_-%",
170175
"<h4 id=\"foo-bar-baz--qux-\">\
171-
<a href=\"#foo-bar-baz--qux-\"><strong>Foo</strong> \
172-
<em>bar</em> baz!?!&amp; -<em>qux</em>-%</a>\
176+
<a class=\"doc-anchor\" href=\"#foo-bar-baz--qux-\">§</a>\
177+
<strong>Foo</strong> <em>bar</em> baz!?!&amp; -<em>qux</em>-%\
173178
</h4>",
174179
);
175180
t(
176181
"#### **Foo?** & \\*bar?!* _`baz`_ ❤ #qux",
177182
"<h5 id=\"foo--bar--baz--qux\">\
178-
<a href=\"#foo--bar--baz--qux\"><strong>Foo?</strong> &amp; *bar?!* \
179-
<em><code>baz</code></em> ❤ #qux</a>\
183+
<a class=\"doc-anchor\" href=\"#foo--bar--baz--qux\">§</a>\
184+
<strong>Foo?</strong> &amp; *bar?!* <em><code>baz</code></em> ❤ #qux\
180185
</h5>",
181186
);
182187
}
@@ -198,12 +203,36 @@ fn test_header_ids_multiple_blocks() {
198203
assert_eq!(output, expect, "original: {}", input);
199204
}
200205

201-
t(&mut map, "# Example", "<h2 id=\"example\"><a href=\"#example\">Example</a></h2>");
202-
t(&mut map, "# Panics", "<h2 id=\"panics\"><a href=\"#panics\">Panics</a></h2>");
203-
t(&mut map, "# Example", "<h2 id=\"example-1\"><a href=\"#example-1\">Example</a></h2>");
204-
t(&mut map, "# Search", "<h2 id=\"search-1\"><a href=\"#search-1\">Search</a></h2>");
205-
t(&mut map, "# Example", "<h2 id=\"example-2\"><a href=\"#example-2\">Example</a></h2>");
206-
t(&mut map, "# Panics", "<h2 id=\"panics-1\"><a href=\"#panics-1\">Panics</a></h2>");
206+
t(
207+
&mut map,
208+
"# Example",
209+
"<h2 id=\"example\"><a class=\"doc-anchor\" href=\"#example\">§</a>Example</h2>",
210+
);
211+
t(
212+
&mut map,
213+
"# Panics",
214+
"<h2 id=\"panics\"><a class=\"doc-anchor\" href=\"#panics\">§</a>Panics</h2>",
215+
);
216+
t(
217+
&mut map,
218+
"# Example",
219+
"<h2 id=\"example-1\"><a class=\"doc-anchor\" href=\"#example-1\">§</a>Example</h2>",
220+
);
221+
t(
222+
&mut map,
223+
"# Search",
224+
"<h2 id=\"search-1\"><a class=\"doc-anchor\" href=\"#search-1\">§</a>Search</h2>",
225+
);
226+
t(
227+
&mut map,
228+
"# Example",
229+
"<h2 id=\"example-2\"><a class=\"doc-anchor\" href=\"#example-2\">§</a>Example</h2>",
230+
);
231+
t(
232+
&mut map,
233+
"# Panics",
234+
"<h2 id=\"panics-1\"><a class=\"doc-anchor\" href=\"#panics-1\">§</a>Panics</h2>",
235+
);
207236
}
208237

209238
#[test]

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,9 @@ h2.small-section-header > .anchor {
833833
.anchor::before {
834834
content: '§';
835835
}
836+
.doc-anchor {
837+
margin-right: 6px;
838+
}
836839

837840
.docblock a:not(.srclink):not(.test-arrow):hover,
838841
.docblock-short a:not(.srclink):not(.test-arrow):hover, .item-info a {

0 commit comments

Comments
 (0)