refactor(highlight): output CSS to public/, add syntax aliases and case-insensitive lookup, fix code block CSS

This commit is contained in:
xfy 2026-06-03 14:08:16 +08:00
parent 34e3bcdf95
commit 5e449013d6
5 changed files with 37 additions and 9 deletions

3
.gitignore vendored
View File

@ -7,6 +7,7 @@
/package-lock.json /package-lock.json
others/ others/
public/style.css public/style.css
public/highlight.css
public/tiptap public/tiptap
generated/highlight.css generated/
.env .env

View File

@ -10,7 +10,7 @@ title = "Yggdrasil - Dioxus SSR"
watch_path = ["src", "Cargo.toml"] watch_path = ["src", "Cargo.toml"]
[web.resource] [web.resource]
style = ["/style.css", "/tiptap/editor.css"] style = ["/style.css", "/highlight.css", "/tiptap/editor.css"]
script = ["/tiptap/editor.js"] script = ["/tiptap/editor.js"]
[web.resource.dev] [web.resource.dev]

View File

@ -1,5 +1,4 @@
@import "tailwindcss"; @import "tailwindcss";
@import "./generated/highlight.css";
@custom-variant dark (&:where(.dark, .dark *)); @custom-variant dark (&:where(.dark, .dark *));
@ -343,6 +342,7 @@
.md-content pre { .md-content pre {
position: relative; position: relative;
margin-bottom: var(--content-gap-paper); margin-bottom: var(--content-gap-paper);
background: var(--color-paper-code-block);
border-radius: var(--radius-paper); border-radius: var(--radius-paper);
overflow-x: auto; overflow-x: auto;
} }
@ -353,7 +353,7 @@
background: transparent; background: transparent;
border-radius: 0; border-radius: 0;
overflow-x: auto; overflow-x: auto;
word-break: break-all; white-space: pre;
font-size: 0.85em; font-size: 0.85em;
line-height: 1.6; line-height: 1.6;
} }

View File

@ -25,11 +25,11 @@ fn main() {
output.push_str("\n/* Catppuccin Mocha (dark) */\n"); output.push_str("\n/* Catppuccin Mocha (dark) */\n");
output.push_str(&mocha_rewritten); output.push_str(&mocha_rewritten);
std::fs::create_dir_all("generated").expect("Failed to create generated/"); std::fs::create_dir_all("public").expect("Failed to create public/");
std::fs::write("generated/highlight.css", output) std::fs::write("public/highlight.css", output)
.expect("Failed to write generated/highlight.css"); .expect("Failed to write public/highlight.css");
println!("Generated generated/highlight.css"); println!("Generated public/highlight.css");
} }
fn strip_comments(css: &str) -> String { fn strip_comments(css: &str) -> String {

View File

@ -19,6 +19,32 @@ pub mod server {
if let Some(s) = ss.find_syntax_by_name(lang) { if let Some(s) = ss.find_syntax_by_name(lang) {
return s; 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") ss.find_syntax_by_extension("txt")
@ -27,12 +53,13 @@ pub mod server {
} }
pub fn highlight_code(code: &str, lang: Option<&str>) -> String { pub fn highlight_code(code: &str, lang: Option<&str>) -> String {
let trimmed = code.trim();
let syntax = find_syntax(lang); let syntax = find_syntax(lang);
let ss = &*SYNTAX_SET; let ss = &*SYNTAX_SET;
let mut generator = let mut generator =
ClassedHTMLGenerator::new_with_class_style(syntax, ss, ClassStyle::Spaced); 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) { if let Err(e) = generator.parse_html_for_line_which_includes_newline(line) {
tracing::warn!("syntect parse error: {:?}", e); tracing::warn!("syntect parse error: {:?}", e);
} }