fix: merge conflict, unify docker, recursive, slug, and custom tag logic in markdown parser
Some checks failed
Deploy / build-and-deploy (push) Failing after 2s
Some checks failed
Deploy / build-and-deploy (push) Failing after 2s
This commit is contained in:
@@ -8,7 +8,7 @@ BLAZINGLY FAST!
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#[warn(unused_imports)]
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use chrono::{DateTime, Utc};
|
||||
@@ -29,6 +29,8 @@ use std::collections::HashMap;
|
||||
use std::sync::RwLock;
|
||||
use serde_json;
|
||||
use sysinfo::{System, Pid, RefreshKind, CpuRefreshKind, ProcessRefreshKind};
|
||||
use serde::Serialize;
|
||||
use regex::Regex;
|
||||
|
||||
const POSTS_CACHE_PATH: &str = "./cache/posts_cache.json";
|
||||
const POST_STATS_PATH: &str = "./cache/post_stats.json";
|
||||
@@ -68,6 +70,19 @@ static POST_CACHE: Lazy<RwLock<HashMap<String, Post>>> = Lazy::new(|| RwLock::ne
|
||||
static ALL_POSTS_CACHE: Lazy<RwLock<Option<Vec<Post>>>> = Lazy::new(|| RwLock::new(None));
|
||||
static POST_STATS: Lazy<RwLock<HashMap<String, PostStats>>> = Lazy::new(|| RwLock::new(HashMap::new()));
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct HealthReport {
|
||||
pub posts_dir_exists: bool,
|
||||
pub posts_count: usize,
|
||||
pub cache_file_exists: bool,
|
||||
pub cache_stats_file_exists: bool,
|
||||
pub cache_readable: bool,
|
||||
pub cache_stats_readable: bool,
|
||||
pub cache_post_count: Option<usize>,
|
||||
pub cache_stats_count: Option<usize>,
|
||||
pub errors: Vec<String>,
|
||||
}
|
||||
|
||||
fn get_posts_directory() -> PathBuf {
|
||||
// Check if we're running in Docker by looking for common Docker environment indicators
|
||||
let is_docker = std::env::var("DOCKER_CONTAINER").is_ok()
|
||||
@@ -205,6 +220,51 @@ fn strip_emojis(s: &str) -> String {
|
||||
.collect()
|
||||
}
|
||||
|
||||
// Function to process custom tags in markdown content
|
||||
fn process_custom_tags(content: &str) -> String {
|
||||
let mut processed = content.to_string();
|
||||
|
||||
// Handle simple tags without parameters FIRST
|
||||
let simple_tags = [
|
||||
("<mytag />", "<div class=\"custom-tag mytag\">This is my custom tag content!</div>"),
|
||||
("<warning />", "<div class=\"custom-tag warning\" style=\"background: #fff3cd; border: 1px solid #ffeaa7; padding: 1rem; border-radius: 4px; margin: 1rem 0;\">⚠️ Warning: This is a custom warning tag!</div>"),
|
||||
("<info />", "<div class=\"custom-tag info\" style=\"background: #d1ecf1; border: 1px solid #bee5eb; padding: 1rem; border-radius: 4px; margin: 1rem 0;\">ℹ️ Info: This is a custom info tag!</div>"),
|
||||
("<success />", "<div class=\"custom-tag success\" style=\"background: #d4edda; border: 1px solid #c3e6cb; padding: 1rem; border-radius: 4px; margin: 1rem 0;\">✅ Success: This is a custom success tag!</div>"),
|
||||
("<error />", "<div class=\"custom-tag error\" style=\"background: #f8d7da; border: 1px solid #f5c6cb; padding: 1rem; border-radius: 4px; margin: 1rem 0;\">❌ Error: This is a custom error tag!</div>"),
|
||||
];
|
||||
|
||||
for (tag, replacement) in simple_tags.iter() {
|
||||
processed = processed.replace(tag, replacement);
|
||||
}
|
||||
|
||||
// Handle tags with parameters like <mytag param="value" />
|
||||
let tag_with_params = Regex::new(r"<(\w+)\s+([^>]*?[a-zA-Z0-9=])[^>]*/>").unwrap();
|
||||
processed = tag_with_params.replace_all(&processed, |caps: ®ex::Captures| {
|
||||
let tag_name = &caps[1];
|
||||
let params = &caps[2];
|
||||
|
||||
match tag_name {
|
||||
"mytag" => {
|
||||
// Parse parameters and generate custom HTML
|
||||
format!("<div class=\"custom-tag mytag\" data-params=\"{}\">Custom content with params: {}</div>", params, params)
|
||||
},
|
||||
"alert" => {
|
||||
// Parse alert type from params
|
||||
if params.contains("type=\"warning\"") {
|
||||
"<div class=\"custom-tag alert warning\" style=\"background: #fff3cd; border: 1px solid #ffeaa7; padding: 1rem; border-radius: 4px; margin: 1rem 0;\">⚠️ Warning Alert!</div>".to_string()
|
||||
} else if params.contains("type=\"error\"") {
|
||||
"<div class=\"custom-tag alert error\" style=\"background: #f8d7da; border: 1px solid #f5c6cb; padding: 1rem; border-radius: 4px; margin: 1rem 0;\">❌ Error Alert!</div>".to_string()
|
||||
} else {
|
||||
"<div class=\"custom-tag alert info\" style=\"background: #d1ecf1; border: 1px solid #bee5eb; padding: 1rem; border-radius: 4px; margin: 1rem 0;\">ℹ️ Info Alert!</div>".to_string()
|
||||
}
|
||||
},
|
||||
_ => format!("<div class=\"custom-tag {}\">Unknown custom tag: {}</div>", tag_name, tag_name)
|
||||
}
|
||||
}).to_string();
|
||||
|
||||
processed
|
||||
}
|
||||
|
||||
static AMMONIA: Lazy<ammonia::Builder<'static>> = Lazy::new(|| {
|
||||
let mut builder = ammonia::Builder::default();
|
||||
// All possible HTML Tags so that you can stylize via HTML
|
||||
@@ -230,7 +290,7 @@ static AMMONIA: Lazy<ammonia::Builder<'static>> = Lazy::new(|| {
|
||||
builder.add_tag_attributes("pre", &["style"]);
|
||||
builder.add_tag_attributes("kbd", &["style"]);
|
||||
builder.add_tag_attributes("samp", &["style"]);
|
||||
builder.add_tag_attributes("div", &["style"]);
|
||||
builder.add_tag_attributes("div", &["style", "class"]);
|
||||
builder.add_tag_attributes("section", &["style"]);
|
||||
builder.add_tag_attributes("article", &["style"]);
|
||||
builder.add_tag_attributes("header", &["style"]);
|
||||
@@ -360,6 +420,7 @@ pub fn get_post_by_slug(slug: &str) -> Result<Post, Box<dyn std::error::Error>>
|
||||
let created_at = get_file_creation_date(&file_path)?;
|
||||
|
||||
let processed_markdown = process_anchor_links(&result.content);
|
||||
let processed_markdown = process_custom_tags(&processed_markdown);
|
||||
eprintln!("[Rust Parser] Processed markdown length: {} characters", processed_markdown.len());
|
||||
|
||||
let parser = Parser::new_ext(&processed_markdown, Options::all());
|
||||
@@ -550,4 +611,70 @@ pub fn save_post_cache_to_disk() {
|
||||
let _ = fs::create_dir_all("./cache");
|
||||
let _ = fs::write(POST_STATS_PATH, map);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn checkhealth() -> HealthReport {
|
||||
let mut errors = Vec::new();
|
||||
let posts_dir = get_posts_directory();
|
||||
let posts_dir_exists = posts_dir.exists() && posts_dir.is_dir();
|
||||
let mut posts_count = 0;
|
||||
if posts_dir_exists {
|
||||
match std::fs::read_dir(&posts_dir) {
|
||||
Ok(entries) => {
|
||||
posts_count = entries.filter_map(|e| e.ok())
|
||||
.filter(|e| e.path().extension().map(|ext| ext == "md").unwrap_or(false))
|
||||
.count();
|
||||
},
|
||||
Err(e) => errors.push(format!("Failed to read posts dir: {}", e)),
|
||||
}
|
||||
} else {
|
||||
errors.push("Posts directory does not exist".to_string());
|
||||
}
|
||||
let cache_file_exists = Path::new(POSTS_CACHE_PATH).exists();
|
||||
let cache_stats_file_exists = Path::new(POST_STATS_PATH).exists();
|
||||
let (mut cache_readable, mut cache_post_count) = (false, None);
|
||||
if cache_file_exists {
|
||||
match std::fs::read_to_string(POSTS_CACHE_PATH) {
|
||||
Ok(data) => {
|
||||
match serde_json::from_str::<HashMap<String, Post>>(&data) {
|
||||
Ok(map) => {
|
||||
cache_readable = true;
|
||||
cache_post_count = Some(map.len());
|
||||
},
|
||||
Err(e) => errors.push(format!("Cache file not valid JSON: {}", e)),
|
||||
}
|
||||
},
|
||||
Err(e) => errors.push(format!("Failed to read cache file: {}", e)),
|
||||
}
|
||||
}
|
||||
let (mut cache_stats_readable, mut cache_stats_count) = (false, None);
|
||||
if cache_stats_file_exists {
|
||||
match std::fs::read_to_string(POST_STATS_PATH) {
|
||||
Ok(data) => {
|
||||
match serde_json::from_str::<HashMap<String, PostStats>>(&data) {
|
||||
Ok(map) => {
|
||||
cache_stats_readable = true;
|
||||
cache_stats_count = Some(map.len());
|
||||
},
|
||||
Err(e) => errors.push(format!("Cache stats file not valid JSON: {}", e)),
|
||||
}
|
||||
},
|
||||
Err(e) => errors.push(format!("Failed to read cache stats file: {}", e)),
|
||||
}
|
||||
}
|
||||
HealthReport {
|
||||
posts_dir_exists,
|
||||
posts_count,
|
||||
cache_file_exists,
|
||||
cache_stats_file_exists,
|
||||
cache_readable,
|
||||
cache_stats_readable,
|
||||
cache_post_count,
|
||||
cache_stats_count,
|
||||
errors,
|
||||
}
|
||||
}
|
||||
cache_stats_count,
|
||||
errors,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user