diff options
| author | Guillaume Gomez <guillaume.gomez@huawei.com> | 2022-02-25 15:09:37 +0100 |
|---|---|---|
| committer | Guillaume Gomez <guillaume.gomez@huawei.com> | 2023-12-05 15:59:39 +0100 |
| commit | 8bc123e321b82359c80ce50dae62e13db39c96cc (patch) | |
| tree | cbfb8e346e5e96fdc17e1e685165cc00641dca6d | |
| parent | cf8d81213c193fc0ca9f2b99165476f8ae028295 (diff) | |
| download | rust-8bc123e321b82359c80ce50dae62e13db39c96cc.tar.gz rust-8bc123e321b82359c80ce50dae62e13db39c96cc.zip | |
Rework doc blocks headings by not turning them into links anymore and putting an anchor to their left side
| -rw-r--r-- | src/librustdoc/html/markdown.rs | 8 | ||||
| -rw-r--r-- | src/librustdoc/html/markdown/tests.rs | 53 | ||||
| -rw-r--r-- | src/librustdoc/html/static/css/rustdoc.css | 3 |
3 files changed, 47 insertions, 17 deletions
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index abc27bcdf07..6a48b9367b8 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -558,12 +558,10 @@ impl<'a, 'b, 'ids, I: Iterator<Item = SpannedEvent<'a>>> Iterator let level = std::cmp::min(level as u32 + (self.heading_offset as u32), MAX_HEADER_LEVEL); - self.buf.push_back((Event::Html(format!("</a></h{level}>").into()), 0..0)); + self.buf.push_back((Event::Html(format!("</h{level}>").into()), 0..0)); - let start_tags = format!( - "<h{level} id=\"{id}\">\ - <a href=\"#{id}\">", - ); + let start_tags = + format!("<h{level} id=\"{id}\"><a class=\"doc-anchor\" href=\"#{id}\">§</a>"); return Some((Event::Html(start_tags.into()), 0..0)); } event diff --git a/src/librustdoc/html/markdown/tests.rs b/src/librustdoc/html/markdown/tests.rs index 5eba1d0609f..fcf77783024 100644 --- a/src/librustdoc/html/markdown/tests.rs +++ b/src/librustdoc/html/markdown/tests.rs @@ -311,24 +311,29 @@ fn test_header() { assert_eq!(output, expect, "original: {}", input); } - t("# Foo bar", "<h2 id=\"foo-bar\"><a href=\"#foo-bar\">Foo bar</a></h2>"); + t( + "# Foo bar", + "<h2 id=\"foo-bar\"><a class=\"doc-anchor\" href=\"#foo-bar\">§</a>Foo bar</h2>", + ); t( "## Foo-bar_baz qux", "<h3 id=\"foo-bar_baz-qux\">\ - <a href=\"#foo-bar_baz-qux\">Foo-bar_baz qux</a></h3>", + <a class=\"doc-anchor\" href=\"#foo-bar_baz-qux\">§</a>\ + Foo-bar_baz qux\ + </h3>", ); t( "### **Foo** *bar* baz!?!& -_qux_-%", "<h4 id=\"foo-bar-baz--qux-\">\ - <a href=\"#foo-bar-baz--qux-\"><strong>Foo</strong> \ - <em>bar</em> baz!?!& -<em>qux</em>-%</a>\ + <a class=\"doc-anchor\" href=\"#foo-bar-baz--qux-\">§</a>\ + <strong>Foo</strong> <em>bar</em> baz!?!& -<em>qux</em>-%\ </h4>", ); t( "#### **Foo?** & \\*bar?!* _`baz`_ ❤ #qux", "<h5 id=\"foo--bar--baz--qux\">\ - <a href=\"#foo--bar--baz--qux\"><strong>Foo?</strong> & *bar?!* \ - <em><code>baz</code></em> ❤ #qux</a>\ + <a class=\"doc-anchor\" href=\"#foo--bar--baz--qux\">§</a>\ + <strong>Foo?</strong> & *bar?!* <em><code>baz</code></em> ❤ #qux\ </h5>", ); } @@ -351,12 +356,36 @@ fn test_header_ids_multiple_blocks() { assert_eq!(output, expect, "original: {}", input); } - t(&mut map, "# Example", "<h2 id=\"example\"><a href=\"#example\">Example</a></h2>"); - t(&mut map, "# Panics", "<h2 id=\"panics\"><a href=\"#panics\">Panics</a></h2>"); - t(&mut map, "# Example", "<h2 id=\"example-1\"><a href=\"#example-1\">Example</a></h2>"); - t(&mut map, "# Search", "<h2 id=\"search-1\"><a href=\"#search-1\">Search</a></h2>"); - t(&mut map, "# Example", "<h2 id=\"example-2\"><a href=\"#example-2\">Example</a></h2>"); - t(&mut map, "# Panics", "<h2 id=\"panics-1\"><a href=\"#panics-1\">Panics</a></h2>"); + t( + &mut map, + "# Example", + "<h2 id=\"example\"><a class=\"doc-anchor\" href=\"#example\">§</a>Example</h2>", + ); + t( + &mut map, + "# Panics", + "<h2 id=\"panics\"><a class=\"doc-anchor\" href=\"#panics\">§</a>Panics</h2>", + ); + t( + &mut map, + "# Example", + "<h2 id=\"example-1\"><a class=\"doc-anchor\" href=\"#example-1\">§</a>Example</h2>", + ); + t( + &mut map, + "# Search", + "<h2 id=\"search-1\"><a class=\"doc-anchor\" href=\"#search-1\">§</a>Search</h2>", + ); + t( + &mut map, + "# Example", + "<h2 id=\"example-2\"><a class=\"doc-anchor\" href=\"#example-2\">§</a>Example</h2>", + ); + t( + &mut map, + "# Panics", + "<h2 id=\"panics-1\"><a class=\"doc-anchor\" href=\"#panics-1\">§</a>Panics</h2>", + ); } #[test] diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index b898eb5d381..22fe5d8b0cf 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -765,6 +765,9 @@ nav.sub { h2.section-header > .anchor { padding-right: 6px; } +.doc-anchor { + margin-right: 6px; +} .main-heading a:hover, .example-wrap .rust a:hover, |
