implement cdn prefix for embed metadata images

This commit is contained in:
zyl 2024-11-13 11:09:12 -08:00
parent 58ba627b9d
commit 762344b5da
Signed by: zyl
SSH key fingerprint: SHA256:uxxbSXbdroP/OnKBGnEDk5q7EKB2razvstC/KmzdXXs
3 changed files with 13 additions and 7 deletions

View file

@ -59,6 +59,8 @@ the embed's description. optional.
full url to an image to use for the embed. optional. full url to an image to use for the embed. optional.
prefixing a url with `cdn$` will join it with the site's cdn url as defined in the site config.
#### `theme_color` #### `theme_color`
the theme color to use for the embed. optional, but the default is currently nonconfigurable. the theme color to use for the embed. optional, but the default is currently nonconfigurable.

View file

@ -287,10 +287,11 @@ impl SiteBuilder {
_ => self.site.config.title.clone(), _ => self.site.config.title.clone(),
}; };
let head = page_metadata.embed.map(|mut embed| { let head = if let Some(embed) = page_metadata.embed {
embed.site_name.clone_from(&self.site.config.title); Some(embed.build(self)?)
embed.build(self) } else {
}); None
};
let out = self.tera.render( let out = self.tera.render(
&page_metadata &page_metadata

View file

@ -81,7 +81,7 @@ pub struct EmbedMetadata {
impl EmbedMetadata { impl EmbedMetadata {
/// builds the embed html tags /// builds the embed html tags
pub fn build(self, builder: &SiteBuilder) -> String { pub fn build(self, builder: &SiteBuilder) -> eyre::Result<String> {
let mut s = format!( let mut s = format!(
r#"<meta content="{}" property="og:title"><meta content="{}" property="og:url">"#, r#"<meta content="{}" property="og:title"><meta content="{}" property="og:url">"#,
self.title, builder.site.config.base_url self.title, builder.site.config.base_url
@ -93,7 +93,10 @@ impl EmbedMetadata {
if let Some(description) = self.description { if let Some(description) = self.description {
s = format!(r#"{s}<meta content="{description}" property="og:description">"#); s = format!(r#"{s}<meta content="{description}" property="og:description">"#);
} }
if let Some(image) = self.image { if let Some(mut image) = self.image {
if image.starts_with("cdn$") {
image = builder.site.config.cdn_url(&image[4..])?.to_string();
}
s = format!(r#"{s}<meta content="{image}" property="og:image">"#); s = format!(r#"{s}<meta content="{image}" property="og:image">"#);
} }
s = format!( s = format!(
@ -104,7 +107,7 @@ impl EmbedMetadata {
s = format!(r#"{s}<meta name="twitter:card" content="summary_large_image">"#); s = format!(r#"{s}<meta name="twitter:card" content="summary_large_image">"#);
} }
s Ok(s)
} }
pub fn default_theme_color() -> String { pub fn default_theme_color() -> String {