mirror of
https://github.com/zyllian/zyllian.github.io.git
synced 2025-01-18 11:47:01 -08:00
implement custom embeds for regular pages
This commit is contained in:
parent
750c0c501f
commit
4fb1f2183d
5 changed files with 28 additions and 6 deletions
|
@ -86,7 +86,7 @@ impl ResourceMethods<BlogPostTemplateData> for ResourceMetadata<BlogPostMetadata
|
||||||
fn get_head_data(&self, site_config: &SiteConfig) -> eyre::Result<String> {
|
fn get_head_data(&self, site_config: &SiteConfig) -> eyre::Result<String> {
|
||||||
Ok(EmbedMetadata {
|
Ok(EmbedMetadata {
|
||||||
title: self.title.clone(),
|
title: self.title.clone(),
|
||||||
site_name: &site_config.title,
|
site_name: site_config.title.clone(),
|
||||||
description: Some(self.inner.desc.clone()),
|
description: Some(self.inner.desc.clone()),
|
||||||
image: Some(self.inner.get_header_image(site_config)?),
|
image: Some(self.inner.get_header_image(site_config)?),
|
||||||
url: None,
|
url: None,
|
||||||
|
|
|
@ -21,6 +21,8 @@ struct TemplateData<'a, T> {
|
||||||
pub page: &'a str,
|
pub page: &'a str,
|
||||||
/// The page's title.
|
/// The page's title.
|
||||||
pub title: &'a str,
|
pub title: &'a str,
|
||||||
|
/// Custom head data for the page.
|
||||||
|
pub head: Option<String>,
|
||||||
/// The page's custom scripts.
|
/// The page's custom scripts.
|
||||||
pub scripts: &'a [String],
|
pub scripts: &'a [String],
|
||||||
/// the page's custom styles.
|
/// the page's custom styles.
|
||||||
|
@ -214,11 +216,17 @@ impl<'a> SiteBuilder<'a> {
|
||||||
_ => self.site.config.title.clone(),
|
_ => 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(
|
let out = self.reg.render(
|
||||||
&page_metadata.template.unwrap_or_else(|| "base".to_string()),
|
&page_metadata.template.unwrap_or_else(|| "base".to_string()),
|
||||||
&TemplateData {
|
&TemplateData {
|
||||||
page: page_html,
|
page: page_html,
|
||||||
title: &title,
|
title: &title,
|
||||||
|
head,
|
||||||
scripts: &page_metadata.scripts,
|
scripts: &page_metadata.scripts,
|
||||||
styles: &page_metadata.styles,
|
styles: &page_metadata.styles,
|
||||||
extra_data,
|
extra_data,
|
||||||
|
|
|
@ -68,7 +68,7 @@ impl ResourceMethods<ImageTemplateData> for ResourceMetadata<ImageMetadata> {
|
||||||
fn get_head_data(&self, site_config: &SiteConfig) -> eyre::Result<String> {
|
fn get_head_data(&self, site_config: &SiteConfig) -> eyre::Result<String> {
|
||||||
Ok(EmbedMetadata {
|
Ok(EmbedMetadata {
|
||||||
title: self.title.clone(),
|
title: self.title.clone(),
|
||||||
site_name: &site_config.title,
|
site_name: site_config.title.clone(),
|
||||||
description: self.inner.desc.clone(),
|
description: self.inner.desc.clone(),
|
||||||
image: Some(self.inner.get_image_url(site_config)?),
|
image: Some(self.inner.get_image_url(site_config)?),
|
||||||
url: None,
|
url: None,
|
||||||
|
|
|
@ -14,6 +14,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use eyre::Context;
|
use eyre::Context;
|
||||||
|
use resource::EmbedMetadata;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use util::get_name;
|
use util::get_name;
|
||||||
|
@ -67,6 +68,9 @@ pub struct PageMetadata {
|
||||||
pub title: Option<String>,
|
pub title: Option<String>,
|
||||||
/// The template to use for the page. If not specified, it defaults to "base".
|
/// The template to use for the page. If not specified, it defaults to "base".
|
||||||
pub template: Option<String>,
|
pub template: Option<String>,
|
||||||
|
/// custom embed info for a template
|
||||||
|
#[serde(default)]
|
||||||
|
pub embed: Option<EmbedMetadata>,
|
||||||
/// The page's custom scripts, if any.
|
/// The page's custom scripts, if any.
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub scripts: Vec<String>,
|
pub scripts: Vec<String>,
|
||||||
|
|
|
@ -80,18 +80,24 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// struct for adding custom meta content embeds
|
/// struct for adding custom meta content embeds
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct EmbedMetadata<'s> {
|
pub struct EmbedMetadata {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub site_name: &'s str,
|
#[serde(default)]
|
||||||
|
pub site_name: String,
|
||||||
|
#[serde(default)]
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
pub url: Option<String>,
|
pub url: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
pub image: Option<String>,
|
pub image: Option<String>,
|
||||||
|
#[serde(default = "EmbedMetadata::default_theme_color")]
|
||||||
pub theme_color: String,
|
pub theme_color: String,
|
||||||
|
#[serde(default)]
|
||||||
pub large_image: bool,
|
pub large_image: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s> EmbedMetadata<'s> {
|
impl EmbedMetadata {
|
||||||
/// builds the embed html tags
|
/// builds the embed html tags
|
||||||
pub fn build(self) -> String {
|
pub fn build(self) -> String {
|
||||||
let mut s = format!(r#"<meta content="{}" property="og:title">"#, self.title);
|
let mut s = format!(r#"<meta content="{}" property="og:title">"#, self.title);
|
||||||
|
@ -118,6 +124,10 @@ impl<'s> EmbedMetadata<'s> {
|
||||||
|
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_theme_color() -> String {
|
||||||
|
"rgb(255, 196, 252)".to_string()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
|
|
Loading…
Add table
Reference in a new issue