Commenting Enabled with Giscus
I've enabled comments on the blog using giscus! Super easy to set up. I stumbled across this post showing how to add giscus to a Docusaurus site.
๐๐๐ Installation Notes Below ๐๐๐
I was hoping to add a comment section to engage with any readers, and now I have. I've used Disqus as a user in the past, but never added it as a widget. Giscus is a great alternative.
The instructions at the aforementioned post got me 90% of the way there.
I ran into an issue since I'm using Docusaurus 3.x and the post was written against 2.x which meant I needed to swap
out import BlogPostItem from '@theme-original/BlogPostItem';
with import BlogPostItem from '@docusaurus/plugin-content-blog/client';
and modify the BlogPostItemWrapper
function slightly. Here are the steps I followed.
- npm
- Yarn
npm i @giscus/react
npm run swizzle @docusaurus/theme-classic BlogPostItem -- --wrap
yarn add @giscus/react
yarn swizzle @docusaurus/theme-classic BlogPostItem --wrap
>yarn add @giscus/react
yarn add v1.22.22
[1/5] ๐ Validating package.json...
[2/5] ๐ Resolving packages...
[3/5] ๐ Fetching packages...
[4/5] ๐ Linking dependencies...
[5/5] ๐จ Building fresh packages...
success Saved lockfile.
success Saved 4 new dependencies.
info Direct dependencies
โโ @giscus/react@3.1.0
info All dependencies
โโ @giscus/react@3.1.0
โโ giscus@1.6.0
โโ lit-element@4.2.0
โโ lit@3.3.0
โจ Done in 4.65s.
>yarn swizzle @docusaurus/theme-classic BlogPostItem --wrap
yarn run v1.22.22
$ docusaurus swizzle @docusaurus/theme-classic BlogPostItem --wrap
โ Which language do you want to use? โบ TypeScript
[WARNING]
Swizzle action wrap is unsafe to perform on BlogPostItem.
It is more likely to be affected by breaking changes in the future
If you want to swizzle it, use the `--danger` flag, or confirm that you understand the risks.
โ Do you really want to swizzle this unsafe internal component? โบ YES: I know what I am doing!
[SUCCESS]
Created wrapper of BlogPostItem from @docusaurus/theme-classic in
- "/markfalk.github.io/src/theme/BlogPostItem/index.tsx"
I created a new GitHub repository to hold the comments and enabled discussions as described in the original post.
Using the GraphQL API Explorer with the following query:
query {
repository(owner: "nameOfYourGitHubAccount", name:"nameOfCreatedRepository"){
id
discussionCategories(first:10) {
edges {
node {
id
name
}
}
}
}
}
I determined the proper values for repoId
and categoryId
.
I created the component file /src/components/GiscusComponent.tsx
(I use TypeScript)
import React from 'react';
import Giscus from "@giscus/react";
import { useColorMode } from '@docusaurus/theme-common';
export default function GiscusComponent() {
const { colorMode } = useColorMode();
return (
<Giscus
repo="markfalk/giscus-data"
repoId="R_kgDOOyyeKA" // Replace - from GraphQL API Explorer
category="General"
categoryId="DIC_kwDOOyyeKM4CqwgG" // Replace - from GraphQL API Explorer / E.g. id of "General"
mapping="url"
term="Welcome to @giscus/react component!"
strict="0"
reactionsEnabled="1"
emitMetadata="1"
inputPosition="top"
theme={colorMode}
lang="en"
loading="lazy"
/>
);
}
I modified src/theme/BlogPostItem/index.tsx
as instructed. I also wanted comments on by default on all blog posts, they can be disabled by adding enableComments: false
to the blog frontmatter.
import React, {type ReactNode} from 'react';
import BlogPostItem from '@theme-original/BlogPostItem';
import type BlogPostItemType from '@theme/BlogPostItem';
import type {WrapperProps} from '@docusaurus/types';
import { useBlogPost } from '@docusaurus/plugin-content-blog/client'
import GiscusComponent from '@site/src/components/GiscusComponent';
import useIsBrowser from '@docusaurus/useIsBrowser';
type Props = WrapperProps<typeof BlogPostItemType>;
/**
* Wraps the original BlogPostItem with an additional Giscus comment component when
* the blog post is viewed as a single page and comments are enabled for the post.
* @param props The original props passed to BlogPostItem
* @returns The wrapped BlogPostItem component
*/
export default function BlogPostItemWrapper(props: Props): ReactNode {
const { metadata, isBlogPostPage } = useBlogPost()
const isBrowser = useIsBrowser();
const { frontMatter } = metadata
const { enableComments = true } = frontMatter
return (
<>
<BlogPostItem {...props} />
{(enableComments && isBlogPostPage) && (
<GiscusComponent />
)}
</>
);
}
---
slug: commenting-enabled-with-giscus
title: Commenting Enabled with Giscus
description: "Commenting Enabled with Giscus"
authors: [mark]
tags: [giscus, docusaurus]
enableComments: false
---
Feel free to leave a comment! ๐