Skip to content

Commit 41b3a75

Browse files
committed
fix(md): remove the leading space of the first paragraph in note card
1 parent addd1c4 commit 41b3a75

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

crates/rari-md/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ mod test {
202202
Ok(())
203203
}
204204

205+
#[test]
206+
fn note_zh_locale() -> Result<(), anyhow::Error> {
207+
let out = m2h("> [!NOTE]\n> This paragraph should have no leading spaces", Locale::ZhCn)?;
208+
assert_eq!(
209+
out,
210+
"<div class=\"notecard note\" data-add-note data-sourcepos=\"1:1-2:46\">\n<p data-sourcepos=\"1:3-2:46\">This paragraph should have no leading spaces</p>\n</div>\n"
211+
);
212+
Ok(())
213+
}
214+
205215
#[test]
206216
fn escape_hrefs() -> Result<(), anyhow::Error> {
207217
fn eh(s: &str) -> Result<String, anyhow::Error> {

crates/rari-md/src/node_card.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub(crate) fn is_callout<'a>(block_quote: &'a AstNode<'a>, locale: Locale) -> Op
8989
}
9090
if text.starts_with(NoteCard::Warning.new_prefix()) {
9191
if text.trim() == NoteCard::Warning.new_prefix() {
92+
remove_leading_space_if_zh_locale(marker, locale);
9293
marker.detach();
9394
} else if let Some(tail) = text.strip_prefix(NoteCard::Warning.new_prefix()) {
9495
data.value = NodeValue::Text(tail.trim().to_string());
@@ -97,6 +98,7 @@ pub(crate) fn is_callout<'a>(block_quote: &'a AstNode<'a>, locale: Locale) -> Op
9798
}
9899
if text.starts_with(NoteCard::Note.new_prefix()) {
99100
if text.trim() == NoteCard::Note.new_prefix() {
101+
remove_leading_space_if_zh_locale(marker, locale);
100102
marker.detach();
101103
} else if let Some(tail) = text.strip_prefix(NoteCard::Note.new_prefix()) {
102104
data.value = NodeValue::Text(tail.trim().to_string());
@@ -109,6 +111,23 @@ pub(crate) fn is_callout<'a>(block_quote: &'a AstNode<'a>, locale: Locale) -> Op
109111
None
110112
}
111113

114+
fn remove_leading_space_if_zh_locale(node: &AstNode, locale: Locale) {
115+
if !matches!(locale, Locale::ZhCn | Locale::ZhTw) {
116+
return;
117+
}
118+
// If the next sibling is a soft break, remove it to avoid extra space. Example raw markdown:
119+
//
120+
// ```
121+
// > [!NOTE]
122+
// > This is a note.
123+
// ```
124+
if let Some(next_sibling) = node.next_sibling() {
125+
if matches!(next_sibling.data.borrow().value, NodeValue::SoftBreak) {
126+
next_sibling.detach();
127+
}
128+
}
129+
}
130+
112131
/// Returns the default title for an alert type
113132
pub fn alert_type_default_title(alert_type: &AlertType) -> String {
114133
match *alert_type {

0 commit comments

Comments
 (0)