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

Topic documentation commenting #170

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3c170b1
feat: init docusaurus-commenting-system plugin
JulesFiliot Dec 21, 2023
5bf3437
feat(commenting system plugin): add config files
JulesFiliot Dec 21, 2023
f085729
feat(commenting system plugin): add package.json
JulesFiliot Dec 21, 2023
54492c8
feat(commenting system plugin): add sass files handling
JulesFiliot Dec 22, 2023
e65f024
feat(commenting system plugin): add plugin index file
JulesFiliot Dec 22, 2023
e2237c5
feat(commenting system UI): add Comment component
JulesFiliot Dec 22, 2023
f15fca2
feat(commenting system UI): add WriteComment component
JulesFiliot Dec 22, 2023
6d66b3d
feat(commenting system UI): add CommentingSection container (commenti…
JulesFiliot Dec 22, 2023
72c1203
feat(commenting system UI): add client index file
JulesFiliot Dec 22, 2023
f413a0a
feat: add commenting system plugin to the docusaurus playground
JulesFiliot Dec 22, 2023
cfc5ac6
feat: add commenting system test code to the playground
JulesFiliot Dec 22, 2023
326fce5
style(CommentingSection): reduce top margin
JulesFiliot Dec 22, 2023
59bcf54
feat(commenting system): add input handling logic to WriteComment com…
JulesFiliot Dec 24, 2023
500fe88
feat(commenting system): add authentication functions
JulesFiliot Dec 24, 2023
bcc3205
fix(commenting system): ts server files not being built
JulesFiliot Dec 24, 2023
af05eef
feat(commenting system): add comments functions
JulesFiliot Dec 24, 2023
2020adc
feat(commenting system): add comments subscibre function
JulesFiliot Dec 24, 2023
351a502
feat(commenting system): add comments handling logic to CommentingSec…
JulesFiliot Dec 24, 2023
a2e503a
feat(commenting system): add user's Supabase credentials handling
JulesFiliot Dec 24, 2023
adda399
feat(commenting system): add Supabase credentials to docusaurus playg…
JulesFiliot Dec 24, 2023
4122ff6
fix(docusaurus playground): fix commenting system import
JulesFiliot Dec 24, 2023
4f9388a
feat(docusaurus playground): add new commenting system test
JulesFiliot Dec 24, 2023
fac62cb
feat(docusaurus playground): add theme swizzle to test commenting system
JulesFiliot Dec 24, 2023
530ea17
fix(commenting system): trailing slash bug when fetching comments
JulesFiliot Dec 24, 2023
0a442f7
feat(commenting system UI): small UI enhancement on WriteComment comp…
JulesFiliot Dec 24, 2023
cd5ecdf
feat(commenting system UI): block multi lines on WriteComment compone…
JulesFiliot Dec 24, 2023
da39df4
docs(commenting system): add plugin docs
JulesFiliot Dec 24, 2023
c511ba8
fix(commenting system): CommentingSection component UI displaying fal…
JulesFiliot Dec 25, 2023
46f90de
feat(commenting system): add date display to Comment component
JulesFiliot Dec 25, 2023
c2d0be5
feat(commenting system): add multi lines comment display handling
JulesFiliot Dec 25, 2023
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
117 changes: 117 additions & 0 deletions packages/docusaurus-commenting-system/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Docusaurus commenting system plugin

The Docusaurus Commenting System plugin allows to add a realtime commenting system to your Docusaurus project. It uses Supabase as a backend and GitHub as an authentication provider.

## Installation

### Supabase setup

1. Create a Supabase project.
2. Activate the GitHub authentication provider (Authentification > Providers).
3. In the SQL Editor, run the following queries:

- To create the `profiles` table:

```sql
create table
public.profiles (
id uuid not null,
user_name text null,
constraint profiles_pkey primary key (id),
constraint profiles_id_fkey foreign key (id) references auth.users (id) on delete cascade
) tablespace pg_default;
```

- To create the `comments` table:

```sql
create table
public.comments (
id bigint generated by default as identity,
created_at timestamp without time zone not null default now(),
text text not null,
page_path text not null,
author_id uuid not null default auth.uid (),
constraint comment_pkey primary key (id),
constraint comment_id_key unique (id),
constraint comments_author_id_fkey foreign key (author_id) references profiles (id)
) tablespace pg_default;
```

- To create a function and a trigger to automatically fill the `profiles` table when a new user authenticates with GitHub for the first time:

```sql
-- inserts a row into public.profiles
create function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = public
as $$
begin
insert into public.profiles (id, user_name)
values (new.id, new.raw_user_meta_data ->> 'user_name');
return new;
end;


$$;

-- trigger the function every time a user is created
create trigger on_auth_user_created
after insert on auth.users
for each row execute procedure public.handle_new_user();

```

4. Activate Realtime on the `comments` table.
5. Go to Database > Schema Visualizer and make sure that your database schema looks like this:
![Supabase database schema](https://iili.io/JRKk0v9.png)
6. Please consider adding RLS (Row Level Security) policies to your tables. You should allow only authenticated users to INSERT on the `comments` table and you can add public read access to the `profiles` and `comments` tables.

### Docusaurus setup

1. Add the plugin to your Docusaurus project: `yarn add @acid-info/docusaurus-commenting-system`.
2. Add the plugin to your `docusaurus.config.js` file:

```js
const config = {
// other config fields...,
plugins: [
// other plugins...,
[
'@acid-info/docusaurus-commenting-system',
{
supabaseUrl: process.env.SUPABASE_URL,
supabaseAnonKey: process.env.SUPABASE_ANON_KEY,
},
],
],
}
```

3. Create a `.env` file at the root of your project and add the following environment variables. They are your Supabase Project URL and Supabase Public API Key, never put a secret key here.

```
SUPABASE_URL=https://example.supabase.co
SUPABASE_ANON_KEY=example.anon.key
```

4. Add the `CommentingSystem` component to your pages. Here is an example in a mdx docs page:

```mdx
## // docs/example.mdx

## title: Example

import { CommentingSystem } from '@acid-info/docusaurus-commenting-system/lib/client/'

# Example doc page

This is an example doc page with the commenting system at the bottom of it.

# Comments section

<CommentingSystem />
```

`N.B.:` The commenting section is unique to each individual page URL. This means that the comments you see on one page are specific to that page only and are not shared or displayed on other pages. Each page has its own separate set of comments.
32 changes: 32 additions & 0 deletions packages/docusaurus-commenting-system/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@acid-info/docusaurus-commenting-system",
"version": "1.0.0-alpha",
"description": "Docusaurus docs commenting system plugin",
"main": "lib/index.js",
"repository": {
"type": "git",
"url": "https://github.com/acid-info/logos-docusaurus-plugins.git",
"directory": "packages/docusaurus-commenting-system"
},
"scripts": {
"build": "tsc --build && node scripts/copy-scss.js",
"watch": "tsc --build --watch",
"prepublishOnly": "yarn build"
},
"dependencies": {
"@acid-info/lsd-react": "^0.1.0-beta.1",
"@docusaurus/types": "^3.0.1",
"@supabase/supabase-js": "^2.39.1",
"date-fns": "^3.0.6",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"copyfiles": "^2.4.1",
"css-loader": "^6.8.1",
"sass": "^1.69.5",
"sass-loader": "^13.3.2",
"style-loader": "^3.3.3"
},
"license": "MIT"
}
37 changes: 37 additions & 0 deletions packages/docusaurus-commenting-system/scripts/copy-scss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require('fs')
const path = require('path')
const copyfiles = require('copyfiles')

function copySCSS(srcDir, destDir) {
fs.readdir(srcDir, { withFileTypes: true }, (err, entries) => {
if (err) {
console.error(err)
return
}

entries.forEach((entry) => {
const srcPath = path.join(srcDir, entry.name)
const destPath = path.join(destDir, entry.name)

if (entry.isDirectory()) {
// Recursive call for directories
copySCSS(srcPath, destPath)
} else if (path.extname(entry.name) === '.scss') {
// Copying .scss files
copyfiles([srcPath, path.dirname(destPath)], { up: true }, (err) => {
if (err) console.error(err)
})
}
})
})
}

const srcClientPath = path.join(__dirname, '../src/client')
const libClientPath = path.join(__dirname, '../lib/client')

// Ensure the destination directory exists
if (!fs.existsSync(libClientPath)) {
fs.mkdirSync(libClientPath, { recursive: true })
}

copySCSS(srcClientPath, libClientPath)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.commentHeader {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 0.3rem;

.username {
font-weight: bold;
}

.date {
font-weight: lighter;
}
}

.commentText {
white-space: pre-line;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react'
import { CardBody } from '@acid-info/lsd-react'
import styles from './Comment.module.scss'

export type CommentProps = {
username: string
comment: string
date: string
}

export const Comment: React.FC<CommentProps> = ({
username,
comment,
date,
}) => {
return (
<CardBody>
<div className={styles.commentHeader}>
<span className={styles.username}>{username}</span>
<span className={styles.date}>{date}</span>
</div>
<div className={styles.commentText}>{comment}</div>
</CardBody>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Comment'
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.textarea {
width: 100%;
min-width: 100%;
max-width: 100%;

&:disabled {
cursor: not-allowed;
}
}

.actionContainer {
display: flex;
justify-content: flex-end;
margin-top: 0.5rem;
}

.githubBtnLabel {
display: flex;
column-gap: 5px;
align-items: center;
justify-content: center;

&::before {
content: '';
width: 16px;
height: 16px;
display: flex;
background: url("data:image/svg+xml,%3Csvg viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12'/%3E%3C/svg%3E")
no-repeat;
}

html[data-theme='dark'] &::before {
background: url("data:image/svg+xml,%3Csvg viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill='white' d='M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12'/%3E%3C/svg%3E")
no-repeat;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { useState } from 'react'
import { CardBody, Button } from '@acid-info/lsd-react'
import styles from './WriteComment.module.scss'

export type WriteCommentProps = {
handleSend: (comment: string) => void
handleSignIn: () => void
isSignedIn?: boolean
}

export const WriteComment: React.FC<WriteCommentProps> = ({
handleSend,
handleSignIn,
isSignedIn = false,
}) => {
const [comment, setComment] = useState('')

const onSendClick = () => {
const commentToSend = comment.trim()
if (commentToSend) {
handleSend(commentToSend)
setComment('')
}
}

return (
<CardBody>
<textarea
className={styles.textarea}
placeholder={isSignedIn ? 'Write a comment...' : 'Sign in to comment'}
disabled={!isSignedIn}
rows={4}
value={comment}
onChange={(e) => setComment(e.target.value)}
/>
<div className={styles.actionContainer}>
<Button
size="small"
onClick={isSignedIn ? onSendClick : handleSignIn}
disabled={isSignedIn && comment.length === 0}
>
<span className={!isSignedIn && styles.githubBtnLabel}>
{isSignedIn ? 'Send' : 'Sign In with GitHub'}
</span>
</Button>
</div>
</CardBody>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './WriteComment'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.commentsContainer {
display: flex;
flex-direction: column;
row-gap: 1rem;
margin-top: 2rem;
}
Loading