markdown support

This commit is contained in:
Ponj 2024-11-15 14:17:02 -05:00
parent 3d36b53fa1
commit 703ab6a2eb
Signed by: p6nj
GPG key ID: 6FED68D87C479A59
4 changed files with 49 additions and 5 deletions

16
Cargo.lock generated
View file

@ -944,6 +944,15 @@ dependencies = [
"tracing-subscriber", "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]] [[package]]
name = "matchers" name = "matchers"
version = "0.1.0" version = "0.1.0"
@ -2541,6 +2550,7 @@ dependencies = [
name = "typonomy" name = "typonomy"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"markdown",
"rocket", "rocket",
"shuttle-rocket", "shuttle-rocket",
"shuttle-runtime", "shuttle-runtime",
@ -2578,6 +2588,12 @@ version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ab17db44d7388991a428b2ee655ce0c212e862eff1768a455c58f9aad6e7893" checksum = "5ab17db44d7388991a428b2ee655ce0c212e862eff1768a455c58f9aad6e7893"
[[package]]
name = "unicode-id"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10103c57044730945224467c09f71a4db0071c123a0648cc3e818913bde6b561"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.13" version = "1.0.13"

View file

@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
markdown = "1.0.0-alpha.21"
rocket = "0" rocket = "0"
shuttle-rocket = "*" shuttle-rocket = "*"
shuttle-runtime = "*" shuttle-runtime = "*"

View file

@ -1,13 +1,14 @@
use rocket::{get, routes}; use rocket::{get, routes};
mod markdown;
use markdown::Markdown;
#[get("/")] #[get("/")]
fn index() -> &'static str { fn index() -> Result<Markdown, String> {
"Hello, world!" "<h1>Hello, world!</h1>".parse()
} }
#[shuttle_runtime::main] #[shuttle_runtime::main]
async fn main() -> shuttle_rocket::ShuttleRocket { async fn main() -> shuttle_rocket::ShuttleRocket {
let rocket = rocket::build().mount("/", routes![index]); Ok(rocket::build().mount("/", routes![index]).into())
Ok(rocket.into())
} }

26
src/markdown.rs Normal file
View file

@ -0,0 +1,26 @@
use std::str::FromStr;
use rocket::{response::content::RawHtml, Responder};
#[derive(Responder)]
pub(super) struct Markdown(RawHtml<String>);
impl FromStr for Markdown {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
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())
}
}