server: add link to news posts back to original article

This commit is contained in:
Bill Thiede 2024-08-12 21:14:32 -07:00
parent c5def6c0e3
commit 8a237bf8e1
2 changed files with 26 additions and 2 deletions

View File

@ -116,6 +116,29 @@ impl Transformer for InlineStyle {
}
}
struct AddOutlink(Option<url::Url>);
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<String, TransformError> {
if let Some(url) = &self.0 {
Ok(format!(
r#"
{html}
<div><a href="{}">View on site</a></div>
"#,
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]);

View File

@ -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<Thread, ServerEr
// TODO: add site specific cleanups. For example:
// * Grafana does <div class="image-wrapp"><img class="lazyload>"<img src="/media/...>"</img></div>
// * Some sites appear to be HTML encoded, unencode them, i.e. imperialviolent
let body_tranformers: Vec<Box<dyn Transformer>> = vec![
let mut body_tranformers: Vec<Box<dyn Transformer>> = vec![
Box::new(AddOutlink(link.clone())),
Box::new(EscapeHtml),
Box::new(InlineStyle),
Box::new(SanitizeHtml {