Skip to content

Commit

Permalink
Fix dropping newlines, breaking some code blocks (#450)
Browse files Browse the repository at this point in the history
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 (Byron/pulldown-cmark-to-cmark#69).

Fixes #440
  • Loading branch information
msrd0 authored May 13, 2024
1 parent fa621ac commit 467ff96
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,16 @@ impl<'a, I: Iterator<Item = Event<'a>>> 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())
},

Expand Down
7 changes: 7 additions & 0 deletions tests/pass/code-block-in-list/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/pass/code-block-in-list/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "code_block_in_list"
version = "0.0.0"
publish = false
edition = "2021"

[lib]
path = "lib.rs"
1 change: 1 addition & 0 deletions tests/pass/code-block-in-list/README.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{readme}}
13 changes: 13 additions & 0 deletions tests/pass/code-block-in-list/README.md
Original file line number Diff line number Diff line change
@@ -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
}
```
12 changes: 12 additions & 0 deletions tests/pass/code-block-in-list/lib.rs
Original file line number Diff line number Diff line change
@@ -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
//! }
//! ```

0 comments on commit 467ff96

Please sign in to comment.