From 34e5ef22a099416300b4ba68a714c98539009c54 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sun, 28 May 2023 11:32:31 -0700 Subject: [PATCH] Don't include empty class attribute. --- src/renderer/html_handlebars/hbs_renderer.rs | 29 ++++++++++++++------ tests/rendered_output.rs | 6 ++-- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index a4e65fab..e753dc2e 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -832,10 +832,12 @@ fn insert_link_into_header( id_counter: &mut HashMap, ) -> String { let id = id.unwrap_or_else(|| utils::unique_id_from_content(content, id_counter)); - let classes = classes.unwrap_or("".to_string()); + let classes = classes + .map(|s| format!(" class=\"{s}\"")) + .unwrap_or_default(); format!( - r##"{text}"##, + r##"{text}"##, level = level, id = id, text = content, @@ -1014,28 +1016,39 @@ 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

"##, ), + // id only + ( + r##"

Foo

"##, + r##"

Foo

"##, + ), + // class only + ( + r##"

Foo

"##, + r##"

Foo

"##, + ), + // both id and class ( r##"

Foo

"##, r##"

Foo

"##, diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index 9fffe7bc..813f70fd 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -105,12 +105,12 @@ fn check_correct_cross_links_in_nested_dir() { assert_contains_strings( first.join("index.html"), - &[r##"

"##], + &[r##"

"##], ); assert_contains_strings( first.join("nested.html"), - &[r##"

"##], + &[r##"

"##], ); } @@ -393,7 +393,7 @@ fn able_to_include_files_in_chapters() { let includes = temp.path().join("book/first/includes.html"); let summary_strings = &[ - r##"

Summary

"##, + r##"

Summary

"##, ">First Chapter", ]; assert_contains_strings(&includes, summary_strings);