diff --git a/Cargo.lock b/Cargo.lock index 92d374d..a52c576 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -32,6 +41,16 @@ dependencies = [ "libc", ] +[[package]] +name = "ansi-to-html" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d73c455ae09fa2223a75114789f30ad605e9e297f79537953523366c05995f5f" +dependencies = [ + "regex", + "thiserror", +] + [[package]] name = "anstream" version = "0.6.15" @@ -172,6 +191,7 @@ dependencies = [ name = "blog-thing" version = "0.1.0" dependencies = [ + "ansi-to-html", "axum", "chrono", "clap", @@ -592,6 +612,35 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -715,6 +764,26 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" +[[package]] +name = "thiserror" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tokio" version = "1.40.0" diff --git a/Cargo.toml b/Cargo.toml index 5460fa5..94bc374 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +ansi-to-html = "0.2.1" axum = "0.7.7" chrono = "0.4.38" clap = { version = "4.5.18", features = ["derive", "env"] } diff --git a/src/main.rs b/src/main.rs index efd3822..50821bf 100755 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,9 @@ use axum::{ routing::get, Router, }; -use std::{collections::HashMap, path::PathBuf, sync::Arc}; use clap::Parser; +use markdown::ParseOptions; +use std::{collections::HashMap, path::PathBuf, sync::Arc}; #[derive(Debug)] struct BlogState { @@ -77,25 +78,182 @@ struct Post { embed_template_name: Option, } +trait ToHtml { + fn is_directive(&self) -> bool; + fn to_html(&self) -> String; + fn template(&self) -> Option; +} + +impl ToHtml for markdown::mdast::Node { + fn to_html(&self) -> String { + let mut s = String::new(); + + if self.is_directive() { + return s; + } + + match self { + markdown::mdast::Node::Root(root) => { + for child in &root.children { + s += &child.to_html(); + } + } + markdown::mdast::Node::Blockquote(block_quote) => { + s += "
"; + for child in &block_quote.children { + s += &child.to_html(); + } + s += "
"; + } + markdown::mdast::Node::FootnoteDefinition(_) => {} + markdown::mdast::Node::MdxJsxFlowElement(_) => {} + markdown::mdast::Node::List(list) => { + s += ""; + } + markdown::mdast::Node::MdxjsEsm(_) => {} + markdown::mdast::Node::Toml(_) => {} + markdown::mdast::Node::Yaml(_) => {} + markdown::mdast::Node::Break(_) => {} + markdown::mdast::Node::InlineCode(inline_code) => { + s += &format!( + "{}", + &ansi_to_html::convert(&inline_code.value).unwrap() + ); + } + markdown::mdast::Node::InlineMath(_) => {} + markdown::mdast::Node::Delete(_) => {} + markdown::mdast::Node::Emphasis(emphasis) => { + s += ""; + for child in &emphasis.children { + s += &child.to_html(); + } + s += ""; + } + markdown::mdast::Node::MdxTextExpression(_) => {} + markdown::mdast::Node::FootnoteReference(_) => {} + markdown::mdast::Node::Html(_) => {} + markdown::mdast::Node::Image(image) => { + if image.url.ends_with(".mp4") { + s += &format!("