diff --git a/markdown_backend/Cargo.toml b/markdown_backend/Cargo.toml index 31e5e68..1b38d05 100644 --- a/markdown_backend/Cargo.toml +++ b/markdown_backend/Cargo.toml @@ -15,4 +15,5 @@ notify = "6.1" syntect = { version = "5.1", features = ["default"] } regex = "1.10" clap = { version = "4.4", features = ["derive"] } -serde_json = "1.0" \ No newline at end of file +serde_json = "1.0" +html-escape = "0.2.13" diff --git a/markdown_backend/src/markdown.rs b/markdown_backend/src/markdown.rs index 1fa0bb2..d8a3f9e 100644 --- a/markdown_backend/src/markdown.rs +++ b/markdown_backend/src/markdown.rs @@ -4,7 +4,7 @@ use std::fs; use std::path::{Path, PathBuf}; use chrono::{DateTime, Utc}; use serde::Deserialize; -use pulldown_cmark::{Parser, Options, html}; +use pulldown_cmark::{Parser, Options, html, Event, Tag, CowStr}; use gray_matter::engine::YAML; use gray_matter::Matter; use ammonia::clean; @@ -77,29 +77,6 @@ fn process_anchor_links(content: &str) -> String { }).to_string() } -fn highlight_code_blocks(html: &str) -> String { - let ss = SyntaxSet::load_defaults_newlines(); - let ts = ThemeSet::load_defaults(); - let theme = &ts.themes["base16-ocean.dark"]; - - // Simple code block detection and highlighting - // In a real implementation, you'd want to parse the HTML and handle code blocks properly - let re = regex::Regex::new(r#"
([^<]+)"#).unwrap();
- re.replace_all(html, |caps: ®ex::Captures| {
- let lang = &caps[1];
- let code = &caps[2];
-
- if let Some(syntax) = ss.find_syntax_by_token(lang) {
- match highlighted_html_for_string(code, &ss, syntax, theme) {
- Ok(highlighted) => highlighted,
- Err(_) => caps[0].to_string(),
- }
- } else {
- caps[0].to_string()
- }
- }).to_string()
-}
-
pub fn get_post_by_slug(slug: &str) -> Result{}", html_escape::encode_text(&code_block_content)))
+ } else {
+ format!("{}", html_escape::encode_text(&code_block_content))
+ }
+ } else {
+ // No language specified
+ format!("{}", html_escape::encode_text(&code_block_content))
+ };
+ events.push(Event::Html(CowStr::Boxed(highlighted.into_boxed_str())));
+ },
+ Event::Text(text) if in_code_block => {
+ code_block_content.push_str(text);
+ },
+ _ if !in_heading && !in_code_block => {
+ events.push(event);
+ },
+ _ => {},
+ }
+ }
+ html::push_html(&mut html_output, events.into_iter());
- let highlighted_html = highlight_code_blocks(&html_output);
- let sanitized_html = clean(&highlighted_html);
+ let sanitized_html = clean(&html_output);
Ok(Post {
slug: slug.to_string(),