Skip to content

Commit

Permalink
Fix markdown newlines around heading and code blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
gyscos committed Jan 9, 2025
1 parent c60958e commit d10f1cc
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions cursive-core/src/utils/markup/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ fn cowvert(cow: CowStr) -> Cow<str> {

/// Iterator that parse a markdown text and outputs styled spans.
pub struct Parser<'a> {
/// Did we process the first item already?
///
/// Used to omit newlines before the very first block.
first: bool,
stack: Vec<Style>,
input: &'a str,
Expand Down Expand Up @@ -82,22 +85,30 @@ impl<'a> Iterator for Parser<'a> {
// Add to the stack!
Tag::Emphasis => self.stack.push(Style::from(Effect::Italic)),
Tag::Heading { level, .. } => {
return Some(self.literal(format!("{} ", heading(level as usize))))
let lines = if self.first { "" } else { "\n\n" };
return Some(self.literal(format!(
"{}{} ",
lines,
heading(level as usize)
)));
}
Tag::BlockQuote(_) => return Some(self.literal("> ")),
Tag::Link {
dest_url, title, ..
} => return Some(self.literal(format!("[{title}]({dest_url})"))),
Tag::CodeBlock(_) => return Some(self.literal("```")),
Tag::CodeBlock(_) => return Some(self.literal("```\n")),
Tag::Strong => self.stack.push(Style::from(Effect::Bold)),
Tag::Paragraph if !self.first => return Some(self.literal("\n\n")),
_ => (),
},
Event::End(tag) => match tag {
// Remove from stack!
TagEnd::Paragraph if self.first => self.first = false,
TagEnd::Heading(..) => return Some(self.literal("\n\n")),
TagEnd::CodeBlock => return Some(self.literal("```")),
TagEnd::Heading(..) => self.first = false,
TagEnd::CodeBlock => {
self.first = false;
return Some(self.literal("```"));
}
TagEnd::Emphasis | TagEnd::Strong => {
self.stack.pop().unwrap();
}
Expand Down

0 comments on commit d10f1cc

Please sign in to comment.