Skip to content

Commit 048896e

Browse files
committed
Add social sharing functionality with buttons for Facebook, Twitter, and LinkedIn
1 parent 322fa16 commit 048896e

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"mongoose": "^8.1.1",
2828
"multer": "^1.4.5-lts.1",
2929
"openai": "^4.71.1",
30+
"social-share-js": "^1.0.4",
3031
"tinymce": "^7.5.0"
3132
}
3233
}

public/css/style.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@ nav.navbar {
1515
height: 20px;
1616
margin-left: 5px;
1717
}
18+
19+
.social-share {
20+
margin-top: 20px;
21+
}
22+
23+
.share-button {
24+
margin-right: 10px;
25+
}
26+
27+
.share-button i {
28+
margin-right: 5px;
29+
}

public/js/socialShare.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
document.addEventListener('DOMContentLoaded', function() {
2+
const shareButtons = document.querySelectorAll('.share-button');
3+
4+
shareButtons.forEach(button => {
5+
button.addEventListener('click', function(e) {
6+
e.preventDefault();
7+
const network = this.dataset.network;
8+
const url = encodeURIComponent(window.location.href);
9+
const title = encodeURIComponent(document.title);
10+
11+
let shareUrl;
12+
switch(network) {
13+
case 'facebook':
14+
shareUrl = `https://www.facebook.com/sharer/sharer.php?u=${url}`;
15+
break;
16+
case 'twitter':
17+
shareUrl = `https://twitter.com/intent/tweet?url=${url}&text=${title}`;
18+
break;
19+
case 'linkedin':
20+
shareUrl = `https://www.linkedin.com/shareArticle?mini=true&url=${url}&title=${title}`;
21+
break;
22+
default:
23+
console.error('Unsupported network:', network);
24+
return;
25+
}
26+
27+
try {
28+
window.open(shareUrl, '_blank', 'width=600,height=400');
29+
} catch (error) {
30+
console.error('Error opening share URL:', error);
31+
console.error(error.stack);
32+
}
33+
});
34+
});
35+
});

views/article.ejs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@
4747
<a href="/articles/tag/<%= tag %>" class="badge bg-secondary text-decoration-none"><%= tag %></a>
4848
<% }) %>
4949
</div>
50+
<!-- Add this section for social sharing buttons -->
51+
<% if (article.status === 'published') { %>
52+
<div class="social-share mt-4">
53+
<h5>Share this article:</h5>
54+
<button class="btn btn-primary share-button" data-network="facebook">
55+
<i class="fab fa-facebook-f"></i> Facebook
56+
</button>
57+
<button class="btn btn-info share-button" data-network="twitter">
58+
<i class="fab fa-twitter"></i> Twitter
59+
</button>
60+
<button class="btn btn-secondary share-button" data-network="linkedin">
61+
<i class="fab fa-linkedin-in"></i> LinkedIn
62+
</button>
63+
</div>
64+
<% } %>
5065
</article>
5166
<section id="comments" class="mt-5">
5267
<h3>Comments</h3>
@@ -66,6 +81,8 @@
6681
</section>
6782
</main>
6883
<%- include('partials/_footer.ejs') %>
84+
<script src="https://kit.fontawesome.com/your-fontawesome-kit.js" crossorigin="anonymous"></script>
6985
<script src="/js/article.js"></script>
86+
<script src="/js/socialShare.js"></script>
7087
</body>
7188
</html>

views/partials/_head.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
<meta charset="UTF-8">
33
<title>PythaSpace</title>
44
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
5+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
56
<link rel="stylesheet" href="/css/style.css">
6-
</head>
7-
7+
</head>

0 commit comments

Comments
 (0)