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 #![allow(clippy::unwrap_used)]
22 #![allow(clippy::indexing_slicing)]
24 use crate::utils::markdown::markdown_to_html;
27 fn test_basic_markdown() {
28 let tests: Vec<_> = vec![
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"
37 "<p>First\nSecond</p>\n"),
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"
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"
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"
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"
60 "[Lemmy](https://join-lemmy.org/ \"Join Lemmy!\")",
61 "<p><a href=\"https://join-lemmy.org/\" title=\"Join Lemmy!\">Lemmy</a></p>\n"
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"
68 // Ensure any custom plugins are added to 'MARKDOWN_PARSER' implementation.
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"
76 tests.iter().for_each(|&(msg, input, expected)| {
77 let result = markdown_to_html(input);
81 "Testing {}, with original input '{}'",