]> Untitled Git - lemmy.git/blob - crates/utils/src/utils/markdown.rs
451c86bc73621f3ff008e500d79dd44fadb43c20
[lemmy.git] / crates / utils / src / utils / markdown.rs
1 use markdown_it::MarkdownIt;
2 use once_cell::sync::Lazy;
3
4 mod spoiler_rule;
5
6 static MARKDOWN_PARSER: Lazy<MarkdownIt> = Lazy::new(|| {
7   let mut parser = MarkdownIt::new();
8   markdown_it::plugins::cmark::add(&mut parser);
9   markdown_it::plugins::extra::add(&mut parser);
10   spoiler_rule::add(&mut parser);
11
12   parser
13 });
14
15 pub fn markdown_to_html(text: &str) -> String {
16   MARKDOWN_PARSER.parse(text).xrender()
17 }
18
19 #[cfg(test)]
20 mod tests {
21   use crate::utils::markdown::markdown_to_html;
22
23   #[test]
24   fn test_basic_markdown() {
25     let tests: Vec<_> = vec![
26       (
27         "headings",
28         "# h1\n## h2\n### h3\n#### h4\n##### h5\n###### h6",
29         "<h1>h1</h1>\n<h2>h2</h2>\n<h3>h3</h3>\n<h4>h4</h4>\n<h5>h5</h5>\n<h6>h6</h6>\n"
30       ),
31       (
32         "line breaks",
33         "First\rSecond",
34         "<p>First\nSecond</p>\n"),
35       (
36         "emphasis",
37         "__bold__ **bold** *italic* ***bold+italic***",
38         "<p><strong>bold</strong> <strong>bold</strong> <em>italic</em> <em><strong>bold+italic</strong></em></p>\n"
39       ),
40       (
41         "blockquotes",
42         "> #### Hello\n > \n > - Hola\n > - 안영 \n>> Goodbye\n",
43         "<blockquote>\n<h4>Hello</h4>\n<ul>\n<li>Hola</li>\n<li>안영</li>\n</ul>\n<blockquote>\n<p>Goodbye</p>\n</blockquote>\n</blockquote>\n"
44       ),
45       (
46         "lists (ordered, unordered)",
47         "1. pen\n2. apple\n3. apple pen\n- pen\n- pineapple\n- pineapple pen",
48         "<ol>\n<li>pen</li>\n<li>apple</li>\n<li>apple pen</li>\n</ol>\n<ul>\n<li>pen</li>\n<li>pineapple</li>\n<li>pineapple pen</li>\n</ul>\n"
49       ),
50       (
51         "code and code blocks",
52         "this is my amazing `code snippet` and my amazing ```code block```",
53         "<p>this is my amazing <code>code snippet</code> and my amazing <code>code block</code></p>\n"
54       ),
55       (
56         "links",
57         "[Lemmy](https://join-lemmy.org/ \"Join Lemmy!\")",
58         "<p><a href=\"https://join-lemmy.org/\" title=\"Join Lemmy!\">Lemmy</a></p>\n"
59       ),
60       (
61         "images",
62         "![My linked image](https://image.com \"image alt text\")",
63         "<p><img src=\"https://image.com\" alt=\"My linked image\" title=\"image alt text\" /></p>\n"
64       ),
65       // Ensure any custom plugins are added to 'MARKDOWN_PARSER' implementation.
66       (
67         "basic spoiler",
68         "::: spoiler click to see more\nhow spicy!\n:::\n",
69         "<details><summary>click to see more</summary><p>how spicy!\n</p></details>\n"
70       ),
71     ];
72
73     tests.iter().for_each(|&(msg, input, expected)| {
74       let result = markdown_to_html(input);
75
76       assert_eq!(
77         result, expected,
78         "Testing {}, with original input '{}'",
79         msg, input
80       );
81     });
82   }
83 }