Skip to content

Commit b37c4f7

Browse files
authored
blog: add social share (#355)
1 parent 9329875 commit b37c4f7

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"react": "^18.2.0",
3232
"react-dom": "^18.2.0",
3333
"react-icons": "^5.0.1",
34+
"react-share": "^5.1.0",
3435
"react-simple-typewriter": "^5.0.1",
3536
"tailwindcss": "^3.2.4"
3637
},

pnpm-lock.yaml

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/theme/BlogPostPage/icons.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import clsx from 'clsx';
2+
import React from 'react';
3+
4+
export const Twitter = (props: React.SVGProps<SVGSVGElement>): JSX.Element => (
5+
<svg
6+
xmlns="http://www.w3.org/2000/svg"
7+
width={36}
8+
height={36}
9+
fill="none"
10+
{...props}
11+
viewBox="0 0 24 24"
12+
className={clsx('text-black dark:text-white', props.className)}
13+
>
14+
<title>Twitter</title>
15+
<rect
16+
width={24}
17+
height={24}
18+
fill="currentColor"
19+
rx={12}
20+
className={clsx('text-gray-200 dark:text-black', props.className)}
21+
/>
22+
<path
23+
fill="currentColor"
24+
fillRule="evenodd"
25+
d="m6 6 4.62 6.75L6.098 18H7.1l3.963-4.604L14.214 18H18l-4.827-7.052L17.433 6h-1l-3.703 4.3L9.786 6H6Zm1.196.632h2.258l7.35 10.736h-2.258L7.196 6.632Z"
26+
clipRule="evenodd"
27+
/>
28+
</svg>
29+
);

src/theme/BlogPostPage/index.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import TOC from '@theme/TOC';
1010
import Unlisted from '@theme/Unlisted';
1111
import GiscusComponent from '@site/src/components/GiscusComponent';
1212
import { PostPaginator } from '@site/src/components/blog/post-paginator';
13+
import { LinkedinShareButton, RedditShareButton, TwitterShareButton, RedditIcon, LinkedinIcon } from 'react-share';
14+
import { Twitter } from './icons';
15+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
1316

1417
function getMultipleRandomPosts(relatedPosts, number) {
1518
// Create a copy of the original array to avoid modifying it
@@ -53,9 +56,8 @@ function getMultipleRandomPosts(relatedPosts, number) {
5356

5457
function BlogPostPageContent({ children }) {
5558
const { metadata, toc } = useBlogPost();
56-
const { relatedPosts } = metadata;
5759

58-
const { nextItem, prevItem, frontMatter, unlisted } = metadata;
60+
const { relatedPosts, nextItem, prevItem, frontMatter, unlisted, permalink, title, description } = metadata;
5961
const {
6062
hide_table_of_contents: hideTableOfContents,
6163
toc_min_heading_level: tocMinHeadingLevel,
@@ -65,6 +67,9 @@ function BlogPostPageContent({ children }) {
6567
const randomThreeRelatedPosts = getMultipleRandomPosts(relatedPosts, 3);
6668

6769
console.log('relatedPosts', relatedPosts);
70+
const {
71+
siteConfig: { url },
72+
} = useDocusaurusContext();
6873

6974
return (
7075
<BlogLayout
@@ -77,6 +82,36 @@ function BlogPostPageContent({ children }) {
7782
{unlisted && <Unlisted />}
7883

7984
<BlogPostItem>{children}</BlogPostItem>
85+
<div className="flex items-center gap-3 pt-10 not-prose">
86+
<span className="text-base">Share on</span>
87+
<TwitterShareButton
88+
windowWidth={750}
89+
windowHeight={800}
90+
url={url + permalink}
91+
className="flex"
92+
title={title}
93+
>
94+
<Twitter width={36} height={36} />
95+
</TwitterShareButton>
96+
<RedditShareButton
97+
className="flex"
98+
windowWidth={750}
99+
windowHeight={600}
100+
url={url + permalink}
101+
title={title}
102+
>
103+
<RedditIcon size={36} round />
104+
</RedditShareButton>
105+
<LinkedinShareButton
106+
url={url + permalink}
107+
title={title}
108+
source={url}
109+
summary={description}
110+
className="flex"
111+
>
112+
<LinkedinIcon size={36} round />
113+
</LinkedinShareButton>
114+
</div>
80115
<PostPaginator title="Related Articles" posts={randomThreeRelatedPosts}></PostPaginator>
81116
<GiscusComponent />
82117
{(nextItem || prevItem) && <BlogPostPaginator nextItem={nextItem} prevItem={prevItem} />}

0 commit comments

Comments
 (0)