Skip to content

Commit e496f93

Browse files
committed
refactor: Migrate the markdown feature from Dart to Rust
1 parent 28a4b9a commit e496f93

File tree

3 files changed

+392
-1
lines changed

3 files changed

+392
-1
lines changed
Lines changed: 387 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,387 @@
1+
use crate::core::{Delta, DeltaIterator};
2+
use crate::rich_text::{is_block, RichTextAttributeKey, RichTextAttributeValue, RichTextAttributes};
3+
use std::collections::HashMap;
4+
5+
const LINEFEEDASCIICODE: i32 = 0x0A;
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use crate::codec::markdown::markdown_encoder::markdown_encoder;
10+
use crate::rich_text::RichTextDelta;
11+
12+
#[test]
13+
fn markdown_encoder_header_1_test() {
14+
let json = r#"[{"insert":"header 1"},{"insert":"\n","attributes":{"header":1}}]"#;
15+
let delta = RichTextDelta::from_json(json).unwrap();
16+
let md = markdown_encoder(&delta);
17+
assert_eq!(md, "# header 1\n");
18+
}
19+
20+
#[test]
21+
fn markdown_encoder_header_2_test() {
22+
let json = r#"[{"insert":"header 2"},{"insert":"\n","attributes":{"header":2}}]"#;
23+
let delta = RichTextDelta::from_json(json).unwrap();
24+
let md = markdown_encoder(&delta);
25+
assert_eq!(md, "## header 2\n");
26+
}
27+
28+
#[test]
29+
fn markdown_encoder_header_3_test() {
30+
let json = r#"[{"insert":"header 3"},{"insert":"\n","attributes":{"header":3}}]"#;
31+
let delta = RichTextDelta::from_json(json).unwrap();
32+
let md = markdown_encoder(&delta);
33+
assert_eq!(md, "### header 3\n");
34+
}
35+
36+
#[test]
37+
fn markdown_encoder_bold_italics_underlined_test() {
38+
let json = r#"[{"insert":"bold","attributes":{"bold":true}},{"insert":" "},{"insert":"italics","attributes":{"italic":true}},{"insert":" "},{"insert":"underlined","attributes":{"underline":true}},{"insert":" "},{"insert":"\n","attributes":{"header":3}}]"#;
39+
let delta = RichTextDelta::from_json(json).unwrap();
40+
let md = markdown_encoder(&delta);
41+
assert_eq!(md, "### **bold** _italics_ <u>underlined</u> \n");
42+
}
43+
#[test]
44+
fn markdown_encoder_strikethrough_highlight_test() {
45+
let json = r##"[{"insert":"strikethrough","attributes":{"strike":true}},{"insert":" "},{"insert":"highlighted","attributes":{"background":"#ffefe3"}},{"insert":"\n"}]"##;
46+
let delta = RichTextDelta::from_json(json).unwrap();
47+
let md = markdown_encoder(&delta);
48+
assert_eq!(md, "~~strikethrough~~ <mark>highlighted</mark>\n");
49+
}
50+
51+
#[test]
52+
fn markdown_encoder_numbered_list_test() {
53+
let json = r#"[{"insert":"numbered list\nitem 1"},{"insert":"\n","attributes":{"list":"ordered"}},{"insert":"item 2"},{"insert":"\n","attributes":{"list":"ordered"}},{"insert":"item3"},{"insert":"\n","attributes":{"list":"ordered"}}]"#;
54+
let delta = RichTextDelta::from_json(json).unwrap();
55+
let md = markdown_encoder(&delta);
56+
assert_eq!(md, "numbered list\n\n1. item 1\n1. item 2\n1. item3\n");
57+
}
58+
59+
#[test]
60+
fn markdown_encoder_bullet_list_test() {
61+
let json = r#"[{"insert":"bullet list\nitem1"},{"insert":"\n","attributes":{"list":"bullet"}}]"#;
62+
let delta = RichTextDelta::from_json(json).unwrap();
63+
let md = markdown_encoder(&delta);
64+
assert_eq!(md, "bullet list\n\n* item1\n");
65+
}
66+
67+
#[test]
68+
fn markdown_encoder_check_list_test() {
69+
let json = r#"[{"insert":"check list\nchecked"},{"insert":"\n","attributes":{"list":"checked"}},{"insert":"unchecked"},{"insert":"\n","attributes":{"list":"unchecked"}}]"#;
70+
let delta = RichTextDelta::from_json(json).unwrap();
71+
let md = markdown_encoder(&delta);
72+
assert_eq!(md, "check list\n\n- [x] checked\n\n- [ ] unchecked\n");
73+
}
74+
75+
#[test]
76+
fn markdown_encoder_code_test() {
77+
let json = r#"[{"insert":"code this "},{"insert":"print(\"hello world\")","attributes":{"code":true}},{"insert":"\n"}]"#;
78+
let delta = RichTextDelta::from_json(json).unwrap();
79+
let md = markdown_encoder(&delta);
80+
assert_eq!(md, "code this `print(\"hello world\")`\n");
81+
}
82+
83+
#[test]
84+
fn markdown_encoder_quote_block_test() {
85+
let json = r#"[{"insert":"this is a quote block"},{"insert":"\n","attributes":{"blockquote":true}}]"#;
86+
let delta = RichTextDelta::from_json(json).unwrap();
87+
let md = markdown_encoder(&delta);
88+
assert_eq!(md, "> this is a quote block\n");
89+
}
90+
91+
#[test]
92+
fn markdown_encoder_link_test() {
93+
let json = r#"[{"insert":"appflowy","attributes":{"link":"https://www.appflowy.io/"}},{"insert":"\n"}]"#;
94+
let delta = RichTextDelta::from_json(json).unwrap();
95+
let md = markdown_encoder(&delta);
96+
assert_eq!(md, "[appflowy](https://www.appflowy.io/)\n");
97+
}
98+
}
99+
100+
struct Attribute {
101+
key: RichTextAttributeKey,
102+
value: RichTextAttributeValue,
103+
}
104+
105+
pub fn markdown_encoder(delta: &Delta<RichTextAttributes>) -> String {
106+
let mut markdown_buffer = String::new();
107+
let mut line_buffer = String::new();
108+
let mut current_inline_style = RichTextAttributes::default();
109+
let mut current_block_lines: Vec<String> = Vec::new();
110+
let mut iterator = DeltaIterator::new(delta);
111+
let mut current_block_style: Option<Attribute> = None;
112+
113+
while iterator.has_next() {
114+
let operation = iterator.next().unwrap();
115+
let operation_data = operation.get_data();
116+
if !operation_data.contains("\n") {
117+
handle_inline(
118+
&mut current_inline_style,
119+
&mut line_buffer,
120+
String::from(operation_data),
121+
operation.get_attributes(),
122+
)
123+
} else {
124+
handle_line(
125+
&mut line_buffer,
126+
&mut markdown_buffer,
127+
String::from(operation_data),
128+
operation.get_attributes(),
129+
&mut current_block_style,
130+
&mut current_block_lines,
131+
&mut current_inline_style,
132+
)
133+
}
134+
}
135+
handle_block(&mut current_block_style, &mut current_block_lines, &mut markdown_buffer);
136+
137+
markdown_buffer
138+
}
139+
140+
fn handle_inline(
141+
current_inline_style: &mut RichTextAttributes,
142+
buffer: &mut String,
143+
mut text: String,
144+
attributes: RichTextAttributes,
145+
) {
146+
let mut marked_for_removal: HashMap<RichTextAttributeKey, RichTextAttributeValue> = HashMap::new();
147+
148+
for key in current_inline_style
149+
.clone()
150+
.keys()
151+
.collect::<Vec<&RichTextAttributeKey>>()
152+
.into_iter()
153+
.rev()
154+
{
155+
if is_block(key) {
156+
continue;
157+
}
158+
159+
if attributes.contains_key(key) {
160+
continue;
161+
}
162+
163+
let padding = trim_right(buffer);
164+
write_attribute(buffer, key, current_inline_style.get(key).unwrap(), true);
165+
if !padding.is_empty() {
166+
buffer.push_str(&padding)
167+
}
168+
marked_for_removal.insert(key.clone(), current_inline_style.get(key).unwrap().clone());
169+
}
170+
171+
for (marked_for_removal_key, marked_for_removal_value) in &marked_for_removal {
172+
current_inline_style.retain(|inline_style_key, inline_style_value| {
173+
inline_style_key != marked_for_removal_key && inline_style_value != marked_for_removal_value
174+
})
175+
}
176+
177+
for (key, value) in attributes.iter() {
178+
if is_block(key) {
179+
continue;
180+
}
181+
if current_inline_style.contains_key(key) {
182+
continue;
183+
}
184+
let original_text = text.clone();
185+
text = text.trim_start().to_string();
186+
let padding = " ".repeat(original_text.len() - text.len());
187+
if !padding.is_empty() {
188+
buffer.push_str(&padding)
189+
}
190+
write_attribute(buffer, key, value, false)
191+
}
192+
193+
buffer.push_str(&text);
194+
*current_inline_style = attributes;
195+
}
196+
197+
fn trim_right(buffer: &mut String) -> String {
198+
let text = buffer.clone();
199+
if !text.ends_with(" ") {
200+
return String::from("");
201+
}
202+
let result = text.trim_end();
203+
buffer.clear();
204+
buffer.push_str(result);
205+
" ".repeat(text.len() - result.len())
206+
}
207+
208+
fn write_attribute(buffer: &mut String, key: &RichTextAttributeKey, value: &RichTextAttributeValue, close: bool) {
209+
match key {
210+
RichTextAttributeKey::Bold => buffer.push_str("**"),
211+
RichTextAttributeKey::Italic => buffer.push_str("_"),
212+
RichTextAttributeKey::Underline => {
213+
if close {
214+
buffer.push_str("</u>")
215+
} else {
216+
buffer.push_str("<u>")
217+
}
218+
}
219+
RichTextAttributeKey::StrikeThrough => {
220+
if close {
221+
buffer.push_str("~~")
222+
} else {
223+
buffer.push_str("~~")
224+
}
225+
}
226+
RichTextAttributeKey::Link => {
227+
if close {
228+
buffer.push_str(format!("]({})", value.0.as_ref().unwrap()).as_str())
229+
} else {
230+
buffer.push_str("[")
231+
}
232+
}
233+
RichTextAttributeKey::Background => {
234+
if close {
235+
buffer.push_str("</mark>")
236+
} else {
237+
buffer.push_str("<mark>")
238+
}
239+
}
240+
RichTextAttributeKey::CodeBlock => {
241+
if close {
242+
buffer.push_str("\n```")
243+
} else {
244+
buffer.push_str("```\n")
245+
}
246+
}
247+
RichTextAttributeKey::InlineCode => {
248+
if close {
249+
buffer.push_str("`")
250+
} else {
251+
buffer.push_str("`")
252+
}
253+
}
254+
_ => {}
255+
}
256+
}
257+
258+
fn handle_line(
259+
buffer: &mut String,
260+
markdown_buffer: &mut String,
261+
data: String,
262+
attributes: RichTextAttributes,
263+
current_block_style: &mut Option<Attribute>,
264+
current_block_lines: &mut Vec<String>,
265+
current_inline_style: &mut RichTextAttributes,
266+
) {
267+
let mut span = String::new();
268+
for c in data.chars() {
269+
if (c as i32) == LINEFEEDASCIICODE {
270+
if !span.is_empty() {
271+
handle_inline(current_inline_style, buffer, span.clone(), attributes.clone());
272+
}
273+
handle_inline(
274+
current_inline_style,
275+
buffer,
276+
String::from(""),
277+
RichTextAttributes::default(),
278+
);
279+
280+
let line_block_key = attributes.keys().find(|key| {
281+
if is_block(*key) {
282+
return true;
283+
} else {
284+
return false;
285+
}
286+
});
287+
288+
match (line_block_key, &current_block_style) {
289+
(Some(line_block_key), Some(current_block_style))
290+
if *line_block_key == current_block_style.key
291+
&& *attributes.get(line_block_key).unwrap() == current_block_style.value =>
292+
{
293+
current_block_lines.push(buffer.clone());
294+
}
295+
(None, None) => {
296+
current_block_lines.push(buffer.clone());
297+
}
298+
_ => {
299+
handle_block(current_block_style, current_block_lines, markdown_buffer);
300+
current_block_lines.clear();
301+
current_block_lines.push(buffer.clone());
302+
303+
match line_block_key {
304+
None => *current_block_style = None,
305+
Some(line_block_key) => {
306+
*current_block_style = Some(Attribute {
307+
key: line_block_key.clone(),
308+
value: attributes.get(line_block_key).unwrap().clone(),
309+
})
310+
}
311+
}
312+
}
313+
}
314+
buffer.clear();
315+
span.clear();
316+
} else {
317+
span.push(c);
318+
}
319+
}
320+
if !span.is_empty() {
321+
handle_inline(current_inline_style, buffer, span.clone(), attributes)
322+
}
323+
}
324+
325+
fn handle_block(
326+
block_style: &mut Option<Attribute>,
327+
current_block_lines: &mut Vec<String>,
328+
markdown_buffer: &mut String,
329+
) {
330+
if current_block_lines.is_empty() {
331+
return;
332+
}
333+
if !markdown_buffer.is_empty() {
334+
markdown_buffer.push('\n')
335+
}
336+
337+
match block_style {
338+
None => {
339+
markdown_buffer.push_str(&current_block_lines.join("\n"));
340+
markdown_buffer.push('\n');
341+
}
342+
Some(block_style) if block_style.key == RichTextAttributeKey::CodeBlock => {
343+
write_attribute(markdown_buffer, &block_style.key, &block_style.value, false);
344+
markdown_buffer.push_str(&current_block_lines.join("\n"));
345+
write_attribute(markdown_buffer, &block_style.key, &block_style.value, true);
346+
markdown_buffer.push('\n');
347+
}
348+
Some(block_style) => {
349+
for line in current_block_lines {
350+
write_block_tag(markdown_buffer, &block_style, false);
351+
markdown_buffer.push_str(line);
352+
markdown_buffer.push('\n');
353+
}
354+
}
355+
}
356+
}
357+
358+
fn write_block_tag(buffer: &mut String, block: &Attribute, close: bool) {
359+
if close {
360+
return;
361+
}
362+
363+
if block.key == RichTextAttributeKey::BlockQuote {
364+
buffer.push_str("> ");
365+
} else if block.key == RichTextAttributeKey::List {
366+
if block.value.0.as_ref().unwrap().eq("bullet") {
367+
buffer.push_str("* ");
368+
} else if block.value.0.as_ref().unwrap().eq("checked") {
369+
buffer.push_str("- [x] ");
370+
} else if block.value.0.as_ref().unwrap().eq("unchecked") {
371+
buffer.push_str("- [ ] ");
372+
} else if block.value.0.as_ref().unwrap().eq("ordered") {
373+
buffer.push_str("1. ");
374+
} else {
375+
buffer.push_str("* ");
376+
}
377+
} else if block.key == RichTextAttributeKey::Header {
378+
if block.value.0.as_ref().unwrap().eq("1") {
379+
buffer.push_str("# ");
380+
} else if block.value.0.as_ref().unwrap().eq("2") {
381+
buffer.push_str("## ");
382+
} else if block.value.0.as_ref().unwrap().eq("3") {
383+
buffer.push_str("### ");
384+
} else if block.key == RichTextAttributeKey::List {
385+
}
386+
}
387+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
pub mod markdown_encoder;

0 commit comments

Comments
 (0)