Skip to content

Commit

Permalink
fix: write block quote kind only once
Browse files Browse the repository at this point in the history
  • Loading branch information
max-heller authored and Byron committed Oct 5, 2024
1 parent 848abc3 commit 2f75de2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 38 deletions.
34 changes: 18 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,28 +461,30 @@ where
formatter.write_char(' ')
}
BlockQuote(kind) => {
if let Some(kind) = kind {
let kind = match kind {
BlockQuoteKind::Note => "NOTE",
BlockQuoteKind::Tip => "TIP",
BlockQuoteKind::Important => "IMPORTANT",
BlockQuoteKind::Warning => "WARNING",
BlockQuoteKind::Caution => "CAUTION",
};
state.padding.push(format!(" > [!{kind}]\n > ").into());
} else {
state.padding.push(" > ".into());
}
let every_line_padding = " > ";
let first_line_padding = kind
.map(|kind| {
let kind = match kind {
BlockQuoteKind::Note => "NOTE",
BlockQuoteKind::Tip => "TIP",
BlockQuoteKind::Important => "IMPORTANT",
BlockQuoteKind::Warning => "WARNING",
BlockQuoteKind::Caution => "CAUTION",
};
Cow::from(format!("{every_line_padding}[!{kind}]"))
})
.unwrap_or(every_line_padding.into());
state.newlines_before_start = 1;

// if we consumed some newlines, we know that we can just write out the next
// level in our blockquote. This should work regardless if we have other
// padding or if we're in a list
if consumed_newlines {
formatter.write_str(" > ")
} else {
formatter.write_char('\n').and(padding(formatter, &state.padding))
if !consumed_newlines {
formatter.write_char('\n').and(padding(formatter, &state.padding))?
}
formatter.write_str(&first_line_padding)?;
state.padding.push(every_line_padding.into());
Ok(())
}
CodeBlock(pulldown_cmark::CodeBlockKind::Indented) => {
state.code_block = Some(CodeBlockKind::Indented);
Expand Down
26 changes: 14 additions & 12 deletions tests/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,14 @@ mod start {
#[test]
fn blockquote() {
assert_eq!(s(Start(BlockQuote(None))), "\n > ");
assert_eq!(s(Start(BlockQuote(Some(BlockQuoteKind::Note)))), "\n > [!NOTE]\n > ");
assert_eq!(s(Start(BlockQuote(Some(BlockQuoteKind::Tip)))), "\n > [!TIP]\n > ");
assert_eq!(s(Start(BlockQuote(Some(BlockQuoteKind::Note)))), "\n > [!NOTE]");
assert_eq!(s(Start(BlockQuote(Some(BlockQuoteKind::Tip)))), "\n > [!TIP]");
assert_eq!(
s(Start(BlockQuote(Some(BlockQuoteKind::Important)))),
"\n > [!IMPORTANT]\n > "
);
assert_eq!(
s(Start(BlockQuote(Some(BlockQuoteKind::Warning)))),
"\n > [!WARNING]\n > "
);
assert_eq!(
s(Start(BlockQuote(Some(BlockQuoteKind::Caution)))),
"\n > [!CAUTION]\n > "
"\n > [!IMPORTANT]"
);
assert_eq!(s(Start(BlockQuote(Some(BlockQuoteKind::Warning)))), "\n > [!WARNING]");
assert_eq!(s(Start(BlockQuote(Some(BlockQuoteKind::Caution)))), "\n > [!CAUTION]");
}
#[test]
fn codeblock() {
Expand Down Expand Up @@ -192,7 +186,7 @@ mod start {
}

mod end {
use pulldown_cmark::{CodeBlockKind, Event::*, HeadingLevel, LinkType::*, Tag, TagEnd};
use pulldown_cmark::{BlockQuoteKind, CodeBlockKind, CowStr, Event::*, HeadingLevel, LinkType::*, Tag, TagEnd};

use super::{es, s};

Expand All @@ -213,6 +207,14 @@ mod end {
#[test]
fn blockquote() {
assert_eq!(s(End(TagEnd::BlockQuote(None))), "");
assert_eq!(
es([
Start(Tag::BlockQuote(Some(BlockQuoteKind::Note))),
Text(CowStr::Borrowed("This is a note")),
End(TagEnd::BlockQuote(Some(BlockQuoteKind::Note)))
]),
"\n > [!NOTE]\n > This is a note"
);
}
#[test]
fn codeblock() {
Expand Down
5 changes: 0 additions & 5 deletions tests/fixtures/snapshots/stupicat-event-by-event-output
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,18 @@ Block quotes
> eirmod tempor invidunt ut `labore et dolore magna` aliquyam erat, sed diam
> voluptua.

>
> [!NOTE]
> Highlights information that users should take into account, even when skimming.

>
> [!TIP]
> Optional information to help a user be more successful.

>
> [!IMPORTANT]
> Crucial information necessary for users to succeed.

>
> [!WARNING]
> Critical content demanding immediate user attention due to potential risks.

>
> [!CAUTION]
> Negative potential consequences of an action.

Expand Down
5 changes: 0 additions & 5 deletions tests/fixtures/snapshots/stupicat-output
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,18 @@ Block quotes
> eirmod tempor invidunt ut `labore et dolore magna` aliquyam erat, sed diam
> voluptua.

>
> [!NOTE]
> Highlights information that users should take into account, even when skimming.

>
> [!TIP]
> Optional information to help a user be more successful.

>
> [!IMPORTANT]
> Crucial information necessary for users to succeed.

>
> [!WARNING]
> Critical content demanding immediate user attention due to potential risks.

>
> [!CAUTION]
> Negative potential consequences of an action.

Expand Down

0 comments on commit 2f75de2

Please sign in to comment.