diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e864668..299e8cb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -41,6 +41,6 @@ repos: - id: check-todos # [NOGREP] name: check-todos # [NOGREP] language: system - entry: sh -c "! grep --color=always --binary-files=without-match --dereference-recursive --exclude-dir='notes' --exclude-dir='target' --exclude-dir='node_modules' --exclude-dir='.git' --exclude='.pre-commit-config.yaml' --exclude-dir='venv' 'TODO' | grep -v '[NOGREP]'" # [NOGREP] + entry: sh -c "! grep --color=always --binary-files=without-match --dereference-recursive --exclude-dir='notes' --exclude-dir='target' --exclude-dir='node_modules' --exclude-dir='.git' --exclude='.pre-commit-config.yaml' --exclude-dir='venv' 'TODO' | grep -v '\[NOGREP\]'" # [NOGREP] pass_filenames: false always_run: true diff --git a/src/main.rs b/src/main.rs index b92568b..a411824 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ use tokio_stream::wrappers::BroadcastStream; use syntect::highlighting::ThemeSet; use syntect::parsing::SyntaxSet; + use tower_http::trace::TraceLayer; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use webbrowser; @@ -98,8 +99,6 @@ struct Args { #[derive(Clone)] struct AppState { - syntax_set: Arc, - theme_set: Arc, tx: Arc>, root: PathBuf, is_root_file: bool, @@ -123,14 +122,9 @@ async fn main() { .with(tracing_subscriber::fmt::layer()) .init(); - let ss = SyntaxSet::load_defaults_newlines(); - let ts = ThemeSet::load_defaults(); - let (tx, _rx) = broadcast::channel::(100); let state = AppState { - syntax_set: Arc::new(ss), - theme_set: Arc::new(ts), tx: Arc::new(tx), root: args.root.clone(), is_root_file, @@ -179,7 +173,7 @@ fn resolve_addr(host: &str, port: u16) -> io::Result { async fn root_handler(State(state): State) -> impl IntoResponse { if state.is_root_file { - match render_single_file(&state.root, &state).await { + match render_single_file(&state.root).await { Ok(template) => template.into_response(), Err(e) => e.into_response(), } @@ -193,7 +187,6 @@ async fn root_handler(State(state): State) -> impl IntoResponse { async fn render_single_file( file_path: &StdPath, - state: &AppState, ) -> Result { let metadata = match fs::metadata(file_path).await { Ok(m) => m, @@ -217,14 +210,17 @@ async fn render_single_file( let display_path = ""; + let ss = SyntaxSet::load_defaults_newlines(); + let ts = ThemeSet::load_defaults(); + let html_content = if extension == "md" { - markdown_to_html(&content, &state.syntax_set, &state.theme_set, display_path) + markdown_to_html(&content, display_path) } else { code_to_html( &content, extension.as_str(), - &state.syntax_set, - &state.theme_set, + &ss, + &ts, ) }; @@ -316,14 +312,17 @@ async fn serve_file( Err(_) => return Err(StatusCode::INTERNAL_SERVER_ERROR), }; + let ss = SyntaxSet::load_defaults_newlines(); + let ts = ThemeSet::load_defaults(); + let html_content = if extension == "md" { - markdown_to_html(&content, &state.syntax_set, &state.theme_set, &full_path) + markdown_to_html(&content, &full_path) } else { code_to_html( &content, extension.as_str(), - &state.syntax_set, - &state.theme_set, + &ss, + &ts, ) }; diff --git a/src/markdown.rs b/src/markdown.rs index b82ef0e..d5a2f31 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -2,10 +2,8 @@ use pulldown_cmark::{ BlockQuoteKind, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag, TagEnd, TextMergeStream, }; -use syntect::highlighting::ThemeSet; -use syntect::parsing::SyntaxSet; -pub fn markdown_to_html(markdown: &str, ss: &SyntaxSet, ts: &ThemeSet, _file_path: &str) -> String { +pub fn markdown_to_html(markdown: &str, _file_path: &str) -> String { let mut options = Options::empty(); options.insert(Options::ENABLE_TABLES); options.insert(Options::ENABLE_FOOTNOTES);