Skip to content

Commit a774719

Browse files
committed
use client component
1 parent 9db1a44 commit a774719

File tree

4 files changed

+908
-33
lines changed

4 files changed

+908
-33
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"js-yaml": "^4.1.0",
7272
"match-sorter": "^6.3.4",
7373
"mdx-bundler": "^10.0.1",
74+
"mermaid": "^11.4.0",
7475
"micromark": "^4.0.0",
7576
"next": "14.2.4",
7677
"next-mdx-remote": "^4.4.1",

src/components/docPage/index.tsx

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {ReactNode} from 'react';
2-
import Script from 'next/script';
32

43
import {getCurrentGuide, getCurrentPlatform, nodeForPath} from 'sentry-docs/docTree';
54
import {serverContext} from 'sentry-docs/serverContext';
@@ -14,6 +13,7 @@ import {Breadcrumbs} from '../breadcrumbs';
1413
import {CodeContextProvider} from '../codeContext';
1514
import {GitHubCTA} from '../githubCTA';
1615
import {Header} from '../header';
16+
import Mermaid from '../mermaid';
1717
import {PaginationNav} from '../paginationNav';
1818
import {PlatformSdkDetail} from '../platformSdkDetail';
1919
import {Sidebar} from '../sidebar';
@@ -55,9 +55,6 @@ export function DocPage({
5555

5656
const leafNode = nodeForPath(rootNode, unversionedPath);
5757

58-
// a hack to show syntax highlighting on editors
59-
const javascript = String.raw;
60-
6158
return (
6259
<div className="tw-app">
6360
<Header pathname={pathname} searchPlatforms={searchPlatforms} />
@@ -112,34 +109,7 @@ export function DocPage({
112109
)}
113110
</main>
114111
</section>
115-
{/* can't use useEffect here (server component) */}
116-
<Script
117-
type="module"
118-
id="mermaid-script"
119-
dangerouslySetInnerHTML={{
120-
__html: javascript`
121-
const mermaidBlocks = document.querySelectorAll('.language-mermaid');
122-
if (mermaidBlocks.length >= 0) {
123-
const {default: mermaid} = await import(
124-
'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs'
125-
);
126-
mermaid.initialize({startOnLoad: false});
127-
mermaidBlocks.forEach(block => {
128-
// get rid of code highlighting
129-
const code = block.textContent;
130-
block.innerHTML = code;
131-
// force transparent background
132-
block.style.backgroundColor = 'transparent';
133-
const parentCodeTabs = block.closest('.code-tabs-wrapper')
134-
if(parentCodeTabs) {
135-
parentCodeTabs.innerHTML = block.outerHTML;
136-
}
137-
});
138-
await mermaid.run({nodes: document.querySelectorAll('.language-mermaid')});
139-
}
140-
`,
141-
}}
142-
/>
112+
<Mermaid />
143113
</div>
144114
);
145115
}

src/components/mermaid.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use client';
2+
import {useEffect} from 'react';
3+
4+
export default function Mermaid() {
5+
useEffect(() => {
6+
(async function () {
7+
const mermaidBlocks =
8+
document.querySelectorAll<HTMLDivElement>('.language-mermaid');
9+
if (mermaidBlocks.length === 0) {
10+
return;
11+
}
12+
const {default: mermaid} = await import('mermaid');
13+
mermaid.initialize({startOnLoad: false});
14+
mermaidBlocks.forEach(block => {
15+
// get rid of code highlighting
16+
const code = block.textContent ?? '';
17+
block.innerHTML = code;
18+
// force transparent background
19+
block.style.backgroundColor = 'transparent';
20+
const parentCodeTabs = block.closest('.code-tabs-wrapper');
21+
if (parentCodeTabs) {
22+
parentCodeTabs.innerHTML = block.outerHTML;
23+
}
24+
});
25+
await mermaid.run({nodes: document.querySelectorAll('.language-mermaid')});
26+
})();
27+
}, []);
28+
return null;
29+
}

0 commit comments

Comments
 (0)