Skip to content

Commit

Permalink
fix: support block quote callouts (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Aug 16, 2024
1 parent e4fdabc commit d1ce3f1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/generation/gen_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ impl<'a> Context<'a> {
self.is_in_list_count > 0
}

pub fn is_in_block_quote(&self) -> bool {
self.is_in_block_quote_count > 0
}

pub fn with_no_text_wrap<T>(&mut self, func: impl FnOnce(&mut Context) -> T) -> T {
self.text_wrap_disabled_count += 1;
let items = func(self);
Expand Down
36 changes: 27 additions & 9 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use super::common::*;
use super::gen_types::*;
use super::utils;
use crate::configuration::*;
use dprint_core::formatting::condition_resolvers;
use dprint_core::formatting::conditions::*;
use dprint_core::formatting::ir_helpers::*;
Expand All @@ -11,6 +7,11 @@ use std::borrow::Cow;
use std::rc::Rc;
use unicode_width::UnicodeWidthStr;

use super::common::*;
use super::gen_types::*;
use super::utils;
use crate::configuration::*;

pub fn generate(node: &Node, context: &mut Context) -> PrintItems {
// eprintln!("Kind: {:?}", node.kind());
// eprintln!("Text: {:?}", node.text(context));
Expand Down Expand Up @@ -38,7 +39,6 @@ pub fn generate(node: &Node, context: &mut Context) -> PrintItems {
Node::List(node) => gen_list(node, false, context),
Node::Item(node) => gen_item(node, context),
Node::TaskListMarker(_) => unreachable!("this should be handled by gen_paragraph"),
Node::TaskListMarker(_) => unreachable!("this should be handled by gen_paragraph"),
Node::HorizontalRule(node) => gen_horizontal_rule(node, context),
Node::SoftBreak(_) => PrintItems::new(),
Node::HardBreak(_) => gen_hard_break(context),
Expand Down Expand Up @@ -75,9 +75,7 @@ fn gen_nodes(nodes: &[Node], context: &mut Context) -> PrintItems {
let mut last_node: Option<&Node> = None;
let mut node_iterator = nodes.iter().filter(|n| !matches!(n, Node::SoftBreak(_)));

while let Some(node) = node_iterator.next() {
let mut node = node;

while let Some(mut node) = node_iterator.next() {
// handle alternate lists
if let Some(Node::List(last_list)) = &last_node {
if let Node::List(list) = &node {
Expand Down Expand Up @@ -136,7 +134,21 @@ fn gen_nodes(nodes: &[Node], context: &mut Context) -> PrintItems {
let new_line_count = context.get_new_lines_in_range(between_range.0, between_range.1);

if new_line_count == 1 {
if matches!(node, Node::Html(_)) {
// Callout example:
// > [!NOTE]
// > Some note.
let is_callout = if context.is_in_block_quote() && matches!(node, Node::Text(_)) {
if let Node::Text(text) = last_node {
is_callout_text(&text.text)
} else {
false
}
} else {
false
};
if is_callout && !context.is_text_wrap_disabled() {
items.push_signal(Signal::NewLine); // force a newline
} else if matches!(node, Node::Html(_)) {
items.push_signal(Signal::NewLine);
} else {
items.extend(get_newline_wrapping_based_on_config(context));
Expand All @@ -163,6 +175,7 @@ fn gen_nodes(nodes: &[Node], context: &mut Context) -> PrintItems {
if node.starts_with_list_word() {
items.push_space();
} else {
if matches!(last_node, Node::Text(_)) && matches!(node, Node::Text(_)) {}
items.extend(get_space_or_newline_based_on_config(context));
}
}
Expand Down Expand Up @@ -423,6 +436,11 @@ fn gen_text(text: &Text, context: &mut Context) -> PrintItems {
gen_str(&text.text, context)
}

fn is_callout_text(text: &str) -> bool {
// ex. [!NOTE]
text.starts_with("[!") && text.ends_with("]") && text[2..text.len() - 1].chars().all(|c| c.is_ascii_uppercase())
}

fn gen_str(text: &str, context: &mut Context) -> PrintItems {
let mut text_builder = TextBuilder::new(context);

Expand Down
24 changes: 24 additions & 0 deletions tests/specs/BlockQuotes/Callouts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
~~ textWrap: always ~~
!! should format !!
> [!NOTE]
> Some sort of note

[expect]
> [!NOTE]
> Some sort of note

!! should format when just a callout !!
> [!NOTE]

[expect]
> [!NOTE]

!! should format when has blank line !!
> [!NOTE]
>
> Some sort of note

[expect]
> [!NOTE]
>
> Some sort of note

0 comments on commit d1ce3f1

Please sign in to comment.