update embeds for the blog and add them to images

This commit is contained in:
zyl 2024-06-21 16:42:55 -07:00
parent 35ac34b69c
commit 750c0c501f
No known key found for this signature in database
3 changed files with 73 additions and 17 deletions

View file

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
resource::{ResourceBuilderConfig, ResourceMetadata, ResourceMethods},
resource::{EmbedMetadata, ResourceBuilderConfig, ResourceMetadata, ResourceMethods},
Site, SiteConfig,
};
@ -84,19 +84,15 @@ impl ResourceMethods<BlogPostTemplateData> for ResourceMetadata<BlogPostMetadata
}
fn get_head_data(&self, site_config: &SiteConfig) -> eyre::Result<String> {
// TODO: update this so we're not just doing raw html injection lmao
Ok(format!(
r#"
<meta property="og:site_name" content="{}">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="{}">
<meta name="twitter:image" content="{}">
<meta name="og:description" content="{}">
"#,
site_config.title,
self.title,
self.inner.get_header_image(site_config)?,
self.inner.desc,
))
Ok(EmbedMetadata {
title: self.title.clone(),
site_name: &site_config.title,
description: Some(self.inner.desc.clone()),
image: Some(self.inner.get_header_image(site_config)?),
url: None,
theme_color: "rgb(255, 196, 252)".to_string(),
large_image: true,
}
.build())
}
}

View file

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
resource::{ResourceBuilderConfig, ResourceMetadata, ResourceMethods},
resource::{EmbedMetadata, ResourceBuilderConfig, ResourceMetadata, ResourceMethods},
Site, SiteConfig,
};
@ -37,6 +37,12 @@ pub struct ImageMetadata {
pub file: String,
}
impl ImageMetadata {
fn get_image_url(&self, site_config: &SiteConfig) -> eyre::Result<String> {
Ok(site_config.cdn_url(&self.file)?.to_string())
}
}
/// Template data for a specific image.
#[derive(Debug, Serialize)]
pub struct ImageTemplateData {
@ -55,7 +61,20 @@ impl ResourceMethods<ImageTemplateData> for ResourceMetadata<ImageMetadata> {
site_config: &SiteConfig,
) -> eyre::Result<ImageTemplateData> {
Ok(ImageTemplateData {
src: site_config.cdn_url(&self.inner.file)?.to_string(),
src: self.inner.get_image_url(site_config)?,
})
}
fn get_head_data(&self, site_config: &SiteConfig) -> eyre::Result<String> {
Ok(EmbedMetadata {
title: self.title.clone(),
site_name: &site_config.title,
description: self.inner.desc.clone(),
image: Some(self.inner.get_image_url(site_config)?),
url: None,
theme_color: "rgb(255, 196, 252)".to_string(),
large_image: true,
}
.build())
}
}

View file

@ -79,6 +79,47 @@ where
}
}
/// struct for adding custom meta content embeds
#[derive(Debug)]
pub struct EmbedMetadata<'s> {
pub title: String,
pub site_name: &'s str,
pub description: Option<String>,
pub url: Option<String>,
pub image: Option<String>,
pub theme_color: String,
pub large_image: bool,
}
impl<'s> EmbedMetadata<'s> {
/// builds the embed html tags
pub fn build(self) -> String {
let mut s = format!(r#"<meta content="{}" property="og:title">"#, self.title);
s = format!(
r#"{s}<meta content="{}" property="og:site_name">"#,
self.site_name
);
if let Some(description) = self.description {
s = format!(r#"{s}<meta content="{description}" property="og:description">"#);
}
if let Some(url) = self.url {
s = format!(r#"{s}<meta content="{url}" property="og:url">"#);
}
if let Some(image) = self.image {
s = format!(r#"{s}<meta content="{image}" property="og:image">"#);
}
s = format!(
r#"{s}<meta content="{}" name="theme-color">"#,
self.theme_color
);
if self.large_image {
s = format!(r#"{s}<meta name="twitter:card" content="summary_large_image">"#);
}
s
}
}
#[derive(Debug, Serialize)]
struct ResourceListTemplateData<'r, M, E> {
resources: Vec<&'r ResourceTemplateData<'r, M, E>>,