implement custom embeds for regular pages

This commit is contained in:
zyl 2024-06-21 17:09:49 -07:00
parent 750c0c501f
commit 4fb1f2183d
No known key found for this signature in database
5 changed files with 28 additions and 6 deletions

View file

@ -86,7 +86,7 @@ impl ResourceMethods<BlogPostTemplateData> for ResourceMetadata<BlogPostMetadata
fn get_head_data(&self, site_config: &SiteConfig) -> eyre::Result<String> {
Ok(EmbedMetadata {
title: self.title.clone(),
site_name: &site_config.title,
site_name: site_config.title.clone(),
description: Some(self.inner.desc.clone()),
image: Some(self.inner.get_header_image(site_config)?),
url: None,

View file

@ -21,6 +21,8 @@ struct TemplateData<'a, T> {
pub page: &'a str,
/// The page's title.
pub title: &'a str,
/// Custom head data for the page.
pub head: Option<String>,
/// The page's custom scripts.
pub scripts: &'a [String],
/// the page's custom styles.
@ -214,11 +216,17 @@ impl<'a> SiteBuilder<'a> {
_ => self.site.config.title.clone(),
};
let head = page_metadata.embed.map(|mut embed| {
embed.site_name.clone_from(&self.site.config.title);
embed.build()
});
let out = self.reg.render(
&page_metadata.template.unwrap_or_else(|| "base".to_string()),
&TemplateData {
page: page_html,
title: &title,
head,
scripts: &page_metadata.scripts,
styles: &page_metadata.styles,
extra_data,

View file

@ -68,7 +68,7 @@ impl ResourceMethods<ImageTemplateData> for ResourceMetadata<ImageMetadata> {
fn get_head_data(&self, site_config: &SiteConfig) -> eyre::Result<String> {
Ok(EmbedMetadata {
title: self.title.clone(),
site_name: &site_config.title,
site_name: site_config.title.clone(),
description: self.inner.desc.clone(),
image: Some(self.inner.get_image_url(site_config)?),
url: None,

View file

@ -14,6 +14,7 @@ use std::{
};
use eyre::Context;
use resource::EmbedMetadata;
use serde::Deserialize;
use url::Url;
use util::get_name;
@ -67,6 +68,9 @@ pub struct PageMetadata {
pub title: Option<String>,
/// The template to use for the page. If not specified, it defaults to "base".
pub template: Option<String>,
/// custom embed info for a template
#[serde(default)]
pub embed: Option<EmbedMetadata>,
/// The page's custom scripts, if any.
#[serde(default)]
pub scripts: Vec<String>,

View file

@ -80,18 +80,24 @@ where
}
/// struct for adding custom meta content embeds
#[derive(Debug)]
pub struct EmbedMetadata<'s> {
#[derive(Debug, Deserialize)]
pub struct EmbedMetadata {
pub title: String,
pub site_name: &'s str,
#[serde(default)]
pub site_name: String,
#[serde(default)]
pub description: Option<String>,
#[serde(default)]
pub url: Option<String>,
#[serde(default)]
pub image: Option<String>,
#[serde(default = "EmbedMetadata::default_theme_color")]
pub theme_color: String,
#[serde(default)]
pub large_image: bool,
}
impl<'s> EmbedMetadata<'s> {
impl EmbedMetadata {
/// builds the embed html tags
pub fn build(self) -> String {
let mut s = format!(r#"<meta content="{}" property="og:title">"#, self.title);
@ -118,6 +124,10 @@ impl<'s> EmbedMetadata<'s> {
s
}
fn default_theme_color() -> String {
"rgb(255, 196, 252)".to_string()
}
}
#[derive(Debug, Serialize)]