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
others/
public/style.css
public/highlight.css
public/tiptap
generated/highlight.css
generated/
.env

View File

@ -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]

View File

@ -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;
}

View File

@ -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 {

View File

@ -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);
}