Skip to content

Commit

Permalink
Add og:image to the article page
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <[email protected]>
  • Loading branch information
JmPotato committed Aug 23, 2024
1 parent 7556313 commit e919437
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ chrono = { version = "0.4", features = ["serde"] }
comrak = { version = "0.27", features = ["syntect"] }
minijinja = { version = "2.1", features = ["loader"] }
password-auth = "1.0.0"
rand = "0.8"
regex = "1.10"
serde = { version = "1.0", features = ["derive"] }
sqlx = { version = "0.8", features = [
"runtime-tokio",
Expand Down
15 changes: 15 additions & 0 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use axum::{
use axum_login::AuthSession;
use chrono::Datelike;
use minijinja::context;
use rand::{thread_rng, Rng};
use regex::Regex;
use serde::Deserialize;
use tracing::error;

Expand Down Expand Up @@ -71,6 +73,19 @@ pub async fn handler_article(
.split(',')
.map(|s| s.trim().to_string())
.collect::<Vec<String>>(),
image => {
// find all image URLs in the article markdown content and choose one randomly.
let re = Regex::new(r"\!\[.*?\]\((.*?)\)").unwrap();
let mut image_urls: Vec<String> = vec![];
for (_, [image_url]) in re.captures_iter(&article.content).map(|c| c.extract()) {
image_urls.push(image_url.to_string());
}
if image_urls.is_empty() {
None
} else {
Some(image_urls[thread_rng().gen_range(0..image_urls.len())].clone())
}
},
logged_in => auth_session.user.is_some(),
},
)
Expand Down
2 changes: 1 addition & 1 deletion src/models/articles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tracing::info;
pub struct Article {
pub id: i32,
title: String,
content: String,
pub content: String,
pub tags: String,
pub created_at: NaiveDateTime,
updated_at: NaiveDateTime,
Expand Down
3 changes: 3 additions & 0 deletions templates/article.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

{% block head %}
<meta property="og:title" content="{{ article.title }}" />
<meta property="og:description" content="{{ article.content | truncate_str(200) }}" />
<meta property="og:image" content="{{ image }}" />
{% if config.twitter_card.enabled %}
<meta name="twitter:card" content="summary">
<meta name="twitter:creator" content="{{ config.blog_author }}">
<meta name="twitter:creator:id" content="{{ config.twitter_card.user_id }}">
<meta name="twitter:title" content="{{ article.title | truncate_str(70) }}">
<meta name="twitter:description" content="{{ article.content | truncate_str(200) }}">
<meta name="twitter:image" content="{{ image }}">
{% endif %}
{% endblock %}

Expand Down

0 comments on commit e919437

Please sign in to comment.