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