Skip to content

Commit

Permalink
Client-side Mermaid (#10394)
Browse files Browse the repository at this point in the history
* client mermaid

* lil loader

* make it work
  • Loading branch information
smallbrownbike authored Jan 28, 2025
1 parent d0d14b6 commit 6666d36
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 225 deletions.
2 changes: 1 addition & 1 deletion contents/docs/how-posthog-works/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ flowchart TD
Events(Events Service)
App(Web Service)
na %% Invisible helper node
na
end
Expand Down
2 changes: 1 addition & 1 deletion gatsby-browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Provider as ToastProvider } from './src/context/toast'
import { RouteUpdateArgs } from 'gatsby'
import { UserProvider } from './src/hooks/useUser'
import { ChatProvider } from './src/hooks/useChat'
import Chat from './src/components/Chat'

initKea(false)

export const wrapRootElement = ({ element }) => (
Expand Down
3 changes: 0 additions & 3 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ module.exports = {
extensions: ['.mdx', '.md'],
gatsbyRemarkPlugins: [
{ resolve: 'gatsby-remark-autolink-headers', options: { icon: false } },
{
resolve: require.resolve(`./plugins/gatsby-remark-mermaid`),
},
{
resolve: require.resolve('./plugins/gatsby-remark-video'),
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"markdown-link-extractor": "^4.0.1",
"markdown-to-jsx": "^7.4.0",
"md5": "^2.3.0",
"mermaid": "^9.1.5",
"mermaid": "^11.4.1",
"mobx": "^6.3.13",
"multiparty": "^4.2.3",
"node-fetch": "^2.6.1",
Expand Down
77 changes: 0 additions & 77 deletions plugins/gatsby-remark-mermaid/index.js

This file was deleted.

34 changes: 0 additions & 34 deletions plugins/gatsby-remark-mermaid/package.json

This file was deleted.

9 changes: 0 additions & 9 deletions plugins/gatsby-remark-mermaid/render.html

This file was deleted.

4 changes: 4 additions & 0 deletions src/components/CodeBlock/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import languageMap from './languages'
import { useValues } from 'kea'
import { layoutLogic } from 'logic/layoutLogic'
import Tooltip from 'components/Tooltip'
import Mermaid from 'components/Mermaid'

type LanguageOption = {
label?: string
Expand Down Expand Up @@ -74,6 +75,9 @@ type MdxCodeBlockChildren = {
}

export const MdxCodeBlock = ({ children, ...props }: MdxCodeBlock) => {
if (children?.props?.className?.includes('language-mermaid')) {
return <Mermaid>{children.props.children}</Mermaid>
}
const childArray = Array.isArray(children) ? children : [children]

const languages = childArray
Expand Down
29 changes: 29 additions & 0 deletions src/components/Mermaid/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { useEffect, useRef, useState } from 'react'
import mermaid from 'mermaid'

export default function Mermaid({ children }: { children: string }): JSX.Element {
const [loading, setLoading] = useState(true)
const mermaidRef = useRef<HTMLDivElement>(null)
useEffect(() => {
mermaid.initialize({ startOnLoad: false })
if (mermaidRef.current && !mermaidRef.current.hasAttribute('data-rendered')) {
mermaid.run({
nodes: [mermaidRef.current],
postRenderCallback: () => {
setLoading(false)
mermaidRef.current?.setAttribute('data-rendered', 'true')
},
})
}
}, [])
return (
<div className="relative">
{loading && (
<div className="bg-accent dark:bg-accent-dark flex items-center justify-center size-full rounded-md animate-pulse absolute inset-0" />
)}
<div ref={mermaidRef} className={loading ? 'invisible' : ''}>
{children}
</div>
</div>
)
}
Loading

0 comments on commit 6666d36

Please sign in to comment.