This commit is contained in:
2025-06-25 19:12:37 +02:00
parent 2d373da4c5
commit 784dcbf91c
3 changed files with 42 additions and 3 deletions

View File

@@ -18,3 +18,4 @@ clap = { version = "4.4", features = ["derive"] }
serde_json = "1.0"
html-escape = "0.2.13"
once_cell = "1.18"
sysinfo = "0.30.7"

View File

@@ -1,4 +1,13 @@
// src/markdown.rs
/*
This is the Rust Markdown Parser.
It supports caching of posts and is
BLAZINGLY FAST!
*/
use std::fs;
use std::path::{Path, PathBuf};
@@ -19,6 +28,7 @@ use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::RwLock;
use serde_json;
use sysinfo::{System, Pid, RefreshKind, CpuRefreshKind, ProcessRefreshKind};
#[derive(Debug, Deserialize, Clone, serde::Serialize)]
pub struct PostFrontmatter {
@@ -47,6 +57,8 @@ pub struct PostStats {
pub cache_misses: u64,
pub last_interpret_time_ms: u128,
pub last_compile_time_ms: u128,
pub last_cpu_usage_percent: f32, // Not f64
pub last_cache_status: String, // "hit" or "miss"
}
static POST_CACHE: Lazy<RwLock<HashMap<String, Post>>> = Lazy::new(|| RwLock::new(HashMap::new()));
@@ -95,10 +107,10 @@ fn process_anchor_links(content: &str) -> String {
}
// Helper function to strip emojis from a string
// Neccesary for the slugify function to work correctly. And the ID's to work with the frontend.
fn strip_emojis(s: &str) -> String {
// Remove all characters in the Emoji Unicode ranges
// This is a simple approach and may not cover all emojis, but works for most cases
// Unicode emoji ranges: https://unicode.org/Public/emoji/15.0/emoji-data.txt
s.chars()
.filter(|c| {
let c = *c as u32;
@@ -128,11 +140,22 @@ static AMMONIA: Lazy<ammonia::Builder<'static>> = Lazy::new(|| {
});
pub fn rsparseinfo() -> String {
// Eagerly load all posts to populate stats
let _ = get_all_posts();
let stats = POST_STATS.read().unwrap();
serde_json::to_string(&stats.values().collect::<Vec<_>>()).unwrap_or_else(|_| "[]".to_string())
let values: Vec<&PostStats> = stats.values().collect();
if values.is_empty() {
"[]".to_string()
} else {
serde_json::to_string(&values).unwrap_or_else(|_| "[]".to_string())
}
}
pub fn get_post_by_slug(slug: &str) -> Result<Post, Box<dyn std::error::Error>> {
let mut sys = System::new_with_specifics(RefreshKind::new().with_processes(ProcessRefreshKind::everything()).with_cpu(CpuRefreshKind::everything()));
sys.refresh_processes();
let pid = sysinfo::get_current_pid()?;
let before_cpu = sys.process(pid).map(|p| p.cpu_usage()).unwrap_or(0.0);
let start = Instant::now();
let mut stats = POST_STATS.write().unwrap();
let entry = stats.entry(slug.to_string()).or_insert_with(|| PostStats {
@@ -144,9 +167,13 @@ pub fn get_post_by_slug(slug: &str) -> Result<Post, Box<dyn std::error::Error>>
entry.cache_hits += 1;
entry.last_interpret_time_ms = 0;
entry.last_compile_time_ms = 0;
entry.last_cache_status = "hit".to_string();
sys.refresh_process(pid);
entry.last_cpu_usage_percent = sys.process(pid).map(|p| p.cpu_usage()).unwrap_or(0.0) - before_cpu;
return Ok(post);
}
entry.cache_misses += 1;
entry.last_cache_status = "miss".to_string();
drop(stats); // Release lock before heavy work
let posts_dir = get_posts_directory();
let file_path = posts_dir.join(format!("{}.md", slug));
@@ -180,7 +207,7 @@ pub fn get_post_by_slug(slug: &str) -> Result<Post, Box<dyn std::error::Error>>
let mut code_block_lang = String::new();
let mut code_block_content = String::new();
let mut events = Vec::new();
let ss = SyntaxSet::load_defaults_newlines();
let ss = SyntaxSet::load_defaults_newlines(); // SS 卐
let ts = ThemeSet::load_defaults();
let theme = &ts.themes["base16-ocean.dark"];
for event in parser {
@@ -261,6 +288,8 @@ pub fn get_post_by_slug(slug: &str) -> Result<Post, Box<dyn std::error::Error>>
});
entry.last_interpret_time_ms = interpret_time.as_millis();
entry.last_compile_time_ms = compile_time.as_millis();
sys.refresh_process(pid);
entry.last_cpu_usage_percent = sys.process(pid).map(|p| p.cpu_usage()).unwrap_or(0.0) - before_cpu;
Ok(post)
}

View File

@@ -1,3 +1,12 @@
// This is the frontend Markdown parser.
// It is written in TypeScript
// While I was writing this, only I and God knew how it works.
// Now, only God knows.
//
// If you are trying to understand how it works , and optimize it. Please increse the counter
//
// Hours wasted here: 12
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';