markdown_encoder.rs 14 KB

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