1
Fork 0

Handle empty documents.

Fixes #31
This commit is contained in:
Jan-Erik Rediger 2023-01-09 21:51:21 +01:00
parent dee9ba5e5f
commit 35e0f51672
4 changed files with 13 additions and 1 deletions

View file

@ -229,7 +229,7 @@ fn add_toc(content: &str, cfg: &Config) -> Result<String> {
log::trace!("content_after_toc={:?}", content_after_toc); log::trace!("content_after_toc={:?}", content_after_toc);
// Multiline markers might have consumed trailing newlines, // Multiline markers might have consumed trailing newlines,
// we ensure there's always one before the content. // we ensure there's always one before the content.
let extra = if content_after_toc.as_bytes()[0] == b'\n' { let extra = if content_after_toc.is_empty() || content_after_toc.as_bytes()[0] == b'\n' {
"" ""
} else { } else {
"\n" "\n"

View file

@ -0,0 +1,3 @@
# Chapter 1
<!-- toc -->

View file

@ -0,0 +1,2 @@
# Chapter 1

View file

@ -157,3 +157,10 @@ fn backslash_escapes() {
// Backslash-escaped elements should still be escaped. // Backslash-escaped elements should still be escaped.
assert_toc!("backslash_escapes"); assert_toc!("backslash_escapes");
} }
#[test]
fn empty_document() {
// Regression test #31
// Empty documents should not fail
assert_toc!("empty_document");
}