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

Docusaurus Integration Page #267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions public/doc/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [Vue](https://github.com/2nthony/vue-cusdis)
- [Docsify](/integration/docsify.md)
- [Jekyll](/integration/jekyll.md)
- [Docusaurus](/integration/docusaurus.md)
- [Hugo](https://discourse.gohugo.io/t/free-and-open-source-comments-for-hugo/32940)
- [Hexo](https://blog.cusdis.com/integate-cusdis-in-hexo)
- [Mkdocs](/integration/mkdocs.md)
Expand Down
122 changes: 122 additions & 0 deletions public/doc/integration/docusaurus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# Integrate Cusdis into Docusaurus

[Docusaurus](https://github.com/facebook/docusaurus) is a user-friendly, open-source static site generator developed by Meta. It harnesses the full potential of **React** and **MDX**, making it easy to start, customizable, and suitable for localization.

## Usage

This guide will walk you through the steps to add Cusdis comments to your Docusaurus website pages.

### Installing Dependencies

Before integrating Cusdis, ensure that you have installed the required dependencies. You will need `react-cusdis`, which is a **React wrapper for Cusdis**. Install it as a dependency in your Docusaurus project:

```bash
npm install react-cusdis
# or
yarn add react-cusdis

```

### Create a Cusdis Component

Next, create a new component for Cusdis in your Docusaurus project. You can name it `CusdisComments.js` and place it in the `src/components folder`. Here's an example of what the component might look like:

```js
import { ReactCusdis } from 'react-cusdis';

export default function CusdisComments(props) {
const appId = process.env.CUSDIS_APP_ID;

return (
<div>
<ReactCusdis
id="cusdis-thread"
attrs={{
host: "https://cusdis.com",
appId: appId,
pageId: props.attrs.pageId,
pageTitle: props.attrs.pageTitle,
pageUrl: props.attrs.pageUrl,
theme: "auto",
}}
/>
</div>
);
}

```

### Import and add the Cusdis Component

Once you've created the `CusdisComments.js` component, import it into the documentation page where you want to add the Cusdis comment system. Import the component at the top of your page file, like this:

```js
import CusdisComments from '../src/components/CusdisComments';
```

In the same documentation page, add the following code where you want to display the comment system:

```js
<CusdisComments
id="cusdis_thread"
attrs={{
pageId: frontMatter.id,
Copy link

@xijinping0 xijinping0 May 19, 2024

Choose a reason for hiding this comment

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

It's probably helpful to mention that the frontMatter here comes from props.content, and the props is from the pager's wrapper (e.g. DocItemWrapper):

export default function DocItemWrapper(props) {
  const { frontMatter } = props.content;
  // ...
}

Copy link

Choose a reason for hiding this comment

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

观摩一下,哈哈。

pageTitle: frontMatter.title,
pageUrl: frontMatter.__resourcePath,
}}
/>
```

This code snippet will render the Cusdis comment system on your Docusaurus page.

### Set Up Environment Variables (optional)

To securely store your Cusdis App ID, follow these steps to set up environment variables in your Docusaurus project:

#### Step 1: Install the `@docusaurus/plugin-client-redirects` Package

Install the `@docusaurus/plugin-client-redirects` package using npm or yarn:

```bash
npm install @docusaurus/plugin-client-redirects
# or
yarn add @docusaurus/plugin-client-redirects
```

#### Step 2: Configure Environment Variables

In your `docusaurus.config.js` file, add the following configuration under the `plugins` section to enable environment variables:

```js
plugins: [
[
"docusaurus2-dotenv",
{
systemvars: true,
},
],
],
```

#### Step 3: Create an .env File

Create a `.env` file in the root of your Docusaurus project and add your **Cusdis App ID** to it. For example:

```env
CUSDIS_APP_ID=your-app-id
```

#### Step 4: Access Environment Variables

You can now access your environment variable in the `CusdisComments` component as follows:

```js
import { ReactCusdis } from 'react-cusdis';

export default function CusdisComments(props) {
const appId = process.env.CUSDIS_APP_ID;

// ... rest of the component code
}

```