Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extract card image from html if not defined #113

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
1 change: 1 addition & 0 deletions example/content/customizing-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions example/templates/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
{% else %}
<meta property="og:description" content="{{ content.html | striptags | trim_start_matches(pat=content.title) | truncate(length=100, end="...") }}">
{%- endif %}
{% if content.extra.card_image %}
<meta property="og:image" content="{{url_for(path=content.extra.card_image, abs=true)}}">
{% if content.card_image %}
<meta property="og:image" content="{{url_for(path=content.card_image, abs=true)}}">
{% elif site.card_image %}
<meta property="og:image" content="{{url_for(path=site.card_image, abs=true)}}">
{% endif %}
Expand Down
1 change: 1 addition & 0 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Content {
pub extra: Option<Value>,
pub links_to: Option<Vec<String>>,
pub back_links: Vec<Self>,
pub card_image: Option<String>,
}

pub fn get_title<'a>(frontmatter: &'a Frontmatter, html: &'a str) -> String {
Expand Down
15 changes: 15 additions & 0 deletions src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn get_content(path: &Path) -> Result<Content, String> {
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,
Expand All @@ -39,10 +40,24 @@ pub fn get_content(path: &Path) -> Result<Content, String> {
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<String> {
if let Some(card_image) = frontmatter.get("card_image") {
return Some(card_image.to_string());
}
// first <img> src attribute
let img_regex = Regex::new(r#"<img[^>]*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<Vec<String>> {
let mut result = Vec::new();
let re = Regex::new(r#"href="\./(.*?)\.html""#).unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
1 change: 1 addition & 0 deletions src/tera_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down