From 9be1dc93dee53574d9e7cd2bbbd148eafd56ec45 Mon Sep 17 00:00:00 2001 From: kageru Date: Tue, 26 Mar 2024 10:59:56 +0100 Subject: [PATCH] add OG image to card details --- src/main.rs | 21 ++++++++++++++++----- static/header.html | 1 + 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index f07f98b..5b9ff6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -138,7 +138,7 @@ async fn search(q: Option, web::Form>>) -> AnyRe }; match data { TargetPage::Data(data) => { - add_data(&mut res, &data)?; + add_data(&mut res, &data, None)?; Ok(HttpResponse::Ok().insert_header(header::ContentType::html()).body(res)) } TargetPage::Redirect(target) => Ok(HttpResponse::Found().insert_header((header::LOCATION, target)).finish()), @@ -168,7 +168,7 @@ async fn card_info(card_id: web::Path) -> AnyResult { body: "Card not found".to_owned(), }, }; - add_data(&mut res, &data)?; + add_data(&mut res, &data, Some(*card_id))?; Ok(HttpResponse::Ok().insert_header(header::ContentType::html()).body(res)) } @@ -181,7 +181,7 @@ async fn help() -> AnyResult { body: HELP_CONTENT.to_owned(), description: String::new(), }; - add_data(&mut res, &data)?; + add_data(&mut res, &data, None)?; Ok(HttpResponse::Ok().insert_header(header::ContentType::html()).body(res)) } @@ -254,9 +254,20 @@ fn compute_results(raw_query: String) -> AnyResult { } } -fn add_data(res: &mut String, pd: &PageData) -> AnyResult<()> { +fn add_data(res: &mut String, pd: &PageData, card_id: Option) -> AnyResult<()> { res.push_str( - &HEADER.replacen("{DESCRIPTION}", &pd.description.replace('"', r#"\""#), 2).replacen("{IMG_HOST}", &IMG_HOST, 1).replacen("{TITLE}", &pd.title, 2), + &HEADER + .replacen("{DESCRIPTION}", &pd.description.replace('"', r#"\""#), 2) + .replacen("{IMG_HOST}", &IMG_HOST, 2) + .replacen("{TITLE}", &pd.title, 2) + .replacen( + "{OG_IMAGE}", + &match card_id { + Some(id) => format!(r#""#, IMG_HOST.as_str()), + None => String::new(), + }, + 1, + ), ); add_searchbox(res, &pd.query)?; res.push_str(&pd.body); diff --git a/static/header.html b/static/header.html index 3b6de3a..cc37300 100644 --- a/static/header.html +++ b/static/header.html @@ -2,6 +2,7 @@ +{OG_IMAGE}