1
Fork 0

Support inline-code in headers used for ToC

This commit is contained in:
Jan-Erik Rediger 2019-08-21 09:10:23 -07:00
parent aa59d5a11d
commit 39470841bc

View file

@ -80,8 +80,13 @@ fn add_toc(content: &str) -> Result<String> {
continue; continue;
} }
if let Event::Text(header) = e { match e {
toc_content.push((current_header_level.unwrap(), header)); Event::Text(header) => toc_content.push((current_header_level.unwrap(), header)),
Event::Code(code) => {
let text = format!("`{}`", code);
toc_content.push((current_header_level.unwrap(), text.into()));
}
_ => {} // Rest is unhandled
} }
} }
@ -158,4 +163,38 @@ mod test {
assert_eq!(expected, add_toc(content).unwrap()); assert_eq!(expected, add_toc(content).unwrap());
} }
#[test]
fn adds_toc_with_inline_code() {
let content = r#"# Chapter
<!-- toc -->
# Header 1
## `Header 1.1`
# Header 2
## Header 2.1
"#;
let expected = r#"# Chapter
* [Header 1](#header-1)
* [`Header 1.1`](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
# Header 1
## `Header 1.1`
# Header 2
## Header 2.1"#;
assert_eq!(expected, add_toc(content).unwrap());
}
} }