From a655d5d2419d314003c12047c41685f5de5d68ff Mon Sep 17 00:00:00 2001 From: rnitta Date: Mon, 3 Jun 2019 21:31:15 +0900 Subject: [PATCH] Header elements wrap links (#948) * swap hierarchy of header for that of link * fix comment --- src/renderer/html_handlebars/hbs_renderer.rs | 24 ++++++++++---------- src/theme/css/general.css | 8 +++---- tests/rendered_output.rs | 5 +++- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 8f3c3320..e4c2faf4 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -496,8 +496,8 @@ fn make_data( Ok(data) } -/// Goes through the rendered HTML, making sure all header tags are wrapped in -/// an anchor so people can link to sections directly. +/// Goes through the rendered HTML, making sure all header tags have +/// an anchor respectively so people can link to sections directly. fn build_header_links(html: &str) -> String { let regex = Regex::new(r"(.*?)").unwrap(); let mut id_counter = HashMap::new(); @@ -508,14 +508,14 @@ fn build_header_links(html: &str) -> String { .parse() .expect("Regex should ensure we only ever get numbers here"); - wrap_header_with_link(level, &caps[2], &mut id_counter) + insert_link_into_header(level, &caps[2], &mut id_counter) }) .into_owned() } -/// Wraps a single header tag with a link, making sure each tag gets its own +/// Insert a sinle link into a header, making sure each link gets its own /// unique ID by appending an auto-incremented number (if necessary). -fn wrap_header_with_link( +fn insert_link_into_header( level: usize, content: &str, id_counter: &mut HashMap, @@ -532,7 +532,7 @@ fn wrap_header_with_link( *id_count += 1; format!( - r##"{text}"##, + r##"{text}"##, level = level, id = id, text = content @@ -640,27 +640,27 @@ mod tests { let inputs = vec![ ( "blah blah

Foo

", - r##"blah blah

Foo

"##, + r##"blah blah

Foo

"##, ), ( "

Foo

", - r##"

Foo

"##, + r##"

Foo

"##, ), ( "

Foo^bar

", - r##"

Foo^bar

"##, + r##"

Foo^bar

"##, ), ( "

", - r##"

"##, + r##"

"##, ), ( "

", - r##"

"##, + r##"

"##, ), ( "

Foo

Foo

", - r##"

Foo

Foo

"##, + r##"

Foo

Foo

"##, ), ]; diff --git a/src/theme/css/general.css b/src/theme/css/general.css index f0aefa2a..57d5d7a6 100644 --- a/src/theme/css/general.css +++ b/src/theme/css/general.css @@ -34,10 +34,10 @@ h4, h5 { margin-top: 2em; } margin-top: 1em; } -a.header:target h1:before, -a.header:target h2:before, -a.header:target h3:before, -a.header:target h4:before { +h1 a.header:target::before, +h2 a.header:target::before, +h3 a.header:target::before, +h4 a.header:target::before { display: inline-block; content: "»"; margin-left: -30px; diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index dfee1793..dd579903 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -327,7 +327,10 @@ fn able_to_include_files_in_chapters() { let includes = temp.path().join("book/first/includes.html"); - let summary_strings = &["

Summary

", ">First Chapter"]; + let summary_strings = &[ + r##"

Summary

"##, + ">First Chapter", + ]; assert_contains_strings(&includes, summary_strings); assert_doesnt_contain_strings(&includes, &["{{#include ../SUMMARY.md::}}"]);