Skip to content

Commit 37caf58

Browse files
Add sharing to Facebook and Twitter for posts
1 parent ab1c7d2 commit 37caf58

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/blog.rs

+29
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub struct BlogConf {
3030
giscus: Option<Giscus>,
3131
google_analytics: Option<GoogleAnalytics>,
3232
syntax_highlight: Option<bool>,
33+
facebook: Option<String>, // Facebook sharing link
34+
twitter_share: Option<String>, // Twitter sharing link
3335
}
3436

3537
#[derive(Debug, Deserialize, Serialize)]
@@ -126,6 +128,25 @@ impl Blog {
126128
pub fn get_current_year(&self) -> i32 {
127129
Utc::now().year()
128130
}
131+
132+
// Generate sharing links for Facebook and Twitter
133+
pub fn generate_facebook_twitter_links(&self, post: &Post) -> (Option<String>, Option<String>) {
134+
let post_url = format!("{}{}", self.conf.get_root(), post.get_url());
135+
let facebook_link = self.conf.facebook.as_ref().map(|_| {
136+
format!(
137+
"https://www.facebook.com/sharer/sharer.php?u={}",
138+
post_url
139+
)
140+
});
141+
let twitter_link = self.conf.twitter_share.as_ref().map(|_| {
142+
format!(
143+
"https://twitter.com/intent/tweet?url={}&text={}",
144+
post_url,
145+
post.get_metadata().get_title()
146+
)
147+
});
148+
(facebook_link, twitter_link)
149+
}
129150
}
130151

131152
impl BlogConf {
@@ -201,6 +222,14 @@ impl BlogConf {
201222
pub fn get_syntax_highlight(&self) -> Option<bool> {
202223
self.syntax_highlight
203224
}
225+
226+
pub fn get_facebook(&self) -> Option<&str> {
227+
self.facebook.as_deref()
228+
}
229+
230+
pub fn get_twitter_share(&self) -> Option<&str> {
231+
self.twitter_share.as_deref()
232+
}
204233
}
205234

206235
impl Post {

templates/base.rs.html

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
@if let Some(twitter) = blog.get_blog_conf().get_twitter() {
2929
<a href="https://twitter.com/@twitter">Twitter</a>
3030
}
31+
@if let Some(facebook) = blog.get_blog_conf().get_facebook() {
32+
<a href="https://www.facebook.com/@facebook">Facebook</a>
33+
}
3134
</nav>
3235
</header>
3336
<main>
@@ -44,6 +47,9 @@
4447
@if let Some(twitter) = blog.get_blog_conf().get_twitter() {
4548
<a href="https://twitter.com/@twitter">Twitter</a>
4649
}
50+
@if let Some(facebook) = blog.get_blog_conf().get_facebook() {
51+
<a href="https://www.facebook.com/@facebook">Facebook</a>
52+
}
4753
<a href="/rss">
4854
<a href="/rss">RSS</a>
4955
</a>

0 commit comments

Comments
 (0)