]> Untitled Git - lemmy.git/blob - crates/utils/src/utils/markdown.rs
Cache & Optimize Woodpecker CI (#3450)
[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   #![allow(clippy::unwrap_used)]
22   #![allow(clippy::indexing_slicing)]
23
24   use crate::utils::markdown::markdown_to_html;
25
26   #[test]
27   fn test_basic_markdown() {
28     let tests: Vec<_> = vec![
29       (
30         "headings",
31         "# h1\n## h2\n### h3\n#### h4\n##### h5\n###### h6",
32         "<h1>h1</h1>\n<h2>h2</h2>\n<h3>h3</h3>\n<h4>h4</h4>\n<h5>h5</h5>\n<h6>h6</h6>\n"
33       ),
34       (
35         "line breaks",
36         "First\rSecond",
37         "<p>First\nSecond</p>\n"),
38       (
39         "emphasis",
40         "__bold__ **bold** *italic* ***bold+italic***",
41         "<p><strong>bold</strong> <strong>bold</strong> <em>italic</em> <em><strong>bold+italic</strong></em></p>\n"
42       ),
43       (
44         "blockquotes",
45         "> #### Hello\n > \n > - Hola\n > - 안영 \n>> Goodbye\n",
46         "<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"
47       ),
48       (
49         "lists (ordered, unordered)",
50         "1. pen\n2. apple\n3. apple pen\n- pen\n- pineapple\n- pineapple pen",
51         "<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"
52       ),
53       (
54         "code and code blocks",
55         "this is my amazing `code snippet` and my amazing ```code block```",
56         "<p>this is my amazing <code>code snippet</code> and my amazing <code>code block</code></p>\n"
57       ),
58       (
59         "links",
60         "[Lemmy](https://join-lemmy.org/ \"Join Lemmy!\")",
61         "<p><a href=\"https://join-lemmy.org/\" title=\"Join Lemmy!\">Lemmy</a></p>\n"
62       ),
63       (
64         "images",
65         "![My linked image](https://image.com \"image alt text\")",
66         "<p><img src=\"https://image.com\" alt=\"My linked image\" title=\"image alt text\" /></p>\n"
67       ),
68       // Ensure any custom plugins are added to 'MARKDOWN_PARSER' implementation.
69       (
70         "basic spoiler",
71         "::: spoiler click to see more\nhow spicy!\n:::\n",
72         "<details><summary>click to see more</summary><p>how spicy!\n</p></details>\n"
73       ),
74     ];
75
76     tests.iter().for_each(|&(msg, input, expected)| {
77       let result = markdown_to_html(input);
78
79       assert_eq!(
80         result, expected,
81         "Testing {}, with original input '{}'",
82         msg, input
83       );
84     });
85   }
86 }