Skip to content

Commit d2fd428

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

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,10 @@ impl<'a, 'b, 'ids, I: Iterator<Item = SpannedEvent<'a>>> Iterator
558558

559559
let level =
560560
std::cmp::min(level as u32 + (self.heading_offset as u32), MAX_HEADER_LEVEL);
561-
self.buf.push_back((Event::Html(format!("</a></h{level}>").into()), 0..0));
561+
self.buf.push_back((Event::Html(format!("</h{level}>").into()), 0..0));
562562

563-
let start_tags = format!(
564-
"<h{level} id=\"{id}\">\
565-
<a href=\"#{id}\">",
566-
);
563+
let start_tags =
564+
format!("<h{level} id=\"{id}\"><a class=\"doc-anchor\" href=\"#{id}\">§</a>");
567565
return Some((Event::Html(start_tags.into()), 0..0));
568566
}
569567
event

src/librustdoc/html/markdown/tests.rs

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

314-
t("# Foo bar", "<h2 id=\"foo-bar\"><a href=\"#foo-bar\">Foo bar</a></h2>");
314+
t(
315+
"# Foo bar",
316+
"<h2 id=\"foo-bar\"><a class=\"doc-anchor\" href=\"#foo-bar\">§</a>Foo bar</h2>",
317+
);
315318
t(
316319
"## Foo-bar_baz qux",
317320
"<h3 id=\"foo-bar_baz-qux\">\
318-
<a href=\"#foo-bar_baz-qux\">Foo-bar_baz qux</a></h3>",
321+
<a class=\"doc-anchor\" href=\"#foo-bar_baz-qux\">§</a>\
322+
Foo-bar_baz qux\
323+
</h3>",
319324
);
320325
t(
321326
"### **Foo** *bar* baz!?!& -_qux_-%",
322327
"<h4 id=\"foo-bar-baz--qux-\">\
323-
<a href=\"#foo-bar-baz--qux-\"><strong>Foo</strong> \
324-
<em>bar</em> baz!?!&amp; -<em>qux</em>-%</a>\
328+
<a class=\"doc-anchor\" href=\"#foo-bar-baz--qux-\">§</a>\
329+
<strong>Foo</strong> <em>bar</em> baz!?!&amp; -<em>qux</em>-%\
325330
</h4>",
326331
);
327332
t(
328333
"#### **Foo?** & \\*bar?!* _`baz`_ ❤ #qux",
329334
"<h5 id=\"foo--bar--baz--qux\">\
330-
<a href=\"#foo--bar--baz--qux\"><strong>Foo?</strong> &amp; *bar?!* \
331-
<em><code>baz</code></em> ❤ #qux</a>\
335+
<a class=\"doc-anchor\" href=\"#foo--bar--baz--qux\">§</a>\
336+
<strong>Foo?</strong> &amp; *bar?!* <em><code>baz</code></em> ❤ #qux\
332337
</h5>",
333338
);
334339
}
@@ -351,12 +356,36 @@ fn test_header_ids_multiple_blocks() {
351356
assert_eq!(output, expect, "original: {}", input);
352357
}
353358

354-
t(&mut map, "# Example", "<h2 id=\"example\"><a href=\"#example\">Example</a></h2>");
355-
t(&mut map, "# Panics", "<h2 id=\"panics\"><a href=\"#panics\">Panics</a></h2>");
356-
t(&mut map, "# Example", "<h2 id=\"example-1\"><a href=\"#example-1\">Example</a></h2>");
357-
t(&mut map, "# Search", "<h2 id=\"search-1\"><a href=\"#search-1\">Search</a></h2>");
358-
t(&mut map, "# Example", "<h2 id=\"example-2\"><a href=\"#example-2\">Example</a></h2>");
359-
t(&mut map, "# Panics", "<h2 id=\"panics-1\"><a href=\"#panics-1\">Panics</a></h2>");
359+
t(
360+
&mut map,
361+
"# Example",
362+
"<h2 id=\"example\"><a class=\"doc-anchor\" href=\"#example\">§</a>Example</h2>",
363+
);
364+
t(
365+
&mut map,
366+
"# Panics",
367+
"<h2 id=\"panics\"><a class=\"doc-anchor\" href=\"#panics\">§</a>Panics</h2>",
368+
);
369+
t(
370+
&mut map,
371+
"# Example",
372+
"<h2 id=\"example-1\"><a class=\"doc-anchor\" href=\"#example-1\">§</a>Example</h2>",
373+
);
374+
t(
375+
&mut map,
376+
"# Search",
377+
"<h2 id=\"search-1\"><a class=\"doc-anchor\" href=\"#search-1\">§</a>Search</h2>",
378+
);
379+
t(
380+
&mut map,
381+
"# Example",
382+
"<h2 id=\"example-2\"><a class=\"doc-anchor\" href=\"#example-2\">§</a>Example</h2>",
383+
);
384+
t(
385+
&mut map,
386+
"# Panics",
387+
"<h2 id=\"panics-1\"><a class=\"doc-anchor\" href=\"#panics-1\">§</a>Panics</h2>",
388+
);
360389
}
361390

362391
#[test]

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,9 @@ nav.sub {
768768
h2.small-section-header > .anchor {
769769
padding-right: 6px;
770770
}
771+
.doc-anchor {
772+
margin-right: 6px;
773+
}
771774

772775
.main-heading a:hover,
773776
.example-wrap .rust a:hover,

0 commit comments

Comments
 (0)