1 use markdown_it::MarkdownIt;
2 use once_cell::sync::Lazy;
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);
15 pub fn markdown_to_html(text: &str) -> String {
16 MARKDOWN_PARSER.parse(text).xrender()
21 use crate::utils::markdown::markdown_to_html;
24 fn test_basic_markdown() {
25 let tests: Vec<_> = vec![
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"
34 "<p>First\nSecond</p>\n"),
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"
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"
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"
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"
57 "[Lemmy](https://join-lemmy.org/ \"Join Lemmy!\")",
58 "<p><a href=\"https://join-lemmy.org/\" title=\"Join Lemmy!\">Lemmy</a></p>\n"
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"
65 // Ensure any custom plugins are added to 'MARKDOWN_PARSER' implementation.
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"
73 tests.iter().for_each(|&(msg, input, expected)| {
74 let result = markdown_to_html(input);
78 "Testing {}, with original input '{}'",