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

Enable comments and reactions on blog posts #41

Closed
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
3 changes: 2 additions & 1 deletion docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const config = {
description: 'An open-source framework for building practical distributed replication mechanisms.',
gitHubUrl: gitHubUrl,
siteLicense: siteLicense,
recentBlogPostsOnHomePage: 5
recentBlogPostsOnHomePage: 5,
disqusShortname: 'https-replica-io-dev'
},

// GitHub pages deployment config.
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@mdx-js/react": "^3.0.1",
"botex": "^1.0.0",
"clsx": "^2.1.1",
"disqus-react": "^1.1.5",
"prism-react-renderer": "^2.3.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
42 changes: 42 additions & 0 deletions src/theme/BlogPostItem/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { useEffect, useState } from 'react';
import { DiscussionEmbed } from 'disqus-react';
import BlogPostItem from '@theme-original/BlogPostItem';
import { useBlogPost } from '@docusaurus/plugin-content-blog/client';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import { useColorMode } from '@docusaurus/theme-common';

export default function BlogPostItemWrapper(props) {
const { metadata } = useBlogPost();
const { frontMatter, permalink, title } = metadata;
const currentUrlOrigin = typeof window !== 'undefined' ? window.location.origin : '';
const fullPermalink = `${currentUrlOrigin}${permalink}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should use ${siteConfig.url} instead of ${currentUrlOrigin} here (see this).

const { comments = true } = frontMatter;
const { siteConfig, i18n } = useDocusaurusContext();
const shortname = siteConfig.customFields.disqusShortname;
const { colorMode } = useColorMode();
const [disqusColorMode, setDisqusColorMode] = useState(colorMode);

useEffect(() => {
// Disqus doesn't auto-update when the site's theme changes,
// so we re-render the DiscussionEmbed to reflect the new theme.
setDisqusColorMode(colorMode);
}, [colorMode]);

return (
<>
<BlogPostItem {...props} />
{comments && (
<DiscussionEmbed
// This property is not used by `DiscussionEmbed`, but forces React to re-render it upon theme changes
colorMode={disqusColorMode}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

React complains about this property's name:

React does not recognize the `colorMode` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `colormode` instead.

Let's make it happy by using colormode instead.

shortname={shortname}
config={{
url: fullPermalink,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I thought metadata.permalink is the full URL. Now I see it doesn't include the base URL. Then I think we can use it as the discussion identifier and add identifier: permalink.

title,
language: i18n.currentLocale,
}}
/>
)}
</>
);
}
2 changes: 1 addition & 1 deletion src/theme/Footer/Copyright/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default function CopyrightWrapper(props) {
const { siteConfig: { customFields: {
siteLicense,
} } } = useDocusaurusContext();

anchalshivank marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now there is a trailing whitespace...

return (
<>
<div>Made with ❤️ on Earth.</div>
Expand Down