Skip to content

Commit fa28c0f

Browse files
RaduNichitamguludag
andcommitted
Enhance support for discourse comments
Co-authored-by: Muhammed Guludag <mguludag@outlook.com> Signed-off-by: Radu Nichita <radunichita99@gmail.com>
1 parent e837736 commit fa28c0f

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

blog/2024-10-30-about-beman/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
slug: about-beman
33
authors: [dabrahams]
44
tags: [beman]
5+
comments: true
56
---
67

78
# About Beman

blog/2025-01-13-beman-intro/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
slug: beman-tutorial
33
authors: [JeffGarland]
44
tags: [cppcon, conference]
5+
comments: true
56
---
67

78
# Beman Project Introduction Online

blog/2025-01-29-nyc-hackathon/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
slug: nyc-hackathon
33
authors: ["camio"]
44
tags: ["hackathon", "beman"]
5+
comments: true
56
---
67

78
# NYC Hackathon

src/components/DiscourseComments/index.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,36 @@ export default function DiscourseComments(): JSX.Element {
3434
discourseEmbedUrl: `${base}${path}`,
3535
};
3636

37+
// Remove existing Discourse iframe if present
38+
const existingIframe = document.querySelector('#discourse-comments iframe');
39+
if (existingIframe && existingIframe.parentNode) {
40+
existingIframe.parentNode.removeChild(existingIframe);
41+
}
42+
43+
// Load or reload the embed script
44+
const existingScript = document.querySelector(`script[src="${forum}javascripts/embed.js"]`);
45+
if (existingScript) {
46+
existingScript.parentNode.removeChild(existingScript);
47+
}
48+
3749
const script = document.createElement('script');
38-
script.src = `${forum}javascripts/embed.js`;
50+
script.src = forum + 'javascripts/embed.js';
3951
script.async = true;
52+
script.referrerPolicy = 'no-referrer-when-downgrade';
53+
script.onload = () => console.log('Discourse script loaded successfully');
54+
script.onerror = () => console.error('Failed to load Discourse script');
4055
document.body.appendChild(script);
56+
57+
return () => {
58+
if (script.parentNode) {
59+
script.parentNode.removeChild(script);
60+
}
61+
// Clean up iframe on unmount
62+
const iframe = document.querySelector('#discourse-comments iframe');
63+
if (iframe && iframe.parentNode) {
64+
iframe.parentNode.removeChild(iframe);
65+
}
66+
};
4167
}
4268
}, [discourseUrl]);
4369

src/theme/BlogPostItem/index.tsx

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
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';
1+
import React from "react";
2+
import { useLocation } from "@docusaurus/router";
3+
import BlogPostItem from "@theme-original/BlogPostItem";
4+
import DiscourseComments from "@site/src/components/DiscourseComments";
55

6-
// 1) Import your DiscourseComments
7-
import DiscourseComments from '@site/src/components/DiscourseComments';
6+
export default function BlogPostItemWrapper(props) {
7+
const { pathname } = useLocation();
8+
// Safely access frontMatter from props.children.type
9+
const frontMatter = props.children?.type?.frontMatter || {};
810

9-
type Props = WrapperProps<typeof BlogPostItemType>;
11+
// Only render comments for individual blog posts (not list or archive pages)
12+
const isBlogListPage =
13+
pathname === "/blog" ||
14+
pathname.startsWith("/blog/archive") ||
15+
pathname.startsWith("/blog/tags");
16+
const commentsEnabled =
17+
frontMatter.comments === true || frontMatter.comments === "true";
18+
const shouldShowComments =
19+
commentsEnabled && !isBlogListPage && frontMatter.slug;
1020

11-
export default function BlogPostItemWrapper(props: Props): ReactNode {
1221
return (
1322
<>
14-
{/* 2) Render the original blog content */}
1523
<BlogPostItem {...props} />
16-
17-
{/* 3) Then render Discourse comments */}
18-
<DiscourseComments />
24+
{shouldShowComments && (
25+
<DiscourseComments key={frontMatter.slug || "discourse"} />
26+
)}
1927
</>
2028
);
21-
}
29+
}

0 commit comments

Comments
 (0)