From 762344b5da05df672a99498cd281b73fa1964843 Mon Sep 17 00:00:00 2001 From: zyl Date: Wed, 13 Nov 2024 11:09:12 -0800 Subject: [PATCH] implement cdn prefix for embed metadata images --- site/pages/docs/pages.md | 2 ++ src/builder.rs | 9 +++++---- src/resource.rs | 9 ++++++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/site/pages/docs/pages.md b/site/pages/docs/pages.md index 7e34da8..cf4aa48 100644 --- a/site/pages/docs/pages.md +++ b/site/pages/docs/pages.md @@ -59,6 +59,8 @@ the embed's description. 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` the theme color to use for the embed. optional, but the default is currently nonconfigurable. diff --git a/src/builder.rs b/src/builder.rs index f7dd083..491c476 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -287,10 +287,11 @@ impl SiteBuilder { _ => self.site.config.title.clone(), }; - let head = page_metadata.embed.map(|mut embed| { - embed.site_name.clone_from(&self.site.config.title); - embed.build(self) - }); + let head = if let Some(embed) = page_metadata.embed { + Some(embed.build(self)?) + } else { + None + }; let out = self.tera.render( &page_metadata diff --git a/src/resource.rs b/src/resource.rs index bf78af2..7594c2a 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -81,7 +81,7 @@ pub struct EmbedMetadata { impl EmbedMetadata { /// builds the embed html tags - pub fn build(self, builder: &SiteBuilder) -> String { + pub fn build(self, builder: &SiteBuilder) -> eyre::Result { let mut s = format!( r#""#, self.title, builder.site.config.base_url @@ -93,7 +93,10 @@ impl EmbedMetadata { if let Some(description) = self.description { s = format!(r#"{s}"#); } - 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}"#); } s = format!( @@ -104,7 +107,7 @@ impl EmbedMetadata { s = format!(r#"{s}"#); } - s + Ok(s) } pub fn default_theme_color() -> String {