Detect language for highlighting
This commit is contained in:
parent
789ba410f7
commit
001a27c694
4 changed files with 21 additions and 7 deletions
|
|
@ -136,7 +136,7 @@ pub fn make_router(config: &'static Config) -> impl Handler {
|
|||
return conn.with_status(404);
|
||||
};
|
||||
|
||||
let Some(file_hash) = root.find(conn.path().split('/')) else {
|
||||
let Some(repo_file) = root.find(conn.path().split('/')) else {
|
||||
return conn.with_status(404);
|
||||
};
|
||||
|
||||
|
|
@ -164,17 +164,30 @@ pub fn make_router(config: &'static Config) -> impl Handler {
|
|||
Some(buf)
|
||||
};
|
||||
let Some(file_content) = file_cache.fetch(
|
||||
(repo_hash_str.to_string(), file_hash.to_string()),
|
||||
(repo_hash_str.to_string(), repo_file.hash.clone()),
|
||||
fetch_file,
|
||||
) else {
|
||||
return conn.with_status(500);
|
||||
};
|
||||
|
||||
let file_lang = conn
|
||||
.path()
|
||||
.rsplit('.')
|
||||
.next()
|
||||
.unwrap_or(giallo::PLAIN_GRAMMAR_NAME);
|
||||
|
||||
let hl_options = giallo::HighlightOptions::new(
|
||||
"py",
|
||||
file_lang,
|
||||
giallo::ThemeVariant::Single("catppuccin-frappe"),
|
||||
);
|
||||
let highlighted = hl_registry.highlight(&file_content, &hl_options).unwrap();
|
||||
let highlighted = hl_registry.highlight(&file_content, &hl_options).unwrap_or_else(|_e| {
|
||||
// If extension is unknown
|
||||
let hl_options = giallo::HighlightOptions::new(
|
||||
giallo::PLAIN_GRAMMAR_NAME,
|
||||
giallo::ThemeVariant::Single("catppuccin-frappe"),
|
||||
);
|
||||
hl_registry.highlight(&file_content, &hl_options).unwrap()
|
||||
});
|
||||
let html = giallo::HtmlRenderer::default().render(
|
||||
&highlighted,
|
||||
&giallo::RenderOptions {
|
||||
|
|
|
|||
Loading…
Reference in a new issue