From b0f6242b8b936d47b32c227cab5b18b4902cf9c4 Mon Sep 17 00:00:00 2001 From: gennyble Date: Fri, 3 May 2024 10:43:53 -0500 Subject: opengraph embeds :) --- src/fs.rs | 12 ++++++++++++ src/main.rs | 37 ++++++++++++++++++++++++++++++++----- src/settings.rs | 1 + 3 files changed, 45 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/fs.rs b/src/fs.rs index 7dc94d3..831aa86 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -57,6 +57,18 @@ impl Webpath { pub fn as_dir(&self) -> String { format!("/{}/", self.webcanon) } + + /// Returns the first directory of the Webpath. Assumes the path is a + /// directory if it ends in a `/`, otherwise pops the last component. + pub fn first_dir(&self) -> String { + if self.is_dir { + self.webcanon.to_string() + } else { + let mut dir = self.webcanon.clone(); + dir.pop(); + dir.to_string() + } + } } impl Deref for Webpath { diff --git a/src/main.rs b/src/main.rs index 57c373e..0c61389 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ use axum::{ routing::get, Extension, Router, }; -use bempline::{Document, Options}; +use bempline::{variables, Document, Options}; use camino::Utf8PathBuf; use confindent::Confindent; pub use error::RuntimeError; @@ -54,6 +54,7 @@ async fn main() { let conf = Confindent::from_file(std::env::args().nth(2).unwrap()).unwrap(); let webroot: Utf8PathBuf = conf.child_parse("Webroot").unwrap(); let templates = conf.child_value("Templates").unwrap(); + let hostname = conf.child_owned("Hostname").unwrap(); let fs = Filesystem::new(&webroot); @@ -61,6 +62,7 @@ async fn main() { template_dir: Utf8PathBuf::from(webroot.join(templates)) .canonicalize_utf8() .unwrap(), + hostname, }; let app = Router::new() @@ -213,10 +215,35 @@ async fn send_template( ) .unwrap(); - template.set( - "title", - templated.frontmatter.get("title").unwrap_or(filename), - ); + let title = templated.frontmatter.get("title").unwrap_or(filename); + + template.set("title", title); + + if let Some(og_description) = templated.frontmatter.get("description") { + let og_title = title; + let og_url = format!("https://{}{}", &settings.hostname, webpath); + + if let Some(art_relpath) = templated.frontmatter.get("art") { + let serving_dir = Utf8PathBuf::from(webpath.first_dir()); + let art_path = serving_dir.join(art_relpath); + + let og_image_alt = match templated.frontmatter.get("art_alt") { + Some(alt) => alt, + None => { + tracing::warn!("{} has art but no alt", resolve.filepath); + "" + } + }; + + let og_image = format!("https://{}/{}", &settings.hostname, art_path); + + variables!(template, og_image, og_image_alt); + } + + let og_site_name = &settings.hostname; + + variables!(template, og_title, og_url, og_description, og_site_name); + } // styles the templated stuff wants let style_pattern = template.get_pattern("styles").unwrap(); diff --git a/src/settings.rs b/src/settings.rs index 9a31d56..06abc18 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -3,4 +3,5 @@ use camino::Utf8PathBuf; #[derive(Clone, Debug)] pub struct Settings { pub template_dir: Utf8PathBuf, + pub hostname: String, } -- cgit 1.4.1-3-g733a5