diff --git a/src/main.rs b/src/main.rs index e4db6a8..dcaf14a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -218,52 +218,67 @@ fn markdown_to_html(markdown: &str, ss: &SyntaxSet, ts: &ThemeSet, file_path: &s Event::End(Tag::CodeBlock(_)) => { in_code_block = false; - // Определяем отображаемое имя языка - let lang_display = current_lang.as_deref().unwrap_or("text"); + // Проверка на Mermaid + let is_mermaid = current_lang.as_deref() == Some("mermaid"); - // Экранируем имя языка для HTML атрибута и текста - let lang_escaped = escape_html(lang_display); + if is_mermaid { + // Для Mermaid просто экранируем контент и оборачиваем в div + // Кнопка копирования тоже нужна + let escaped_code = escape_html(¤t_code); + let mermaid_html = format!( + r#"
+
+ Mermaid Diagram + +
+
{}
+
"#, + escaped_code + ); + processed_events.push(Event::Html(mermaid_html.into())); + } else { + // Обычная обработка кода с подсветкой + let lang_display = current_lang.as_deref().unwrap_or("text"); + let lang_escaped = escape_html(lang_display); - // Подсветка синтаксиса (построчно) - let highlighted_html = if let Some(lang) = ¤t_lang { - if let Some(syntax) = ss.find_syntax_by_token(lang) { - let mut h = HighlightLines::new(syntax, theme); - let mut result_html = String::new(); + let highlighted_html = if let Some(lang) = ¤t_lang { + if let Some(syntax) = ss.find_syntax_by_token(lang) { + let mut h = HighlightLines::new(syntax, theme); + let mut result_html = String::new(); - for line in current_code.lines() { - let line_with_newline = format!("{}\n", line); - match h.highlight_line(&line_with_newline, ss) { - Ok(regions) => { - let html_line = styled_line_to_highlighted_html(®ions[..], IncludeBackground::No) - .unwrap_or_else(|_| escape_html(&line_with_newline)); - result_html.push_str(&html_line); - }, - Err(_) => result_html.push_str(&escape_html(&line_with_newline)), + for line in current_code.lines() { + let line_with_newline = format!("{}\n", line); + match h.highlight_line(&line_with_newline, ss) { + Ok(regions) => { + let html_line = styled_line_to_highlighted_html(®ions[..], IncludeBackground::No) + .unwrap_or_else(|_| escape_html(&line_with_newline)); + result_html.push_str(&html_line); + }, + Err(_) => result_html.push_str(&escape_html(&line_with_newline)), + } } + result_html + } else { + escape_html(¤t_code) } - result_html } else { escape_html(¤t_code) - } - } else { - escape_html(¤t_code) - }; + }; - // Формируем HTML с заголовком и кнопкой копирования - // Мы экранируем current_code еще раз для data-атрибута, хотя для копирования будем брать текст из pre - let code_container = format!( - r#"
-
- {} - -
-
{}
-
"#, - lang_escaped, - highlighted_html - ); + let code_container = format!( + r#"
+
+ {} + +
+
{}
+
"#, + lang_escaped, + highlighted_html + ); - processed_events.push(Event::Html(code_container.into())); + processed_events.push(Event::Html(code_container.into())); + } }, Event::Text(text) if in_code_block => { current_code.push_str(&text); @@ -288,6 +303,8 @@ fn markdown_to_html(markdown: &str, ss: &SyntaxSet, ts: &ThemeSet, file_path: &s Markdown Preview + +