Adding Giscus to Docusaurus
· 2 min read
I tried following the instructions from Alex Fornuto - Adding Comments to Docusaurus Sites but ran into some issues (including applying feedback from the comments).
Here is what I had to do to get it working for my (v3.6) Docusaurus:
- install @giscus/react:
npm -i @giscus/react - follow instructions from http://giscus.app to configure giscus for your repository.
- Add GiscusComponent code
src/components/GiscusComponent/index.tsximport React from 'react';import Giscus from "@giscus/react";import { useColorMode } from '@docusaurus/theme-common';export default function GiscusComponent() {const { colorMode } = useColorMode();return (<Giscusrepo="repo"repoId="id"category="Announcements"categoryId="id"mapping="pathname"strict="0"reactionsEnabled="1"emitMetadata="0"inputPosition="top"theme={colorMode}lang={currentLocale}loading="lazy"/>);}
- Add BlogPostItem code
src/theme/BlogPostItem/index.tsximport React 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';type Props = WrapperProps<typeof BlogPostItemType>;export default function BlogPostItemWrapper(props: Props): JSX.Element {const { isBlogPostPage } = useBlogPost();return (<><BlogPostItem {...props} />{isBlogPostPage && (<div ><hr /><GiscusComponent /></div>)}</>);}
- Done
The issues I ran into were related to:
- the
useBlogPosthook was moved from@docusaurus/theme-common/internalto@docusaurus/plugin-content-blog/clientin docusaurus v3.5. - the initial post used "eject-swizzling" on the "BlogPostPage", but in the comments an easier method was described using "wrap-swizzling" on the "BlogPostItem".
