diff --git a/server/src/lib.rs b/server/src/lib.rs index 11c5d30..b501767 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -116,6 +116,29 @@ impl Transformer for InlineStyle { } } +struct AddOutlink(Option); + +impl Transformer for AddOutlink { + fn should_run(&self, _: &str) -> bool { + self.0 + .as_ref() + .map(|l| l.scheme().starts_with("http")) + .unwrap_or(false) + } + fn transform(&self, html: &str) -> Result { + if let Some(url) = &self.0 { + Ok(format!( + r#" + {html} +
View on site
+ "#, + url + )) + } else { + Ok(html.to_string()) + } + } +} pub fn linkify_html(text: &str) -> String { let mut finder = LinkFinder::new(); let finder = finder.url_must_have_scheme(false).kinds(&[LinkKind::Url]); diff --git a/server/src/newsreader.rs b/server/src/newsreader.rs index 0bae333..06fad43 100644 --- a/server/src/newsreader.rs +++ b/server/src/newsreader.rs @@ -13,7 +13,7 @@ use crate::{ compute_offset_limit, error::ServerError, graphql::{Body, Email, Html, Message, Tag, Thread, ThreadSummary}, - EscapeHtml, InlineStyle, SanitizeHtml, StripHtml, Transformer, + AddOutlink, EscapeHtml, InlineStyle, SanitizeHtml, StripHtml, Transformer, }; pub fn is_newsreader_search(query: &str) -> bool { @@ -191,7 +191,8 @@ pub async fn thread(pool: &PgPool, thread_id: String) -> Result // * Some sites appear to be HTML encoded, unencode them, i.e. imperialviolent - let body_tranformers: Vec> = vec![ + let mut body_tranformers: Vec> = vec![ + Box::new(AddOutlink(link.clone())), Box::new(EscapeHtml), Box::new(InlineStyle), Box::new(SanitizeHtml {