1
Fork 0

Don't write the TOC if no marker was found

Fixes #13
This commit is contained in:
Jan-Erik Rediger 2021-04-06 18:50:54 +02:00
parent dd92ae0ec3
commit 2001e01ffa
2 changed files with 53 additions and 9 deletions

View file

@ -20,4 +20,3 @@ toml = "0.5.6"
[dev-dependencies] [dev-dependencies]
pretty_assertions = "0.6.1" pretty_assertions = "0.6.1"
env_logger = "0.7.1"

View file

@ -154,6 +154,11 @@ fn add_toc(content: &str, cfg: &Config) -> Result<String> {
c += 1; c += 1;
log::trace!("Event: {:?}", e); log::trace!("Event: {:?}", e);
if !toc_found { if !toc_found {
log::trace!(
"TOC not found yet. Location: {}, Start: {}",
mark_loc,
mark_start
);
if e == mark[mark_loc] { if e == mark[mark_loc] {
if mark_start == -1 { if mark_start == -1 {
mark_start = c; mark_start = c;
@ -209,25 +214,20 @@ fn add_toc(content: &str, cfg: &Config) -> Result<String> {
} }
let toc = build_toc(&toc_content); let toc = build_toc(&toc_content);
log::trace!("Built TOC: {:?}", toc);
let toc_events = Parser::new(&toc).collect::<Vec<_>>(); let toc_events = Parser::new(&toc).collect::<Vec<_>>();
let mut c = -1; let mut c = -1;
let events = Parser::new_ext(&content, opts) let events = Parser::new_ext(&content, opts)
.map(|e| { .map(|e| {
c += 1; c += 1;
if c > mark_start && c < mark_start + (mark.len() as i32) { if toc_found && c > mark_start && c < mark_start + (mark.len() as i32) {
vec![] vec![]
} else if c == mark_start { } else if toc_found && c == mark_start {
toc_events.clone() toc_events.clone()
} else { } else {
vec![e] vec![e]
} }
// if let Event::Html(html) = e.clone() {
// if &*html == marker {
// return toc_events.clone();
// }
// }
// vec![e]
}) })
.flatten(); .flatten();
@ -712,4 +712,49 @@ text"#;
assert_eq!(expected, add_toc(content, &with_max_level(7)).unwrap()); assert_eq!(expected, add_toc(content, &with_max_level(7)).unwrap());
} }
// Regression test for [#13](https://github.com/badboy/mdbook-toc/issues/13).
// Choosing a non-HTML TOC marker breaks sites that don't use it at all,
// removed the header and first paragraph.
#[test]
fn nonhtml_marker_no_toc_in_page() {
let _ = env_logger::builder().is_test(true).try_init();
let marker = "[[_TOC_]]".to_owned();
let content = r#"# Chapter
First paragraph
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1
"#;
let expected = r#"# Chapter
First paragraph
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1"#;
assert_eq!(expected, add_toc(content, &with_marker(marker)).unwrap());
}
} }