'use client'; import type { ContentKitMarkdown } from '@gitbook/api'; import React from 'react'; import { useContentKitClientContext } from './context'; import { resolveDynamicBinding } from './dynamic'; import type { ContentKitClientElementProps } from './types'; /** * Client component to render the default markdown output and then update it on state update. */ export function ElementMarkdownClient( props: ContentKitClientElementProps & { initialMarkdown: string | undefined; renderMarkdown: (markdown: string) => React.ReactNode | Promise; children: React.ReactNode; } ) { const { element, initialMarkdown = element.content, renderMarkdown, children: initialChildren, } = props; const [children, setChildren] = React.useState(null); const context = useContentKitClientContext(); const markdown = resolveDynamicBinding(context.state, element.content); React.useEffect(() => { if (initialMarkdown === markdown) { setChildren(null); return; } let cancelled = false; (async () => { const parsed = await renderMarkdown(markdown); if (!cancelled) { setChildren(parsed); } })(); return () => { cancelled = true; }; }, [initialMarkdown, markdown]); return <>{children || initialChildren}; }