diff --git a/.gitignore b/.gitignore index 975714f..6d0c5be 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ /package-lock.json others/ public/style.css +public/highlight.css public/tiptap -generated/highlight.css +generated/ .env diff --git a/Dioxus.toml b/Dioxus.toml index 6b734a9..00d96b1 100644 --- a/Dioxus.toml +++ b/Dioxus.toml @@ -10,7 +10,7 @@ title = "Yggdrasil - Dioxus SSR" watch_path = ["src", "Cargo.toml"] [web.resource] -style = ["/style.css", "/tiptap/editor.css"] +style = ["/style.css", "/highlight.css", "/tiptap/editor.css"] script = ["/tiptap/editor.js"] [web.resource.dev] diff --git a/input.css b/input.css index cd2f548..b0319ee 100644 --- a/input.css +++ b/input.css @@ -1,5 +1,4 @@ @import "tailwindcss"; -@import "./generated/highlight.css"; @custom-variant dark (&:where(.dark, .dark *)); @@ -343,6 +342,7 @@ .md-content pre { position: relative; margin-bottom: var(--content-gap-paper); + background: var(--color-paper-code-block); border-radius: var(--radius-paper); overflow-x: auto; } @@ -353,7 +353,7 @@ background: transparent; border-radius: 0; overflow-x: auto; - word-break: break-all; + white-space: pre; font-size: 0.85em; line-height: 1.6; } diff --git a/src/bin/generate_highlight_css.rs b/src/bin/generate_highlight_css.rs index b954978..7a44add 100644 --- a/src/bin/generate_highlight_css.rs +++ b/src/bin/generate_highlight_css.rs @@ -25,11 +25,11 @@ fn main() { output.push_str("\n/* Catppuccin Mocha (dark) */\n"); output.push_str(&mocha_rewritten); - std::fs::create_dir_all("generated").expect("Failed to create generated/"); - std::fs::write("generated/highlight.css", output) - .expect("Failed to write generated/highlight.css"); + std::fs::create_dir_all("public").expect("Failed to create public/"); + std::fs::write("public/highlight.css", output) + .expect("Failed to write public/highlight.css"); - println!("Generated generated/highlight.css"); + println!("Generated public/highlight.css"); } fn strip_comments(css: &str) -> String { diff --git a/src/highlight.rs b/src/highlight.rs index 6701d53..dff5ee6 100644 --- a/src/highlight.rs +++ b/src/highlight.rs @@ -19,6 +19,32 @@ pub mod server { if let Some(s) = ss.find_syntax_by_name(lang) { return s; } + let lower = lang.to_lowercase(); + if lower != lang { + if let Some(s) = ss.find_syntax_by_extension(&lower) { + return s; + } + if let Some(s) = ss.find_syntax_by_name(&lower) { + return s; + } + } + let aliases: &[(&str, &str)] = &[ + ("rust", "rs"), + ("js", "javascript"), + ("ts", "typescript"), + ("py", "python"), + ("rb", "ruby"), + ("sh", "bash"), + ("yaml", "yml"), + ("md", "markdown"), + ]; + for &(from, to) in aliases { + if lang == from { + if let Some(s) = ss.find_syntax_by_extension(to) { + return s; + } + } + } } } ss.find_syntax_by_extension("txt") @@ -27,12 +53,13 @@ pub mod server { } pub fn highlight_code(code: &str, lang: Option<&str>) -> String { + let trimmed = code.trim(); let syntax = find_syntax(lang); let ss = &*SYNTAX_SET; let mut generator = ClassedHTMLGenerator::new_with_class_style(syntax, ss, ClassStyle::Spaced); - for line in LinesWithEndings::from(code) { + for line in LinesWithEndings::from(trimmed) { if let Err(e) = generator.parse_html_for_line_which_includes_newline(line) { tracing::warn!("syntect parse error: {:?}", e); }