1
Fork 0

Rework doc blocks headings by not turning them into links anymore and putting an anchor to their left side

This commit is contained in:
Guillaume Gomez 2022-02-25 15:09:37 +01:00
parent cf8d81213c
commit 8bc123e321
3 changed files with 47 additions and 17 deletions

View file

@ -558,12 +558,10 @@ impl<'a, 'b, 'ids, I: Iterator<Item = SpannedEvent<'a>>> Iterator
let level = let level =
std::cmp::min(level as u32 + (self.heading_offset as u32), MAX_HEADER_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!( let start_tags =
"<h{level} id=\"{id}\">\ format!("<h{level} id=\"{id}\"><a class=\"doc-anchor\" href=\"#{id}\">§</a>");
<a href=\"#{id}\">",
);
return Some((Event::Html(start_tags.into()), 0..0)); return Some((Event::Html(start_tags.into()), 0..0));
} }
event event

View file

@ -311,24 +311,29 @@ fn test_header() {
assert_eq!(output, expect, "original: {}", input); 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( t(
"## Foo-bar_baz qux", "## Foo-bar_baz qux",
"<h3 id=\"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( t(
"### **Foo** *bar* baz!?!& -_qux_-%", "### **Foo** *bar* baz!?!& -_qux_-%",
"<h4 id=\"foo-bar-baz--qux-\">\ "<h4 id=\"foo-bar-baz--qux-\">\
<a href=\"#foo-bar-baz--qux-\"><strong>Foo</strong> \ <a class=\"doc-anchor\" href=\"#foo-bar-baz--qux-\">§</a>\
<em>bar</em> baz!?!&amp; -<em>qux</em>-%</a>\ <strong>Foo</strong> <em>bar</em> baz!?!&amp; -<em>qux</em>-%\
</h4>", </h4>",
); );
t( t(
"#### **Foo?** & \\*bar?!* _`baz`_ ❤ #qux", "#### **Foo?** & \\*bar?!* _`baz`_ ❤ #qux",
"<h5 id=\"foo--bar--baz--qux\">\ "<h5 id=\"foo--bar--baz--qux\">\
<a href=\"#foo--bar--baz--qux\"><strong>Foo?</strong> &amp; *bar?!* \ <a class=\"doc-anchor\" href=\"#foo--bar--baz--qux\">§</a>\
<em><code>baz</code></em> #qux</a>\ <strong>Foo?</strong> &amp; *bar?!* <em><code>baz</code></em> #qux\
</h5>", </h5>",
); );
} }
@ -351,12 +356,36 @@ fn test_header_ids_multiple_blocks() {
assert_eq!(output, expect, "original: {}", input); assert_eq!(output, expect, "original: {}", input);
} }
t(&mut map, "# Example", "<h2 id=\"example\"><a href=\"#example\">Example</a></h2>"); t(
t(&mut map, "# Panics", "<h2 id=\"panics\"><a href=\"#panics\">Panics</a></h2>"); &mut map,
t(&mut map, "# Example", "<h2 id=\"example-1\"><a href=\"#example-1\">Example</a></h2>"); "# Example",
t(&mut map, "# Search", "<h2 id=\"search-1\"><a href=\"#search-1\">Search</a></h2>"); "<h2 id=\"example\"><a class=\"doc-anchor\" href=\"#example\">§</a>Example</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,
"# 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] #[test]

View file

@ -765,6 +765,9 @@ nav.sub {
h2.section-header > .anchor { h2.section-header > .anchor {
padding-right: 6px; padding-right: 6px;
} }
.doc-anchor {
margin-right: 6px;
}
.main-heading a:hover, .main-heading a:hover,
.example-wrap .rust a:hover, .example-wrap .rust a:hover,