From 27d6359e5a56972fa315128203c7ad32a33ca40a Mon Sep 17 00:00:00 2001 From: Bruno Rocha Date: Sun, 3 Nov 2024 16:26:26 +0000 Subject: [PATCH] feat: extract card image from html if not defined --- ...10-19-what-is-marmite-static-site-generator.md | 6 ++++++ example/content/customizing-templates.md | 1 + example/templates/content.html | 4 ++-- src/content.rs | 1 + src/markdown.rs | 15 +++++++++++++++ src/site.rs | 1 + src/tera_functions.rs | 1 + 7 files changed, 27 insertions(+), 2 deletions(-) diff --git a/example/content/2024-10-19-what-is-marmite-static-site-generator.md b/example/content/2024-10-19-what-is-marmite-static-site-generator.md index e56aa55..3e5c6d6 100644 --- a/example/content/2024-10-19-what-is-marmite-static-site-generator.md +++ b/example/content/2024-10-19-what-is-marmite-static-site-generator.md @@ -157,6 +157,12 @@ Content - tag2 ``` **default** empty + +**card_image** + + : Image url to use as social card image `og:image` + **format** `./media/file.png` or `https://path/to/img.jpg` + **default** first image extracted from html content, config card_image, or None. **extra** diff --git a/example/content/customizing-templates.md b/example/content/customizing-templates.md index a32a339..2bba553 100644 --- a/example/content/customizing-templates.md +++ b/example/content/customizing-templates.md @@ -68,6 +68,7 @@ date: DateTimeObject or None extra: {key: value} links_to: [str] or None back_links: [Content] or [] +card_image: str or None ``` There are 6 templates inside the `templates` folder, each adds more data to context. diff --git a/example/templates/content.html b/example/templates/content.html index e3c273c..74eb0a8 100644 --- a/example/templates/content.html +++ b/example/templates/content.html @@ -7,8 +7,8 @@ {% else %} {%- endif %} -{% if content.extra.card_image %} - +{% if content.card_image %} + {% elif site.card_image %} {% endif %} diff --git a/src/content.rs b/src/content.rs index c565e8c..356018c 100644 --- a/src/content.rs +++ b/src/content.rs @@ -20,6 +20,7 @@ pub struct Content { pub extra: Option, pub links_to: Option>, pub back_links: Vec, + pub card_image: Option, } pub fn get_title<'a>(frontmatter: &'a Frontmatter, html: &'a str) -> String { diff --git a/src/markdown.rs b/src/markdown.rs index 4e056d3..658fb6b 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -29,6 +29,7 @@ pub fn get_content(path: &Path) -> Result { let extra = frontmatter.get("extra").map(std::borrow::ToOwned::to_owned); let links_to = get_links_to(&html); let back_links = Vec::new(); // will be mutated later + let card_image = get_card_image(&frontmatter, &html); let content = Content { title, description, @@ -39,10 +40,24 @@ pub fn get_content(path: &Path) -> Result { extra, links_to, back_links, + card_image, }; Ok(content) } +/// Capture `card_image` from frontmatter, then if not defined +/// take the first img src found in the post content +fn get_card_image(frontmatter: &Frontmatter, html: &str) -> Option { + if let Some(card_image) = frontmatter.get("card_image") { + return Some(card_image.to_string()); + } + // first src attribute + let img_regex = Regex::new(r#"]*src="([^"]+)""#).unwrap(); + img_regex + .captures(html) + .and_then(|caps| caps.get(1).map(|m| m.as_str().to_string())) +} + fn get_links_to(html: &str) -> Option> { let mut result = Vec::new(); let re = Regex::new(r#"href="\./(.*?)\.html""#).unwrap(); diff --git a/src/site.rs b/src/site.rs index 80ae8bd..d62a7b5 100644 --- a/src/site.rs +++ b/src/site.rs @@ -564,6 +564,7 @@ fn handle_404( tags: vec![], links_to: None, back_links: vec![], + card_image: None, }; if input_404_path.exists() { let custom_content = get_content(&input_404_path)?; diff --git a/src/tera_functions.rs b/src/tera_functions.rs index cf91165..03dd30f 100644 --- a/src/tera_functions.rs +++ b/src/tera_functions.rs @@ -14,6 +14,7 @@ impl Function for UrlFor { .get("path") .and_then(Value::as_str) .ok_or_else(|| tera::Error::msg("Missing `path` argument"))? + .trim_start_matches("./") .to_string(); let abs_prefixes = ["http", "https", "mailto"];