Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,18 @@ use tokio::sync::mpsc;
#[prefix = "static/"]
struct Static;

impl Static {
fn get_styles() -> String {
match Static::get("static/global.css") {
Some(file) => match std::str::from_utf8(&file.data) {
Ok(css) => css.to_string(),
Err(e) => {
eprintln!("Failed to read CSS file: {e}");
String::new()
}
},
None => {
eprintln!("CSS file not found in embedded assets.");
String::new()
}
}
}

fn get_scripts() -> String {
match Static::get("static/client.js") {
Some(file) => match std::str::from_utf8(&file.data) {
Ok(js) => js.to_string(),
Err(e) => {
eprintln!("Failed to read JS file: {e}");
String::new()
}
},
None => {
eprintln!("JS file not found in embedded assets.");
fn get_embedded_file(file_path: &str) -> String {
match Static::get(file_path) {
Some(file) => match std::str::from_utf8(&file.data) {
Ok(content) => content.to_string(),
Err(e) => {
eprintln!("Failed to read embedded file: {e}");
String::new()
}
},
None => {
eprintln!("File not found in embedded files.");
String::new()
}
}
}
Expand Down Expand Up @@ -134,6 +116,23 @@ struct Mdwatch {
title: String,
style: String,
script: String,
lib: Libs,
}

struct Libs {
hljs_theme_dark: String,
hljs_theme_light: String,
hljs_script: String,
}

impl Default for Libs {
fn default() -> Self {
Self {
hljs_theme_dark: get_embedded_file("static/lib/github-dark.min.css"),
hljs_theme_light: get_embedded_file("static/lib/github-light.min.css"),
hljs_script: get_embedded_file("static/lib/highlight.min.js"),
}
}
}

#[get("/")]
Expand Down Expand Up @@ -174,8 +173,9 @@ async fn home(file: web::Data<String>) -> actix_web::Result<HttpResponse> {
let template = Mdwatch {
content: html_output,
title: file_name.to_string_lossy().to_string(),
style: Static::get_styles(),
script: Static::get_scripts(),
style: get_embedded_file("static/global.css"),
script: get_embedded_file("static/client.js"),
lib: Libs::default(),
};

match template.render() {
Expand Down
13 changes: 11 additions & 2 deletions static/client.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
function setThemeIcon(theme) {
const button = document.querySelector(".theme-toggle");
if (!button) return;

button.innerHTML =
theme === "light"
? '<i class="fa-regular fa-sun"></i>'
: '<i class="fa-solid fa-moon"></i>';
? `
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor" class="size-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 3v2.25m6.364.386-1.591 1.591M21 12h-2.25m-.386 6.364-1.591-1.591M12 18.75V21m-4.773-4.227-1.591 1.591M5.25 12H3m4.227-4.773L5.636 5.636M15.75 12a3.75 3.75 0 1 1-7.5 0 3.75 3.75 0 0 1 7.5 0Z" />
</svg>
`
: `
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor" class="size-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M21.752 15.002A9.72 9.72 0 0 1 18 15.75c-5.385 0-9.75-4.365-9.75-9.75 0-1.33.266-2.597.748-3.752A9.753 9.753 0 0 0 3 11.25C3 16.635 7.365 21 12.75 21a9.753 9.753 0 0 0 9.002-5.998Z" />
</svg>
`;
}

function highlightBlocks(root = document) {
Expand Down
10 changes: 10 additions & 0 deletions static/lib/github-dark.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions static/lib/github-light.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading