Skip to content

Commit d1ce3f1

Browse files
authored
fix: support block quote callouts (#115)
1 parent e4fdabc commit d1ce3f1

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

src/generation/gen_types.rs

+4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ impl<'a> Context<'a> {
108108
self.is_in_list_count > 0
109109
}
110110

111+
pub fn is_in_block_quote(&self) -> bool {
112+
self.is_in_block_quote_count > 0
113+
}
114+
111115
pub fn with_no_text_wrap<T>(&mut self, func: impl FnOnce(&mut Context) -> T) -> T {
112116
self.text_wrap_disabled_count += 1;
113117
let items = func(self);

src/generation/generate.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
use super::common::*;
2-
use super::gen_types::*;
3-
use super::utils;
4-
use crate::configuration::*;
51
use dprint_core::formatting::condition_resolvers;
62
use dprint_core::formatting::conditions::*;
73
use dprint_core::formatting::ir_helpers::*;
@@ -11,6 +7,11 @@ use std::borrow::Cow;
117
use std::rc::Rc;
128
use unicode_width::UnicodeWidthStr;
139

10+
use super::common::*;
11+
use super::gen_types::*;
12+
use super::utils;
13+
use crate::configuration::*;
14+
1415
pub fn generate(node: &Node, context: &mut Context) -> PrintItems {
1516
// eprintln!("Kind: {:?}", node.kind());
1617
// eprintln!("Text: {:?}", node.text(context));
@@ -38,7 +39,6 @@ pub fn generate(node: &Node, context: &mut Context) -> PrintItems {
3839
Node::List(node) => gen_list(node, false, context),
3940
Node::Item(node) => gen_item(node, context),
4041
Node::TaskListMarker(_) => unreachable!("this should be handled by gen_paragraph"),
41-
Node::TaskListMarker(_) => unreachable!("this should be handled by gen_paragraph"),
4242
Node::HorizontalRule(node) => gen_horizontal_rule(node, context),
4343
Node::SoftBreak(_) => PrintItems::new(),
4444
Node::HardBreak(_) => gen_hard_break(context),
@@ -75,9 +75,7 @@ fn gen_nodes(nodes: &[Node], context: &mut Context) -> PrintItems {
7575
let mut last_node: Option<&Node> = None;
7676
let mut node_iterator = nodes.iter().filter(|n| !matches!(n, Node::SoftBreak(_)));
7777

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

138136
if new_line_count == 1 {
139-
if matches!(node, Node::Html(_)) {
137+
// Callout example:
138+
// > [!NOTE]
139+
// > Some note.
140+
let is_callout = if context.is_in_block_quote() && matches!(node, Node::Text(_)) {
141+
if let Node::Text(text) = last_node {
142+
is_callout_text(&text.text)
143+
} else {
144+
false
145+
}
146+
} else {
147+
false
148+
};
149+
if is_callout && !context.is_text_wrap_disabled() {
150+
items.push_signal(Signal::NewLine); // force a newline
151+
} else if matches!(node, Node::Html(_)) {
140152
items.push_signal(Signal::NewLine);
141153
} else {
142154
items.extend(get_newline_wrapping_based_on_config(context));
@@ -163,6 +175,7 @@ fn gen_nodes(nodes: &[Node], context: &mut Context) -> PrintItems {
163175
if node.starts_with_list_word() {
164176
items.push_space();
165177
} else {
178+
if matches!(last_node, Node::Text(_)) && matches!(node, Node::Text(_)) {}
166179
items.extend(get_space_or_newline_based_on_config(context));
167180
}
168181
}
@@ -423,6 +436,11 @@ fn gen_text(text: &Text, context: &mut Context) -> PrintItems {
423436
gen_str(&text.text, context)
424437
}
425438

439+
fn is_callout_text(text: &str) -> bool {
440+
// ex. [!NOTE]
441+
text.starts_with("[!") && text.ends_with("]") && text[2..text.len() - 1].chars().all(|c| c.is_ascii_uppercase())
442+
}
443+
426444
fn gen_str(text: &str, context: &mut Context) -> PrintItems {
427445
let mut text_builder = TextBuilder::new(context);
428446

tests/specs/BlockQuotes/Callouts.txt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
~~ textWrap: always ~~
2+
!! should format !!
3+
> [!NOTE]
4+
> Some sort of note
5+
6+
[expect]
7+
> [!NOTE]
8+
> Some sort of note
9+
10+
!! should format when just a callout !!
11+
> [!NOTE]
12+
13+
[expect]
14+
> [!NOTE]
15+
16+
!! should format when has blank line !!
17+
> [!NOTE]
18+
>
19+
> Some sort of note
20+
21+
[expect]
22+
> [!NOTE]
23+
>
24+
> Some sort of note

0 commit comments

Comments
 (0)