| 1 | use std::sync::LazyLock; |
| 2 | |
| 3 | use regex::Regex; |
| 4 | |
| 5 | static CODE_FENCE: LazyLock<Regex> = |
| 6 | LazyLock::new(|| Regex::new(r"(?s)```[^\n`]*\n(.*?)```").unwrap()); |
| 7 | static INLINE_CODE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"`([^`\n]+)`").unwrap()); |
| 8 | static BOLD: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\*\*([^*\n]+)\*\*").unwrap()); |
| 9 | static LINK: LazyLock<Regex> = |
| 10 | LazyLock::new(|| Regex::new(r"\[([^\]\n]+)\]\((https?://[^)\s]+)\)").unwrap()); |
| 11 | |
| 12 | const LIST_MARKER: &str = "- "; |
| 13 | |
| 14 | pub fn escape_html(text: &str) -> String { |
| 15 | let mut escaped = String::with_capacity(text.len()); |
| 16 | for character in text.chars() { |
| 17 | match character { |
| 18 | '&' => escaped.push_str("&"), |
| 19 | '<' => escaped.push_str("<"), |
| 20 | '>' => escaped.push_str(">"), |
| 21 | '"' => escaped.push_str("""), |
| 22 | '\'' => escaped.push_str("'"), |
| 23 | other => escaped.push(other), |
| 24 | } |
| 25 | } |
| 26 | escaped |
| 27 | } |
| 28 | |
| 29 | pub fn plain_text_to_html(text: &str) -> String { |
| 30 | escape_html(text).replace('\n', "<br>") |
| 31 | } |
| 32 | |
| 33 | /// Teams chat HTML for light markdown, plus `- ` list lines as `<ul><li>`. |
| 34 | pub fn markdown_to_html(text: &str) -> String { |
| 35 | let mut html = String::new(); |
| 36 | let mut position = 0; |
| 37 | for fence in CODE_FENCE.captures_iter(text) { |
| 38 | let whole = fence.get(0).expect("group 0 always exists"); |
| 39 | html.push_str(&block_html(&text[position..whole.start()])); |
| 40 | html.push_str("<pre>"); |
| 41 | html.push_str(&escape_html(fence[1].trim_end_matches('\n'))); |
| 42 | html.push_str("</pre>"); |
| 43 | position = whole.end(); |
| 44 | } |
| 45 | html.push_str(&block_html(&text[position..])); |
| 46 | html |
| 47 | } |
| 48 | |
| 49 | fn block_html(segment: &str) -> String { |
| 50 | if !segment.lines().any(is_list_line) { |
| 51 | return inline_html(segment); |
| 52 | } |
| 53 | let mut html = String::new(); |
| 54 | let mut text_run: Vec<&str> = Vec::new(); |
| 55 | let mut list_run: Vec<&str> = Vec::new(); |
| 56 | for line in segment.split('\n') { |
| 57 | if is_list_line(line) { |
| 58 | flush_text(&mut html, &mut text_run); |
| 59 | list_run.push(&line[LIST_MARKER.len()..]); |
| 60 | } else { |
| 61 | flush_list(&mut html, &mut list_run); |
| 62 | text_run.push(line); |
| 63 | } |
| 64 | } |
| 65 | flush_list(&mut html, &mut list_run); |
| 66 | flush_text(&mut html, &mut text_run); |
| 67 | html |
| 68 | } |
| 69 | |
| 70 | fn is_list_line(line: &str) -> bool { |
| 71 | line.starts_with(LIST_MARKER) |
| 72 | } |
| 73 | |
| 74 | fn flush_text(html: &mut String, run: &mut Vec<&str>) { |
| 75 | if !run.is_empty() { |
| 76 | html.push_str(&inline_html(&run.join("\n"))); |
| 77 | run.clear(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | fn flush_list(html: &mut String, run: &mut Vec<&str>) { |
| 82 | if run.is_empty() { |
| 83 | return; |
| 84 | } |
| 85 | html.push_str("<ul>"); |
| 86 | for item in run.drain(..) { |
| 87 | html.push_str("<li>"); |
| 88 | html.push_str(&inline_html(item)); |
| 89 | html.push_str("</li>"); |
| 90 | } |
| 91 | html.push_str("</ul>"); |
| 92 | } |
| 93 | |
| 94 | fn inline_html(text: &str) -> String { |
| 95 | let escaped = plain_text_to_html(text); |
| 96 | let coded = INLINE_CODE.replace_all(&escaped, "<code>${1}</code>"); |
| 97 | let bolded = BOLD.replace_all(&coded, "<b>${1}</b>"); |
| 98 | LINK.replace_all(&bolded, r#"<a href="${2}">${1}</a>"#) |
| 99 | .into_owned() |
| 100 | } |