11import React , { useEffect } from 'react' ;
2+ import { useLocation } from '@docusaurus/router' ;
23import useDocusaurusContext from '@docusaurus/useDocusaurusContext' ;
34
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 {
5+ export default function DiscourseComments ( ) {
136 const {
147 siteConfig : {
8+ url,
159 themeConfig : { discourseUrl } ,
1610 } ,
17- } = useDocusaurusContext < { discourseUrl : string } > ( ) ;
11+ } = useDocusaurusContext ( ) ;
12+ const { pathname } = useLocation ( ) ;
1813
19- useEffect ( ( ) => {
20- const win = window as DiscourseEmbedWindow ;
14+ // If discourseUrl is not defined, return null to skip rendering
15+ if ( ! discourseUrl ) {
16+ console . log ( 'DiscourseComments: No discourseUrl defined in themeConfig, skipping render' ) ;
17+ return null ;
18+ }
2119
22- if ( ! win . DiscourseEmbed ) {
23- const forum = discourseUrl . endsWith ( '/' ) ? discourseUrl : ` ${ discourseUrl } /` ;
24- const path = window . location . pathname ;
20+ if ( pathname == "/blog" || pathname == '/blog/' ) {
21+ return ;
22+ }
2523
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 } ` ;
24+ useEffect ( ( ) => {
3125
32- win . DiscourseEmbed = {
33- discourseUrl : forum ,
34- discourseEmbedUrl : `${ base } ${ path } ` ,
35- } ;
26+ const baseUrl = url . endsWith ( '/' ) ? url . slice ( - 1 , 1 ) : url ; // Ensure baseUrl does not end with a slash
27+ const forum = discourseUrl ;
28+ const embedUrl = baseUrl + window . location . pathname ;
3629
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- }
30+ console . log ( ' Discourse comments component mounted' ) ;
31+ console . log ( 'Base URL:' , baseUrl ) ;
32+ console . log ( 'Current pathname:' , pathname ) ;
33+ console . log ( 'Discourse URL:' , forum ) ;
34+ console . log ( 'Embed URL:' , embedUrl ) ;
4235
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- }
36+ // Update or set DiscourseEmbed
37+ window . DiscourseEmbed = {
38+ discourseUrl : forum ,
39+ discourseEmbedUrl : embedUrl ,
40+ } ;
4841
49- const script = document . createElement ( 'script' ) ;
50- script . src = forum + 'javascripts/embed.js' ;
51- 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' ) ;
55- document . body . appendChild ( script ) ;
42+ // Remove existing Discourse iframe if present
43+ const existingIframe = document . querySelector ( '#discourse-comments iframe' ) ;
44+ if ( existingIframe && existingIframe . parentNode ) {
45+ existingIframe . parentNode . removeChild ( existingIframe ) ;
46+ }
5647
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- } ;
48+ // Load or reload the embed script
49+ const existingScript = document . querySelector ( `script[src="${ forum } javascripts/embed.js"]` ) ;
50+ if ( existingScript ) {
51+ existingScript . parentNode . removeChild ( existingScript ) ;
6752 }
68- } , [ discourseUrl ] ) ;
6953
70- return < div id = "discourse-comments" style = { { marginTop : '2rem' } } /> ;
71- }
54+ const script = document . createElement ( 'script' ) ;
55+ script . src = forum + 'javascripts/embed.js' ;
56+ script . async = true ;
57+ script . onload = ( ) => console . log ( 'Discourse script loaded successfully' ) ;
58+ script . onerror = ( ) => console . error ( 'Failed to load Discourse script' ) ;
59+ document . body . appendChild ( script ) ;
60+
61+ return ( ) => {
62+ if ( script . parentNode ) {
63+ script . parentNode . removeChild ( script ) ;
64+ }
65+ // Clean up iframe on unmount
66+ const iframe = document . querySelector ( '#discourse-comments iframe' ) ;
67+ if ( iframe && iframe . parentNode ) {
68+ iframe . parentNode . removeChild ( iframe ) ;
69+ }
70+ } ;
71+ } , [ pathname , discourseUrl ] ) ; // Depend on pathname to re-run on route changes
72+
73+ return < div id = "discourse-comments" /> ;
74+ }
0 commit comments