Skip to content

Commit e837736

Browse files
Integrate Discourse comment feature for blog posts (#98)
* Add initial prototype for comments Signed-off-by: Radu Nichita <[email protected]> * Re-deploy - will be reverted --------- Signed-off-by: Radu Nichita <[email protected]> Co-authored-by: Darius Neațu <[email protected]>
1 parent f635980 commit e837736

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

docusaurus.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ const config: Config = {
123123
theme: prismThemes.github,
124124
darkTheme: prismThemes.dracula,
125125
},
126+
discourseUrl: 'https://discourse.bemanproject.org/', // Temporary change, re-deploy.
126127
} satisfies Preset.ThemeConfig,
127128
};
128129

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@docusaurus/preset-classic": "3.7.0",
2020
"@mdx-js/react": "^3.0.0",
2121
"@remark-embedder/core": "^3.0.3",
22+
"@remark-embedder/transformer-oembed": "^5.0.1",
2223
"clsx": "^2.0.0",
2324
"prism-react-renderer": "^2.3.0",
2425
"react": "^19.0.0",
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React, { useEffect } from 'react';
2+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
3+
4+
type DiscourseEmbedWindow = Window & {
5+
DiscourseEmbed?: {
6+
discourseUrl: string;
7+
discourseEmbedUrl: string;
8+
discourseTopicTitle?: string;
9+
};
10+
};
11+
12+
export default function DiscourseComments(): JSX.Element {
13+
const {
14+
siteConfig: {
15+
themeConfig: { discourseUrl },
16+
},
17+
} = useDocusaurusContext<{ discourseUrl: string }>();
18+
19+
useEffect(() => {
20+
const win = window as DiscourseEmbedWindow;
21+
22+
if (!win.DiscourseEmbed) {
23+
const forum = discourseUrl.endsWith('/') ? discourseUrl : `${discourseUrl}/`;
24+
const path = window.location.pathname;
25+
26+
// Use production URL only if not localhost
27+
const isLocal = window.location.hostname === 'localhost';
28+
const base = isLocal
29+
? 'https://bemanproject.org'
30+
: `${window.location.origin}`;
31+
32+
win.DiscourseEmbed = {
33+
discourseUrl: forum,
34+
discourseEmbedUrl: `${base}${path}`,
35+
};
36+
37+
const script = document.createElement('script');
38+
script.src = `${forum}javascripts/embed.js`;
39+
script.async = true;
40+
document.body.appendChild(script);
41+
}
42+
}, [discourseUrl]);
43+
44+
return <div id="discourse-comments" style={{ marginTop: '2rem' }} />;
45+
}

src/theme/BlogPostItem/index.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React, { type ReactNode } from 'react';
2+
import BlogPostItem from '@theme-original/BlogPostItem';
3+
import type BlogPostItemType from '@theme/BlogPostItem';
4+
import type { WrapperProps } from '@docusaurus/types';
5+
6+
// 1) Import your DiscourseComments
7+
import DiscourseComments from '@site/src/components/DiscourseComments';
8+
9+
type Props = WrapperProps<typeof BlogPostItemType>;
10+
11+
export default function BlogPostItemWrapper(props: Props): ReactNode {
12+
return (
13+
<>
14+
{/* 2) Render the original blog content */}
15+
<BlogPostItem {...props} />
16+
17+
{/* 3) Then render Discourse comments */}
18+
<DiscourseComments />
19+
</>
20+
);
21+
}

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,13 @@
20332033
unified "^11.0.4"
20342034
unist-util-visit "^5.0.0"
20352035

2036+
"@remark-embedder/transformer-oembed@^5.0.1":
2037+
version "5.0.1"
2038+
resolved "https://registry.yarnpkg.com/@remark-embedder/transformer-oembed/-/transformer-oembed-5.0.1.tgz#7734d3ecb1ea101447832f6350c5ebf0b249380c"
2039+
integrity sha512-NKR9WYyzI74iZ1nWVXcKQ9dz1xIBuN3nONqVsRbyp9yVZJv8tCi25BSVmgVQtqaNsEjbSemEp8yp+hwQzuLVxg==
2040+
dependencies:
2041+
"@babel/runtime" "^7.24.5"
2042+
20362043
"@sideway/address@^4.1.5":
20372044
version "4.1.5"
20382045
resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.5.tgz#4bc149a0076623ced99ca8208ba780d65a99b9d5"

0 commit comments

Comments
 (0)