markdown_encoder.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. use crate::core::{OperationIterator, Operations};
  2. use crate::text_delta::{is_block, TextAttributeKey, TextAttributeValue, TextAttributes};
  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::text_delta::TextDelta;
  9. #[test]
  10. fn markdown_encoder_header_1_test() {
  11. let json = r#"[{"insert":"header 1"},{"insert":"\n","attributes":{"header":1}}]"#;
  12. let delta = TextDelta::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 = TextDelta::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 = TextDelta::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 = TextDelta::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 = TextDelta::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 = TextDelta::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 = TextDelta::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 = TextDelta::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 = TextDelta::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 = TextDelta::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 = TextDelta::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: TextAttributeKey,
  89. value: TextAttributeValue,
  90. }
  91. pub fn markdown_encoder(delta: &Operations<TextAttributes>) -> String {
  92. let mut markdown_buffer = String::new();
  93. let mut line_buffer = String::new();
  94. let mut current_inline_style = TextAttributes::default();
  95. let mut current_block_lines: Vec<String> = Vec::new();
  96. let mut iterator = OperationIterator::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 TextAttributes,
  125. buffer: &mut String,
  126. mut text: String,
  127. attributes: TextAttributes,
  128. ) {
  129. let mut marked_for_removal: HashMap<TextAttributeKey, TextAttributeValue> = HashMap::new();
  130. for key in current_inline_style
  131. .clone()
  132. .keys()
  133. .collect::<Vec<&TextAttributeKey>>()
  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: &TextAttributeKey, value: &TextAttributeValue, close: bool) {
  184. match key {
  185. TextAttributeKey::Bold => buffer.push_str("**"),
  186. TextAttributeKey::Italic => buffer.push('_'),
  187. TextAttributeKey::Underline => {
  188. if close {
  189. buffer.push_str("</u>")
  190. } else {
  191. buffer.push_str("<u>")
  192. }
  193. }
  194. TextAttributeKey::StrikeThrough => buffer.push_str("~~"),
  195. TextAttributeKey::Link => {
  196. if close {
  197. buffer.push_str(format!("]({})", value.0.as_ref().unwrap()).as_str())
  198. } else {
  199. buffer.push('[')
  200. }
  201. }
  202. TextAttributeKey::Background => {
  203. if close {
  204. buffer.push_str("</mark>")
  205. } else {
  206. buffer.push_str("<mark>")
  207. }
  208. }
  209. TextAttributeKey::CodeBlock => {
  210. if close {
  211. buffer.push_str("\n```")
  212. } else {
  213. buffer.push_str("```\n")
  214. }
  215. }
  216. TextAttributeKey::InlineCode => buffer.push('`'),
  217. _ => {}
  218. }
  219. }
  220. fn handle_line(
  221. buffer: &mut String,
  222. markdown_buffer: &mut String,
  223. data: String,
  224. attributes: TextAttributes,
  225. current_block_style: &mut Option<Attribute>,
  226. current_block_lines: &mut Vec<String>,
  227. current_inline_style: &mut TextAttributes,
  228. ) {
  229. let mut span = String::new();
  230. for c in data.chars() {
  231. if (c as i32) == LINEFEEDASCIICODE {
  232. if !span.is_empty() {
  233. handle_inline(current_inline_style, buffer, span.clone(), attributes.clone());
  234. }
  235. handle_inline(
  236. current_inline_style,
  237. buffer,
  238. String::from(""),
  239. TextAttributes::default(),
  240. );
  241. let line_block_key = attributes.keys().find(|key| is_block(*key));
  242. match (line_block_key, &current_block_style) {
  243. (Some(line_block_key), Some(current_block_style))
  244. if *line_block_key == current_block_style.key
  245. && *attributes.get(line_block_key).unwrap() == current_block_style.value =>
  246. {
  247. current_block_lines.push(buffer.clone());
  248. }
  249. (None, None) => {
  250. current_block_lines.push(buffer.clone());
  251. }
  252. _ => {
  253. handle_block(current_block_style, current_block_lines, markdown_buffer);
  254. current_block_lines.clear();
  255. current_block_lines.push(buffer.clone());
  256. match line_block_key {
  257. None => *current_block_style = None,
  258. Some(line_block_key) => {
  259. *current_block_style = Some(Attribute {
  260. key: line_block_key.clone(),
  261. value: attributes.get(line_block_key).unwrap().clone(),
  262. })
  263. }
  264. }
  265. }
  266. }
  267. buffer.clear();
  268. span.clear();
  269. } else {
  270. span.push(c);
  271. }
  272. }
  273. if !span.is_empty() {
  274. handle_inline(current_inline_style, buffer, span.clone(), attributes)
  275. }
  276. }
  277. fn handle_block(
  278. block_style: &mut Option<Attribute>,
  279. current_block_lines: &mut Vec<String>,
  280. markdown_buffer: &mut String,
  281. ) {
  282. if current_block_lines.is_empty() {
  283. return;
  284. }
  285. if !markdown_buffer.is_empty() {
  286. markdown_buffer.push('\n')
  287. }
  288. match block_style {
  289. None => {
  290. markdown_buffer.push_str(&current_block_lines.join("\n"));
  291. markdown_buffer.push('\n');
  292. }
  293. Some(block_style) if block_style.key == TextAttributeKey::CodeBlock => {
  294. write_attribute(markdown_buffer, &block_style.key, &block_style.value, false);
  295. markdown_buffer.push_str(&current_block_lines.join("\n"));
  296. write_attribute(markdown_buffer, &block_style.key, &block_style.value, true);
  297. markdown_buffer.push('\n');
  298. }
  299. Some(block_style) => {
  300. for line in current_block_lines {
  301. write_block_tag(markdown_buffer, block_style, false);
  302. markdown_buffer.push_str(line);
  303. markdown_buffer.push('\n');
  304. }
  305. }
  306. }
  307. }
  308. fn write_block_tag(buffer: &mut String, block: &Attribute, close: bool) {
  309. if close {
  310. return;
  311. }
  312. if block.key == TextAttributeKey::BlockQuote {
  313. buffer.push_str("> ");
  314. } else if block.key == TextAttributeKey::List {
  315. if block.value.0.as_ref().unwrap().eq("bullet") {
  316. buffer.push_str("* ");
  317. } else if block.value.0.as_ref().unwrap().eq("checked") {
  318. buffer.push_str("- [x] ");
  319. } else if block.value.0.as_ref().unwrap().eq("unchecked") {
  320. buffer.push_str("- [ ] ");
  321. } else if block.value.0.as_ref().unwrap().eq("ordered") {
  322. buffer.push_str("1. ");
  323. } else {
  324. buffer.push_str("* ");
  325. }
  326. } else if block.key == TextAttributeKey::Header {
  327. if block.value.0.as_ref().unwrap().eq("1") {
  328. buffer.push_str("# ");
  329. } else if block.value.0.as_ref().unwrap().eq("2") {
  330. buffer.push_str("## ");
  331. } else if block.value.0.as_ref().unwrap().eq("3") {
  332. buffer.push_str("### ");
  333. } else if block.key == TextAttributeKey::List {
  334. }
  335. }
  336. }