From 703ab6a2ebd9c1cae672e8350fc23a475f94020f Mon Sep 17 00:00:00 2001 From: p6nj Date: Fri, 15 Nov 2024 14:17:02 -0500 Subject: [PATCH] markdown support --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 1 + src/main.rs | 11 ++++++----- src/markdown.rs | 26 ++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 src/markdown.rs diff --git a/Cargo.lock b/Cargo.lock index eeead43..ce2e550 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -944,6 +944,15 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "markdown" +version = "1.0.0-alpha.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6491e6c702bf7e3b24e769d800746d5f2c06a6c6a2db7992612e0f429029e81" +dependencies = [ + "unicode-id", +] + [[package]] name = "matchers" version = "0.1.0" @@ -2541,6 +2550,7 @@ dependencies = [ name = "typonomy" version = "0.1.0" dependencies = [ + "markdown", "rocket", "shuttle-rocket", "shuttle-runtime", @@ -2578,6 +2588,12 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ab17db44d7388991a428b2ee655ce0c212e862eff1768a455c58f9aad6e7893" +[[package]] +name = "unicode-id" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10103c57044730945224467c09f71a4db0071c123a0648cc3e818913bde6b561" + [[package]] name = "unicode-ident" version = "1.0.13" diff --git a/Cargo.toml b/Cargo.toml index 5dc6dab..d01d296 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +markdown = "1.0.0-alpha.21" rocket = "0" shuttle-rocket = "*" shuttle-runtime = "*" diff --git a/src/main.rs b/src/main.rs index 988779a..fd5d4c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,14 @@ use rocket::{get, routes}; +mod markdown; +use markdown::Markdown; + #[get("/")] -fn index() -> &'static str { - "Hello, world!" +fn index() -> Result { + "

Hello, world!

".parse() } #[shuttle_runtime::main] async fn main() -> shuttle_rocket::ShuttleRocket { - let rocket = rocket::build().mount("/", routes![index]); - - Ok(rocket.into()) + Ok(rocket::build().mount("/", routes![index]).into()) } diff --git a/src/markdown.rs b/src/markdown.rs new file mode 100644 index 0000000..caa7ccb --- /dev/null +++ b/src/markdown.rs @@ -0,0 +1,26 @@ +use std::str::FromStr; + +use rocket::{response::content::RawHtml, Responder}; + +#[derive(Responder)] +pub(super) struct Markdown(RawHtml); + +impl FromStr for Markdown { + type Err = String; + fn from_str(s: &str) -> Result { + markdown::to_html_with_options( + s, + &markdown::Options { + compile: markdown::CompileOptions { + allow_dangerous_html: true, + allow_dangerous_protocol: true, + ..markdown::CompileOptions::default() + }, + ..markdown::Options::default() + }, + ) + .map(RawHtml) + .map(Markdown) + .map_err(|e| e.to_string()) + } +}