From 467ff966ebc1d8a0bebd8a9fa7c084e8b0ec6893 Mon Sep 17 00:00:00 2001 From: msrd0 Date: Mon, 13 May 2024 15:09:37 +0200 Subject: [PATCH] Fix dropping newlines, breaking some code blocks (#450) Turns out that codeblocks, while usually being formatted as one text event containing all the lines, sometimes have multiple text events, each ending with a newline. When removing those newlines, they are (a) missing in the output, and (b) pulldown-cmark-to-cmark chokes on the indent of the end of codeblock (https://github.com/Byron/pulldown-cmark-to-cmark/issues/69). Fixes #440 --- src/output.rs | 5 ++++- tests/pass/code-block-in-list/Cargo.lock | 7 +++++++ tests/pass/code-block-in-list/Cargo.toml | 8 ++++++++ tests/pass/code-block-in-list/README.j2 | 1 + tests/pass/code-block-in-list/README.md | 13 +++++++++++++ tests/pass/code-block-in-list/lib.rs | 12 ++++++++++++ 6 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/pass/code-block-in-list/Cargo.lock create mode 100644 tests/pass/code-block-in-list/Cargo.toml create mode 100644 tests/pass/code-block-in-list/README.j2 create mode 100644 tests/pass/code-block-in-list/README.md create mode 100644 tests/pass/code-block-in-list/lib.rs diff --git a/src/output.rs b/src/output.rs index 829f7d2..64640a0 100644 --- a/src/output.rs +++ b/src/output.rs @@ -310,13 +310,16 @@ impl<'a, I: Iterator>> Iterator for EventFilter<'a, I> { }), Event::Text(text) if self.in_code_block => { - let filtered = text + let mut filtered = text .lines() .filter(|line| !is_hidden_codeblock_line(line)) .join("\n"); if filtered.is_empty() { continue; } + if text.ends_with('\n') { + filtered.push('\n'); + } Event::Text(filtered.into()) }, diff --git a/tests/pass/code-block-in-list/Cargo.lock b/tests/pass/code-block-in-list/Cargo.lock new file mode 100644 index 0000000..f809142 --- /dev/null +++ b/tests/pass/code-block-in-list/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "code_block_in_list" +version = "0.0.0" diff --git a/tests/pass/code-block-in-list/Cargo.toml b/tests/pass/code-block-in-list/Cargo.toml new file mode 100644 index 0000000..9c8e86b --- /dev/null +++ b/tests/pass/code-block-in-list/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "code_block_in_list" +version = "0.0.0" +publish = false +edition = "2021" + +[lib] +path = "lib.rs" diff --git a/tests/pass/code-block-in-list/README.j2 b/tests/pass/code-block-in-list/README.j2 new file mode 100644 index 0000000..e38bb54 --- /dev/null +++ b/tests/pass/code-block-in-list/README.j2 @@ -0,0 +1 @@ +{{readme}} \ No newline at end of file diff --git a/tests/pass/code-block-in-list/README.md b/tests/pass/code-block-in-list/README.md new file mode 100644 index 0000000..e5349a8 --- /dev/null +++ b/tests/pass/code-block-in-list/README.md @@ -0,0 +1,13 @@ +This is an example testing code blocks in lists + +* in the past, this has caused issues + +* one of such cases was [\#440][__link0] + +* therefore, we have this example/test: + + ```rust + struct Foo { + bar: usize + } + ``` diff --git a/tests/pass/code-block-in-list/lib.rs b/tests/pass/code-block-in-list/lib.rs new file mode 100644 index 0000000..5076ed5 --- /dev/null +++ b/tests/pass/code-block-in-list/lib.rs @@ -0,0 +1,12 @@ +//! This is an example testing code blocks in lists +//! +//! - in the past, this has caused issues +//! - one of such cases was [#440](https://github.com/msrd0/cargo-doc2readme/issues/440) +//! - therefore, we have this example/test: +//! +//! ```rust +//! # #[derive(Clone, Copy)] +//! struct Foo { +//! bar: usize +//! } +//! ```