diff options
author | Jan-Erik Rediger <janerik@fnordig.de> | 2023-01-09 21:51:21 +0100 |
---|---|---|
committer | Jan-Erik Rediger <janerik@fnordig.de> | 2023-01-09 21:51:21 +0100 |
commit | 35e0f51672daaa679f965a95d109daaa4fece4ea (patch) | |
tree | b0f59a0732c9d1ba9f54d2baa3475af3e7a3ab40 | |
parent | dee9ba5e5ffc9a76d16dd6f1fedc19a15881c094 (diff) |
Handle empty documents.
Fixes #31
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | tests/empty_document.in.md | 3 | ||||
-rw-r--r-- | tests/empty_document.out.md | 2 | ||||
-rw-r--r-- | tests/it.rs | 7 |
4 files changed, 13 insertions, 1 deletions
@@ -229,7 +229,7 @@ fn add_toc(content: &str, cfg: &Config) -> Result<String> { log::trace!("content_after_toc={:?}", content_after_toc); // Multiline markers might have consumed trailing newlines, // 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 { "\n" diff --git a/tests/empty_document.in.md b/tests/empty_document.in.md new file mode 100644 index 0000000..b5041f2 --- /dev/null +++ b/tests/empty_document.in.md @@ -0,0 +1,3 @@ +# Chapter 1 + +<!-- toc --> diff --git a/tests/empty_document.out.md b/tests/empty_document.out.md new file mode 100644 index 0000000..6786da2 --- /dev/null +++ b/tests/empty_document.out.md @@ -0,0 +1,2 @@ +# Chapter 1 + diff --git a/tests/it.rs b/tests/it.rs index 474b2dc..7101ded 100644 --- a/tests/it.rs +++ b/tests/it.rs @@ -157,3 +157,10 @@ fn backslash_escapes() { // Backslash-escaped elements should still be escaped. assert_toc!("backslash_escapes"); } + +#[test] +fn empty_document() { + // Regression test #31 + // Empty documents should not fail + assert_toc!("empty_document"); +} |