server: add link to news posts back to original article
This commit is contained in:
parent
c5def6c0e3
commit
8a237bf8e1
@ -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 {
|
pub fn linkify_html(text: &str) -> String {
|
||||||
let mut finder = LinkFinder::new();
|
let mut finder = LinkFinder::new();
|
||||||
let finder = finder.url_must_have_scheme(false).kinds(&[LinkKind::Url]);
|
let finder = finder.url_must_have_scheme(false).kinds(&[LinkKind::Url]);
|
||||||
|
|||||||
@ -13,7 +13,7 @@ use crate::{
|
|||||||
compute_offset_limit,
|
compute_offset_limit,
|
||||||
error::ServerError,
|
error::ServerError,
|
||||||
graphql::{Body, Email, Html, Message, Tag, Thread, ThreadSummary},
|
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 {
|
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:
|
// TODO: add site specific cleanups. For example:
|
||||||
// * Grafana does <div class="image-wrapp"><img class="lazyload>"<img src="/media/...>"</img></div>
|
// * 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
|
// * 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(EscapeHtml),
|
||||||
Box::new(InlineStyle),
|
Box::new(InlineStyle),
|
||||||
Box::new(SanitizeHtml {
|
Box::new(SanitizeHtml {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user