diff --git a/Cargo.toml b/Cargo.toml index 0338fca..b4b1feb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ anyhow = "1.0" chrono = { version = "0.4.41", features = ["serde"] } clap = { version = "4.5", features = ["derive"] } redb = "3.0" +regex = "1.11" rocket = "0.5" rocket_dyn_templates = { version = "0.2", features = ["tera"] } url = { version = "2.5", features = ["serde"] } diff --git a/src/main.rs b/src/main.rs index 475c64c..17c6781 100644 --- a/src/main.rs +++ b/src/main.rs @@ -219,7 +219,29 @@ fn rocket() -> _ { warn!("Canonical URL option is required for the RSS feed by the specification!") // @TODO } rocket::build() - .attach(Template::fairing()) + .attach(Template::custom(|engines| { + engines.tera.register_filter("url", { + use regex::{Captures, Regex}; + use rocket_dyn_templates::tera::{Error, Value}; + Box::new( + move |v: &Value, + _: &std::collections::HashMap| + -> Result { + match v.as_str() { + Some(s) => match Regex::new(r"[A-z]+:\/\/(?:[^\s]+|$)") { + Ok(r) => Ok(r + .replace_all(s, |c: &Captures| { + format!("{}", &c[0], &c[0]) + }) + .into()), + Err(e) => Err(Error::msg(e)), + }, + None => Err(Error::msg("Expected a string")), + } + }, + ) + }) + })) .configure(rocket::Config { port: config.port, address: config.host, diff --git a/templates/index.html.tera b/templates/index.html.tera index 7fbce0c..62cc6dc 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -11,7 +11,7 @@ {% for post in posts %}
-

{{ post.message | linebreaksbr | safe }}

+

{{ post.message | url | linebreaksbr | safe }}

  • {{ post.time }}
  • diff --git a/templates/post.html.tera b/templates/post.html.tera index 52075f0..9ef8212 100644 --- a/templates/post.html.tera +++ b/templates/post.html.tera @@ -2,7 +2,7 @@ {% block content %}
    -

    {{ post.message | linebreaksbr | safe }}

    +

    {{ post.message | url | linebreaksbr | safe }}