diff --git a/packages/stack/src/plugins/blog/client/components/shared/markdown-content.tsx b/packages/stack/src/plugins/blog/client/components/shared/markdown-content.tsx
index 7b8c34d72..279aad028 100644
--- a/packages/stack/src/plugins/blog/client/components/shared/markdown-content.tsx
+++ b/packages/stack/src/plugins/blog/client/components/shared/markdown-content.tsx
@@ -1,5 +1,6 @@
"use client";
+import type { ReactNode } from "react";
import { MarkdownContent as SharedMarkdownContent } from "@workspace/ui/components/markdown-content";
import { useStack } from "@btst/stack/context";
import { DefaultImage, DefaultLink } from "./defaults";
@@ -11,6 +12,7 @@ import "highlight.js/styles/panda-syntax-light.css";
export type MarkdownContentProps = {
markdown: string;
className?: string;
+ afterFirstParagraph?: ReactNode;
};
/**
@@ -18,7 +20,11 @@ export type MarkdownContentProps = {
* This is a thin wrapper around the shared MarkdownContent component
* that provides the framework-wide Link and Image components.
*/
-export function MarkdownContent({ markdown, className }: MarkdownContentProps) {
+export function MarkdownContent({
+ markdown,
+ className,
+ afterFirstParagraph,
+}: MarkdownContentProps) {
const { router } = useStack();
const Link = router?.Link ?? DefaultLink;
const Image = router?.Image ?? DefaultImage;
@@ -26,6 +32,7 @@ export function MarkdownContent({ markdown, className }: MarkdownContentProps) {
return (
ReactNode;
+
+ /** Render content after the first top-level body paragraph, preserving a single Markdown document. Omitted if no paragraph exists. */
+ postIntroductionSlot?: (post: SerializedPost) => ReactNode;
}
diff --git a/packages/ui/src/components/markdown-content.tsx b/packages/ui/src/components/markdown-content.tsx
index 81386b2c8..cf2d75c0e 100644
--- a/packages/ui/src/components/markdown-content.tsx
+++ b/packages/ui/src/components/markdown-content.tsx
@@ -243,9 +243,18 @@ function PreRenderer(props: React.HTMLAttributes) {
);
}
+function markIntroduction() {
+ return (tree: { children: { type: string; tagName?: string; data?: Record }[] }) => {
+ const paragraph = tree.children.find(node => node.type === "element" && node.tagName === "p");
+ if (paragraph) paragraph.data = { ...paragraph.data, btstIntroduction: true };
+ };
+}
+
export type MarkdownContentProps = {
/** The markdown string to render */
markdown: string;
+ /** Content inserted after the first top-level paragraph. Omitted when there is no paragraph. */
+ afterFirstParagraph?: ReactNode;
/** Additional CSS class name */
className?: string;
/** Variant for different use cases - 'default' for full blog posts, 'chat' for compact chat messages */
@@ -258,6 +267,7 @@ export type MarkdownContentProps = {
export function MarkdownContent({
markdown,
+ afterFirstParagraph,
className,
variant = "default",
LinkComponent = DefaultLink,
@@ -384,6 +394,12 @@ export function MarkdownContent({
const components = useMemo(() => {
return {
+ p: ({ node, children, ...props }) => (
+ <>
+ {children}
+ {node?.data && "btstIntroduction" in node.data && node.data.btstIntroduction === true ? afterFirstParagraph : null}
+ >
+ ),
a: AnchorRenderer,
img: ImgRenderer,
span: ({ node: _node, className, ...props }) => (
@@ -414,7 +430,7 @@ export function MarkdownContent({
}, [hasMath, mathPlugins]);
const rehypePlugins = useMemo(() => {
- const plugins: unknown[] = [rehypeRaw, rehypeHighlight];
+ const plugins: unknown[] = [rehypeRaw, rehypeHighlight, markIntroduction];
if (hasMath && mathPlugins?.rehypeKatex) {
plugins.push(mathPlugins.rehypeKatex);
}
From fcf3e534e7cf5b080e7b19d6d9c9d408018c7b45 Mon Sep 17 00:00:00 2001
From: olliethedev <5933733+olliethedev@users.noreply.github.com>
Date: Mon, 19 Oct 2026 11:29:46 -0400
Subject: [PATCH 2/6] fix(blog): refresh introduction slot when its props
change
---
packages/stack/registry/btst-ai-chat.json | 2 +-
packages/stack/registry/btst-blog.json | 2 +-
.../blog/__tests__/introduction-slot.test.tsx | 32 +++++++++++++++++++
.../ui/src/components/markdown-content.tsx | 2 +-
4 files changed, 35 insertions(+), 3 deletions(-)
diff --git a/packages/stack/registry/btst-ai-chat.json b/packages/stack/registry/btst-ai-chat.json
index 0aca5d309..9a93c4dbd 100644
--- a/packages/stack/registry/btst-ai-chat.json
+++ b/packages/stack/registry/btst-ai-chat.json
@@ -182,7 +182,7 @@
{
"path": "ui/components/markdown-content.tsx",
"type": "registry:component",
- "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport {\n\tcreateElement,\n\tisValidElement,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n\tuseEffect,\n} from \"react\";\nimport type {\n\tComponentPropsWithoutRef,\n\tComponentType,\n\tMouseEventHandler,\n\tReactNode,\n} from \"react\";\nimport ReactMarkdown from \"react-markdown\";\nimport type { Components } from \"react-markdown\";\nimport rehypeHighlight from \"rehype-highlight\";\nimport rehypeRaw from \"rehype-raw\";\nimport remarkGfm from \"remark-gfm\";\nimport { CopyIcon, CheckIcon } from \"lucide-react\";\n\n// Default Link component\nconst DefaultLink = (props: React.ComponentProps<\"a\">) => {\n\treturn ;\n};\n\n// Default Image component\nconst DefaultImage = (props: React.ImgHTMLAttributes) => {\n\treturn ;\n};\n\n// Utility to detect if markdown contains math syntax\nfunction containsMath(markdown: string): boolean {\n\treturn (\n\t\t/\\$\\$[\\s\\S]+?\\$\\$/.test(markdown) || // Display math\n\t\t/(?;\n\t\treturn getNodeText(props.children as ReactNode);\n\t}\n\treturn \"\";\n}\n\n// Deterministic hash function for generating consistent IDs\nfunction simpleHash(str: string): string {\n\tlet hash = 0;\n\tfor (let i = 0; i < str.length; i++) {\n\t\tconst char = str.charCodeAt(i);\n\t\thash = (hash << 5) - hash + char;\n\t\thash = hash & hash;\n\t}\n\treturn Math.abs(hash).toString(36).slice(0, 6);\n}\n\nfunction isCheckboxElement(\n\tnode: ReactNode,\n): node is ReturnType {\n\treturn (\n\t\tisValidElement(node) &&\n\t\t(node.type as unknown) === \"input\" &&\n\t\t(node.props as { type?: string }).type === \"checkbox\"\n\t);\n}\n\nfunction createTaskListItemRenderer() {\n\treturn function LiRenderer(\n\t\tprops: React.LiHTMLAttributes & { children?: ReactNode },\n\t) {\n\t\tconst { className, children, ...rest } = props;\n\t\tconst isTaskItem = (className ?? \"\").split(\" \").includes(\"task-list-item\");\n\t\tif (!isTaskItem) {\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t}\n\n\t\tconst childArray = Array.isArray(children) ? children : [children];\n\t\tconst checkboxNode = childArray.find(isCheckboxElement);\n\t\tconst nonCheckboxChildren = childArray.filter((c) => !isCheckboxElement(c));\n\n\t\tconst labelText = getNodeText(nonCheckboxChildren as unknown as ReactNode);\n\t\tconst baseId = slugify(labelText || \"task-item\");\n\t\tconst hashSuffix = simpleHash(labelText || \"task-item\");\n\t\tconst uniqueId = `${baseId}-${hashSuffix}`;\n\n\t\treturn (\n\t\t\t\n\t\t\t\t{checkboxNode\n\t\t\t\t\t? createElement(checkboxNode.type, {\n\t\t\t\t\t\t\t...checkboxNode.props,\n\t\t\t\t\t\t\tid: uniqueId,\n\t\t\t\t\t\t\t\"aria-label\": labelText || \"Task item\",\n\t\t\t\t\t\t})\n\t\t\t\t\t: null}\n\t\t\t\t\n\t\t\t\t\t{nonCheckboxChildren as unknown as ReactNode}\n\t\t\t\t \n\t\t\t \n\t\t);\n\t};\n}\n\ntype HeadingTag = \"h1\" | \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\";\nfunction createHeadingRenderer(tag: T, LinkComponent: NonNullable) {\n\treturn function HeadingRenderer(props: ComponentPropsWithoutRef) {\n\t\tconst { children, ...rest } = props as { children: ReactNode };\n\t\tconst text = getNodeText(children);\n\t\tconst id = slugify(text);\n\t\treturn createElement(\n\t\t\ttag,\n\t\t\t{ id, ...(rest as object) },\n\t\t\tchildren,\n\t\t\ttext ? (\n\t\t\t\t\n\t\t\t\t\t#\n\t\t\t\t \n\t\t\t) : null,\n\t\t);\n\t};\n}\n\ntype CodeProps = React.HTMLAttributes & {\n\tinline?: boolean;\n\tnode?: unknown;\n};\n\nfunction CodeRenderer({ inline, className, children, ...rest }: CodeProps) {\n\tconst hasLanguage = /language-([a-z0-9-]+)/i.test(className ?? \"\");\n\tconst isInline = inline ?? !hasLanguage;\n\tif (isInline) {\n\t\treturn (\n\t\t\t\n\t\t\t\t{children}\n\t\t\t\n\t\t);\n\t}\n\treturn (\n\t\t\n\t\t\t{children}\n\t\t\n\t);\n}\n\nfunction PreRenderer(props: React.HTMLAttributes) {\n\tconst { children, ...rest } = props;\n\tconst child = Array.isArray(children) ? children[0] : children;\n\tlet language: string | undefined;\n\tif (isValidElement(child)) {\n\t\tconst className = (child.props as { className?: string }).className;\n\t\tconst match = /language-([a-z0-9-]+)/i.exec(className ?? \"\");\n\t\tlanguage = match?.[1];\n\t}\n\tconst label = (language ?? \"text\").toUpperCase();\n\n\tconst preRef = useRef(null);\n\tconst [copied, setCopied] = useState(false);\n\tconst resetTimerRef = useRef(null);\n\n\tlet codeText = \"\";\n\tif (isValidElement(child)) {\n\t\tconst childProps = child.props as { children?: ReactNode };\n\t\tcodeText = getNodeText(childProps.children as ReactNode);\n\t}\n\tconst normalized = codeText.endsWith(\"\\n\") ? codeText.slice(0, -1) : codeText;\n\tconst lineCount = Math.max(1, normalized.split(\"\\n\").length);\n\tconst digitCount = String(lineCount).length;\n\t\n\tconst onCopy: MouseEventHandler = async (e) => {\n\t\te.preventDefault();\n\t\te.stopPropagation();\n\t\ttry {\n\t\t\tconst text = preRef.current?.textContent ?? \"\";\n\t\t\tif (\n\t\t\t\ttext &&\n\t\t\t\ttypeof navigator !== \"undefined\" &&\n\t\t\t\tnavigator.clipboard?.writeText\n\t\t\t) {\n\t\t\t\tawait navigator.clipboard.writeText(text);\n\t\t\t\tsetCopied(true);\n\t\t\t\tif (resetTimerRef.current) window.clearTimeout(resetTimerRef.current);\n\t\t\t\tresetTimerRef.current = window.setTimeout(() => {\n\t\t\t\t\tsetCopied(false);\n\t\t\t\t\tresetTimerRef.current = null;\n\t\t\t\t}, 2000);\n\t\t\t}\n\t\t} catch {}\n\t};\n\t\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t{label} \n\t\t\t\t\n\t\t\t\t\t{copied ? : }\n\t\t\t\t \n\t\t\t
\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t{Array.from({ length: lineCount }).map((_, idx) => (\n\t\t\t\t\t\t{idx + 1} \n\t\t\t\t\t))}\n\t\t\t\t \n\t\t\t\t
\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t
\n\t\t
\n\t);\n}\n\nfunction markIntroduction() {\n\treturn (tree: { children: { type: string; tagName?: string; data?: Record }[] }) => {\n\t\tconst paragraph = tree.children.find(node => node.type === \"element\" && node.tagName === \"p\");\n\t\tif (paragraph) paragraph.data = { ...paragraph.data, btstIntroduction: true };\n\t};\n}\n\nexport type MarkdownContentProps = {\n\t/** The markdown string to render */\n\tmarkdown: string;\n\t/** Content inserted after the first top-level paragraph. Omitted when there is no paragraph. */\n\tafterFirstParagraph?: ReactNode;\n\t/** Additional CSS class name */\n\tclassName?: string;\n\t/** Variant for different use cases - 'default' for full blog posts, 'chat' for compact chat messages */\n\tvariant?: \"default\" | \"chat\";\n\t/** Custom Link component for navigation */\n\tLinkComponent?: ComponentType & Record>;\n\t/** Custom Image component for images */\n\tImageComponent?: ComponentType & Record>;\n};\n\nexport function MarkdownContent({\n\tmarkdown,\n\tafterFirstParagraph,\n\tclassName,\n\tvariant = \"default\",\n\tLinkComponent = DefaultLink,\n\tImageComponent = DefaultImage,\n}: MarkdownContentProps) {\n\tconst [mathPlugins, setMathPlugins] = useState<{\n\t\tremarkMath: unknown;\n\t\trehypeKatex: unknown;\n\t} | null>(null);\n\tconst [isLoadingMath, setIsLoadingMath] = useState(false);\n\n\tconst hasMath = useMemo(() => containsMath(markdown), [markdown]);\n\n\t// Dynamically load math plugins and CSS only if needed\n\tuseEffect(() => {\n\t\tif (!hasMath || mathPlugins || isLoadingMath) return;\n\n\t\tsetIsLoadingMath(true);\n\n\t\t// Dynamically inject KaTeX CSS into the document\n\t\tconst katexCSSId = \"katex-css\";\n\t\tif (typeof document !== \"undefined\" && !document.getElementById(katexCSSId)) {\n\t\t\tconst link = document.createElement(\"link\");\n\t\t\tlink.id = katexCSSId;\n\t\t\tlink.rel = \"stylesheet\";\n\t\t\tlink.href =\n\t\t\t\t\"https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css\";\n\t\t\tlink.integrity =\n\t\t\t\t\"sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+\";\n\t\t\tlink.crossOrigin = \"anonymous\";\n\t\t\tdocument.head.appendChild(link);\n\n\t\t\t// Add font-display override for KaTeX fonts\n\t\t\tconst fontDisplayStyleId = \"katex-font-display-override\";\n\t\t\tif (!document.getElementById(fontDisplayStyleId)) {\n\t\t\t\tconst style = document.createElement(\"style\");\n\t\t\t\tstyle.id = fontDisplayStyleId;\n\t\t\t\tstyle.textContent = `\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Main';\n\t\t\t\t\t\tfont-style: normal;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Main-Regular.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Math';\n\t\t\t\t\t\tfont-style: italic;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Math-Italic.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t`;\n\t\t\t\tdocument.head.appendChild(style);\n\t\t\t}\n\t\t}\n\n\t\tPromise.all([import(\"remark-math\"), import(\"rehype-katex\")])\n\t\t\t.then(([remarkMathModule, rehypeKatexModule]) => {\n\t\t\t\tsetMathPlugins({\n\t\t\t\t\tremarkMath: remarkMathModule.default,\n\t\t\t\t\trehypeKatex: rehypeKatexModule.default,\n\t\t\t\t});\n\t\t\t})\n\t\t\t.catch((error) => {\n\t\t\t\tconsole.error(\"Failed to load math plugins:\", error);\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\tsetIsLoadingMath(false);\n\t\t\t});\n\t}, [hasMath, mathPlugins, isLoadingMath]);\n\n\t// Create anchor renderer with custom Link component\n\tconst AnchorRenderer = useMemo(() => {\n\t\treturn function AnchorRendererComponent(\n\t\t\tprops: React.AnchorHTMLAttributes,\n\t\t) {\n\t\t\tconst { href = \"\", children, className: anchorClassName, ...rest } = props;\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t};\n\t}, [LinkComponent]);\n\n\t// Create image renderer with custom Image component\n\tconst ImgRenderer = useMemo(() => {\n\t\treturn function ImgRendererComponent(\n\t\t\tprops: React.ImgHTMLAttributes,\n\t\t) {\n\t\t\tconst {\n\t\t\t\tsrc = \"\",\n\t\t\t\talt = \"\",\n\t\t\t\tclassName: imgClassName,\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t\tstyle,\n\t\t\t\t...rest\n\t\t\t} = props;\n\n\t\t\tconst imageProps: React.ComponentProps = {\n\t\t\t\tsrc,\n\t\t\t\talt,\n\t\t\t\tclassName: imgClassName as string,\n\t\t\t\tstyle,\n\t\t\t\t...rest,\n\t\t\t};\n\n\t\t\tif (width != null && height != null) {\n\t\t\t\timageProps.width = width as number;\n\t\t\t\timageProps.height = height as number;\n\t\t\t} else {\n\t\t\t\treturn (\n\t\t\t\t\t\n\t\t\t\t\t\t \n\t\t\t\t\t \n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn ;\n\t\t};\n\t}, [ImageComponent]);\n\n\tconst components = useMemo(() => {\n\t\treturn {\n\t\t\tp: ({ node, children, ...props }) => (\n\t\t\t\t<>\n\t\t\t\t\t{children}
\n\t\t\t\t\t{node?.data && \"btstIntroduction\" in node.data && node.data.btstIntroduction === true ? afterFirstParagraph : null}\n\t\t\t\t>\n\t\t\t),\n\t\t\ta: AnchorRenderer,\n\t\t\timg: ImgRenderer,\n\t\t\tspan: ({ node: _node, className, ...props }) => (\n\t\t\t\t \n\t\t\t),\n\t\t\tcode: CodeRenderer,\n\t\t\tpre: PreRenderer,\n\t\t\th1: createHeadingRenderer(\"h1\", LinkComponent),\n\t\t\th2: createHeadingRenderer(\"h2\", LinkComponent),\n\t\t\th3: createHeadingRenderer(\"h3\", LinkComponent),\n\t\t\th4: createHeadingRenderer(\"h4\", LinkComponent),\n\t\t\th5: createHeadingRenderer(\"h5\", LinkComponent),\n\t\t\th6: createHeadingRenderer(\"h6\", LinkComponent),\n\t\t\tli: createTaskListItemRenderer(),\n\t\t};\n\t}, [AnchorRenderer, ImgRenderer, LinkComponent]);\n\n\tconst remarkPlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [remarkGfm];\n\t\tif (hasMath && mathPlugins?.remarkMath) {\n\t\t\tplugins.push(mathPlugins.remarkMath);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst rehypePlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [rehypeRaw, rehypeHighlight, markIntroduction];\n\t\tif (hasMath && mathPlugins?.rehypeKatex) {\n\t\t\tplugins.push(mathPlugins.rehypeKatex);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst variantClass = variant === \"chat\" ? \"markdown-content-chat\" : \"\";\n\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\n\t\t\t\t\t\t{markdown}\n\t\t\t\t\t \n\t\t\t\t
\n\t\t\t
\n\t\t
\n\t);\n}\n",
+ "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport {\n\tcreateElement,\n\tisValidElement,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n\tuseEffect,\n} from \"react\";\nimport type {\n\tComponentPropsWithoutRef,\n\tComponentType,\n\tMouseEventHandler,\n\tReactNode,\n} from \"react\";\nimport ReactMarkdown from \"react-markdown\";\nimport type { Components } from \"react-markdown\";\nimport rehypeHighlight from \"rehype-highlight\";\nimport rehypeRaw from \"rehype-raw\";\nimport remarkGfm from \"remark-gfm\";\nimport { CopyIcon, CheckIcon } from \"lucide-react\";\n\n// Default Link component\nconst DefaultLink = (props: React.ComponentProps<\"a\">) => {\n\treturn ;\n};\n\n// Default Image component\nconst DefaultImage = (props: React.ImgHTMLAttributes) => {\n\treturn ;\n};\n\n// Utility to detect if markdown contains math syntax\nfunction containsMath(markdown: string): boolean {\n\treturn (\n\t\t/\\$\\$[\\s\\S]+?\\$\\$/.test(markdown) || // Display math\n\t\t/(?;\n\t\treturn getNodeText(props.children as ReactNode);\n\t}\n\treturn \"\";\n}\n\n// Deterministic hash function for generating consistent IDs\nfunction simpleHash(str: string): string {\n\tlet hash = 0;\n\tfor (let i = 0; i < str.length; i++) {\n\t\tconst char = str.charCodeAt(i);\n\t\thash = (hash << 5) - hash + char;\n\t\thash = hash & hash;\n\t}\n\treturn Math.abs(hash).toString(36).slice(0, 6);\n}\n\nfunction isCheckboxElement(\n\tnode: ReactNode,\n): node is ReturnType {\n\treturn (\n\t\tisValidElement(node) &&\n\t\t(node.type as unknown) === \"input\" &&\n\t\t(node.props as { type?: string }).type === \"checkbox\"\n\t);\n}\n\nfunction createTaskListItemRenderer() {\n\treturn function LiRenderer(\n\t\tprops: React.LiHTMLAttributes & { children?: ReactNode },\n\t) {\n\t\tconst { className, children, ...rest } = props;\n\t\tconst isTaskItem = (className ?? \"\").split(\" \").includes(\"task-list-item\");\n\t\tif (!isTaskItem) {\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t}\n\n\t\tconst childArray = Array.isArray(children) ? children : [children];\n\t\tconst checkboxNode = childArray.find(isCheckboxElement);\n\t\tconst nonCheckboxChildren = childArray.filter((c) => !isCheckboxElement(c));\n\n\t\tconst labelText = getNodeText(nonCheckboxChildren as unknown as ReactNode);\n\t\tconst baseId = slugify(labelText || \"task-item\");\n\t\tconst hashSuffix = simpleHash(labelText || \"task-item\");\n\t\tconst uniqueId = `${baseId}-${hashSuffix}`;\n\n\t\treturn (\n\t\t\t\n\t\t\t\t{checkboxNode\n\t\t\t\t\t? createElement(checkboxNode.type, {\n\t\t\t\t\t\t\t...checkboxNode.props,\n\t\t\t\t\t\t\tid: uniqueId,\n\t\t\t\t\t\t\t\"aria-label\": labelText || \"Task item\",\n\t\t\t\t\t\t})\n\t\t\t\t\t: null}\n\t\t\t\t\n\t\t\t\t\t{nonCheckboxChildren as unknown as ReactNode}\n\t\t\t\t \n\t\t\t \n\t\t);\n\t};\n}\n\ntype HeadingTag = \"h1\" | \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\";\nfunction createHeadingRenderer(tag: T, LinkComponent: NonNullable) {\n\treturn function HeadingRenderer(props: ComponentPropsWithoutRef) {\n\t\tconst { children, ...rest } = props as { children: ReactNode };\n\t\tconst text = getNodeText(children);\n\t\tconst id = slugify(text);\n\t\treturn createElement(\n\t\t\ttag,\n\t\t\t{ id, ...(rest as object) },\n\t\t\tchildren,\n\t\t\ttext ? (\n\t\t\t\t\n\t\t\t\t\t#\n\t\t\t\t \n\t\t\t) : null,\n\t\t);\n\t};\n}\n\ntype CodeProps = React.HTMLAttributes & {\n\tinline?: boolean;\n\tnode?: unknown;\n};\n\nfunction CodeRenderer({ inline, className, children, ...rest }: CodeProps) {\n\tconst hasLanguage = /language-([a-z0-9-]+)/i.test(className ?? \"\");\n\tconst isInline = inline ?? !hasLanguage;\n\tif (isInline) {\n\t\treturn (\n\t\t\t\n\t\t\t\t{children}\n\t\t\t\n\t\t);\n\t}\n\treturn (\n\t\t\n\t\t\t{children}\n\t\t\n\t);\n}\n\nfunction PreRenderer(props: React.HTMLAttributes) {\n\tconst { children, ...rest } = props;\n\tconst child = Array.isArray(children) ? children[0] : children;\n\tlet language: string | undefined;\n\tif (isValidElement(child)) {\n\t\tconst className = (child.props as { className?: string }).className;\n\t\tconst match = /language-([a-z0-9-]+)/i.exec(className ?? \"\");\n\t\tlanguage = match?.[1];\n\t}\n\tconst label = (language ?? \"text\").toUpperCase();\n\n\tconst preRef = useRef(null);\n\tconst [copied, setCopied] = useState(false);\n\tconst resetTimerRef = useRef(null);\n\n\tlet codeText = \"\";\n\tif (isValidElement(child)) {\n\t\tconst childProps = child.props as { children?: ReactNode };\n\t\tcodeText = getNodeText(childProps.children as ReactNode);\n\t}\n\tconst normalized = codeText.endsWith(\"\\n\") ? codeText.slice(0, -1) : codeText;\n\tconst lineCount = Math.max(1, normalized.split(\"\\n\").length);\n\tconst digitCount = String(lineCount).length;\n\t\n\tconst onCopy: MouseEventHandler = async (e) => {\n\t\te.preventDefault();\n\t\te.stopPropagation();\n\t\ttry {\n\t\t\tconst text = preRef.current?.textContent ?? \"\";\n\t\t\tif (\n\t\t\t\ttext &&\n\t\t\t\ttypeof navigator !== \"undefined\" &&\n\t\t\t\tnavigator.clipboard?.writeText\n\t\t\t) {\n\t\t\t\tawait navigator.clipboard.writeText(text);\n\t\t\t\tsetCopied(true);\n\t\t\t\tif (resetTimerRef.current) window.clearTimeout(resetTimerRef.current);\n\t\t\t\tresetTimerRef.current = window.setTimeout(() => {\n\t\t\t\t\tsetCopied(false);\n\t\t\t\t\tresetTimerRef.current = null;\n\t\t\t\t}, 2000);\n\t\t\t}\n\t\t} catch {}\n\t};\n\t\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t{label} \n\t\t\t\t\n\t\t\t\t\t{copied ? : }\n\t\t\t\t \n\t\t\t
\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t{Array.from({ length: lineCount }).map((_, idx) => (\n\t\t\t\t\t\t{idx + 1} \n\t\t\t\t\t))}\n\t\t\t\t \n\t\t\t\t
\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t
\n\t\t
\n\t);\n}\n\nfunction markIntroduction() {\n\treturn (tree: { children: { type: string; tagName?: string; data?: Record }[] }) => {\n\t\tconst paragraph = tree.children.find(node => node.type === \"element\" && node.tagName === \"p\");\n\t\tif (paragraph) paragraph.data = { ...paragraph.data, btstIntroduction: true };\n\t};\n}\n\nexport type MarkdownContentProps = {\n\t/** The markdown string to render */\n\tmarkdown: string;\n\t/** Content inserted after the first top-level paragraph. Omitted when there is no paragraph. */\n\tafterFirstParagraph?: ReactNode;\n\t/** Additional CSS class name */\n\tclassName?: string;\n\t/** Variant for different use cases - 'default' for full blog posts, 'chat' for compact chat messages */\n\tvariant?: \"default\" | \"chat\";\n\t/** Custom Link component for navigation */\n\tLinkComponent?: ComponentType & Record>;\n\t/** Custom Image component for images */\n\tImageComponent?: ComponentType & Record>;\n};\n\nexport function MarkdownContent({\n\tmarkdown,\n\tafterFirstParagraph,\n\tclassName,\n\tvariant = \"default\",\n\tLinkComponent = DefaultLink,\n\tImageComponent = DefaultImage,\n}: MarkdownContentProps) {\n\tconst [mathPlugins, setMathPlugins] = useState<{\n\t\tremarkMath: unknown;\n\t\trehypeKatex: unknown;\n\t} | null>(null);\n\tconst [isLoadingMath, setIsLoadingMath] = useState(false);\n\n\tconst hasMath = useMemo(() => containsMath(markdown), [markdown]);\n\n\t// Dynamically load math plugins and CSS only if needed\n\tuseEffect(() => {\n\t\tif (!hasMath || mathPlugins || isLoadingMath) return;\n\n\t\tsetIsLoadingMath(true);\n\n\t\t// Dynamically inject KaTeX CSS into the document\n\t\tconst katexCSSId = \"katex-css\";\n\t\tif (typeof document !== \"undefined\" && !document.getElementById(katexCSSId)) {\n\t\t\tconst link = document.createElement(\"link\");\n\t\t\tlink.id = katexCSSId;\n\t\t\tlink.rel = \"stylesheet\";\n\t\t\tlink.href =\n\t\t\t\t\"https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css\";\n\t\t\tlink.integrity =\n\t\t\t\t\"sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+\";\n\t\t\tlink.crossOrigin = \"anonymous\";\n\t\t\tdocument.head.appendChild(link);\n\n\t\t\t// Add font-display override for KaTeX fonts\n\t\t\tconst fontDisplayStyleId = \"katex-font-display-override\";\n\t\t\tif (!document.getElementById(fontDisplayStyleId)) {\n\t\t\t\tconst style = document.createElement(\"style\");\n\t\t\t\tstyle.id = fontDisplayStyleId;\n\t\t\t\tstyle.textContent = `\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Main';\n\t\t\t\t\t\tfont-style: normal;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Main-Regular.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Math';\n\t\t\t\t\t\tfont-style: italic;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Math-Italic.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t`;\n\t\t\t\tdocument.head.appendChild(style);\n\t\t\t}\n\t\t}\n\n\t\tPromise.all([import(\"remark-math\"), import(\"rehype-katex\")])\n\t\t\t.then(([remarkMathModule, rehypeKatexModule]) => {\n\t\t\t\tsetMathPlugins({\n\t\t\t\t\tremarkMath: remarkMathModule.default,\n\t\t\t\t\trehypeKatex: rehypeKatexModule.default,\n\t\t\t\t});\n\t\t\t})\n\t\t\t.catch((error) => {\n\t\t\t\tconsole.error(\"Failed to load math plugins:\", error);\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\tsetIsLoadingMath(false);\n\t\t\t});\n\t}, [hasMath, mathPlugins, isLoadingMath]);\n\n\t// Create anchor renderer with custom Link component\n\tconst AnchorRenderer = useMemo(() => {\n\t\treturn function AnchorRendererComponent(\n\t\t\tprops: React.AnchorHTMLAttributes,\n\t\t) {\n\t\t\tconst { href = \"\", children, className: anchorClassName, ...rest } = props;\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t};\n\t}, [LinkComponent]);\n\n\t// Create image renderer with custom Image component\n\tconst ImgRenderer = useMemo(() => {\n\t\treturn function ImgRendererComponent(\n\t\t\tprops: React.ImgHTMLAttributes,\n\t\t) {\n\t\t\tconst {\n\t\t\t\tsrc = \"\",\n\t\t\t\talt = \"\",\n\t\t\t\tclassName: imgClassName,\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t\tstyle,\n\t\t\t\t...rest\n\t\t\t} = props;\n\n\t\t\tconst imageProps: React.ComponentProps = {\n\t\t\t\tsrc,\n\t\t\t\talt,\n\t\t\t\tclassName: imgClassName as string,\n\t\t\t\tstyle,\n\t\t\t\t...rest,\n\t\t\t};\n\n\t\t\tif (width != null && height != null) {\n\t\t\t\timageProps.width = width as number;\n\t\t\t\timageProps.height = height as number;\n\t\t\t} else {\n\t\t\t\treturn (\n\t\t\t\t\t\n\t\t\t\t\t\t \n\t\t\t\t\t \n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn ;\n\t\t};\n\t}, [ImageComponent]);\n\n\tconst components = useMemo(() => {\n\t\treturn {\n\t\t\tp: ({ node, children, ...props }) => (\n\t\t\t\t<>\n\t\t\t\t\t{children}
\n\t\t\t\t\t{node?.data && \"btstIntroduction\" in node.data && node.data.btstIntroduction === true ? afterFirstParagraph : null}\n\t\t\t\t>\n\t\t\t),\n\t\t\ta: AnchorRenderer,\n\t\t\timg: ImgRenderer,\n\t\t\tspan: ({ node: _node, className, ...props }) => (\n\t\t\t\t \n\t\t\t),\n\t\t\tcode: CodeRenderer,\n\t\t\tpre: PreRenderer,\n\t\t\th1: createHeadingRenderer(\"h1\", LinkComponent),\n\t\t\th2: createHeadingRenderer(\"h2\", LinkComponent),\n\t\t\th3: createHeadingRenderer(\"h3\", LinkComponent),\n\t\t\th4: createHeadingRenderer(\"h4\", LinkComponent),\n\t\t\th5: createHeadingRenderer(\"h5\", LinkComponent),\n\t\t\th6: createHeadingRenderer(\"h6\", LinkComponent),\n\t\t\tli: createTaskListItemRenderer(),\n\t\t};\n\t}, [AnchorRenderer, ImgRenderer, LinkComponent, afterFirstParagraph]);\n\n\tconst remarkPlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [remarkGfm];\n\t\tif (hasMath && mathPlugins?.remarkMath) {\n\t\t\tplugins.push(mathPlugins.remarkMath);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst rehypePlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [rehypeRaw, rehypeHighlight, markIntroduction];\n\t\tif (hasMath && mathPlugins?.rehypeKatex) {\n\t\t\tplugins.push(mathPlugins.rehypeKatex);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst variantClass = variant === \"chat\" ? \"markdown-content-chat\" : \"\";\n\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\n\t\t\t\t\t\t{markdown}\n\t\t\t\t\t \n\t\t\t\t
\n\t\t\t
\n\t\t
\n\t);\n}\n",
"target": "src/components/ui/markdown-content.tsx"
},
{
diff --git a/packages/stack/registry/btst-blog.json b/packages/stack/registry/btst-blog.json
index 6526d23c6..cfe3e15b1 100644
--- a/packages/stack/registry/btst-blog.json
+++ b/packages/stack/registry/btst-blog.json
@@ -430,7 +430,7 @@
{
"path": "ui/components/markdown-content.tsx",
"type": "registry:component",
- "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport {\n\tcreateElement,\n\tisValidElement,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n\tuseEffect,\n} from \"react\";\nimport type {\n\tComponentPropsWithoutRef,\n\tComponentType,\n\tMouseEventHandler,\n\tReactNode,\n} from \"react\";\nimport ReactMarkdown from \"react-markdown\";\nimport type { Components } from \"react-markdown\";\nimport rehypeHighlight from \"rehype-highlight\";\nimport rehypeRaw from \"rehype-raw\";\nimport remarkGfm from \"remark-gfm\";\nimport { CopyIcon, CheckIcon } from \"lucide-react\";\n\n// Default Link component\nconst DefaultLink = (props: React.ComponentProps<\"a\">) => {\n\treturn ;\n};\n\n// Default Image component\nconst DefaultImage = (props: React.ImgHTMLAttributes) => {\n\treturn ;\n};\n\n// Utility to detect if markdown contains math syntax\nfunction containsMath(markdown: string): boolean {\n\treturn (\n\t\t/\\$\\$[\\s\\S]+?\\$\\$/.test(markdown) || // Display math\n\t\t/(?;\n\t\treturn getNodeText(props.children as ReactNode);\n\t}\n\treturn \"\";\n}\n\n// Deterministic hash function for generating consistent IDs\nfunction simpleHash(str: string): string {\n\tlet hash = 0;\n\tfor (let i = 0; i < str.length; i++) {\n\t\tconst char = str.charCodeAt(i);\n\t\thash = (hash << 5) - hash + char;\n\t\thash = hash & hash;\n\t}\n\treturn Math.abs(hash).toString(36).slice(0, 6);\n}\n\nfunction isCheckboxElement(\n\tnode: ReactNode,\n): node is ReturnType {\n\treturn (\n\t\tisValidElement(node) &&\n\t\t(node.type as unknown) === \"input\" &&\n\t\t(node.props as { type?: string }).type === \"checkbox\"\n\t);\n}\n\nfunction createTaskListItemRenderer() {\n\treturn function LiRenderer(\n\t\tprops: React.LiHTMLAttributes & { children?: ReactNode },\n\t) {\n\t\tconst { className, children, ...rest } = props;\n\t\tconst isTaskItem = (className ?? \"\").split(\" \").includes(\"task-list-item\");\n\t\tif (!isTaskItem) {\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t}\n\n\t\tconst childArray = Array.isArray(children) ? children : [children];\n\t\tconst checkboxNode = childArray.find(isCheckboxElement);\n\t\tconst nonCheckboxChildren = childArray.filter((c) => !isCheckboxElement(c));\n\n\t\tconst labelText = getNodeText(nonCheckboxChildren as unknown as ReactNode);\n\t\tconst baseId = slugify(labelText || \"task-item\");\n\t\tconst hashSuffix = simpleHash(labelText || \"task-item\");\n\t\tconst uniqueId = `${baseId}-${hashSuffix}`;\n\n\t\treturn (\n\t\t\t\n\t\t\t\t{checkboxNode\n\t\t\t\t\t? createElement(checkboxNode.type, {\n\t\t\t\t\t\t\t...checkboxNode.props,\n\t\t\t\t\t\t\tid: uniqueId,\n\t\t\t\t\t\t\t\"aria-label\": labelText || \"Task item\",\n\t\t\t\t\t\t})\n\t\t\t\t\t: null}\n\t\t\t\t\n\t\t\t\t\t{nonCheckboxChildren as unknown as ReactNode}\n\t\t\t\t \n\t\t\t \n\t\t);\n\t};\n}\n\ntype HeadingTag = \"h1\" | \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\";\nfunction createHeadingRenderer(tag: T, LinkComponent: NonNullable) {\n\treturn function HeadingRenderer(props: ComponentPropsWithoutRef) {\n\t\tconst { children, ...rest } = props as { children: ReactNode };\n\t\tconst text = getNodeText(children);\n\t\tconst id = slugify(text);\n\t\treturn createElement(\n\t\t\ttag,\n\t\t\t{ id, ...(rest as object) },\n\t\t\tchildren,\n\t\t\ttext ? (\n\t\t\t\t\n\t\t\t\t\t#\n\t\t\t\t \n\t\t\t) : null,\n\t\t);\n\t};\n}\n\ntype CodeProps = React.HTMLAttributes & {\n\tinline?: boolean;\n\tnode?: unknown;\n};\n\nfunction CodeRenderer({ inline, className, children, ...rest }: CodeProps) {\n\tconst hasLanguage = /language-([a-z0-9-]+)/i.test(className ?? \"\");\n\tconst isInline = inline ?? !hasLanguage;\n\tif (isInline) {\n\t\treturn (\n\t\t\t\n\t\t\t\t{children}\n\t\t\t\n\t\t);\n\t}\n\treturn (\n\t\t\n\t\t\t{children}\n\t\t\n\t);\n}\n\nfunction PreRenderer(props: React.HTMLAttributes) {\n\tconst { children, ...rest } = props;\n\tconst child = Array.isArray(children) ? children[0] : children;\n\tlet language: string | undefined;\n\tif (isValidElement(child)) {\n\t\tconst className = (child.props as { className?: string }).className;\n\t\tconst match = /language-([a-z0-9-]+)/i.exec(className ?? \"\");\n\t\tlanguage = match?.[1];\n\t}\n\tconst label = (language ?? \"text\").toUpperCase();\n\n\tconst preRef = useRef(null);\n\tconst [copied, setCopied] = useState(false);\n\tconst resetTimerRef = useRef(null);\n\n\tlet codeText = \"\";\n\tif (isValidElement(child)) {\n\t\tconst childProps = child.props as { children?: ReactNode };\n\t\tcodeText = getNodeText(childProps.children as ReactNode);\n\t}\n\tconst normalized = codeText.endsWith(\"\\n\") ? codeText.slice(0, -1) : codeText;\n\tconst lineCount = Math.max(1, normalized.split(\"\\n\").length);\n\tconst digitCount = String(lineCount).length;\n\t\n\tconst onCopy: MouseEventHandler = async (e) => {\n\t\te.preventDefault();\n\t\te.stopPropagation();\n\t\ttry {\n\t\t\tconst text = preRef.current?.textContent ?? \"\";\n\t\t\tif (\n\t\t\t\ttext &&\n\t\t\t\ttypeof navigator !== \"undefined\" &&\n\t\t\t\tnavigator.clipboard?.writeText\n\t\t\t) {\n\t\t\t\tawait navigator.clipboard.writeText(text);\n\t\t\t\tsetCopied(true);\n\t\t\t\tif (resetTimerRef.current) window.clearTimeout(resetTimerRef.current);\n\t\t\t\tresetTimerRef.current = window.setTimeout(() => {\n\t\t\t\t\tsetCopied(false);\n\t\t\t\t\tresetTimerRef.current = null;\n\t\t\t\t}, 2000);\n\t\t\t}\n\t\t} catch {}\n\t};\n\t\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t{label} \n\t\t\t\t\n\t\t\t\t\t{copied ? : }\n\t\t\t\t \n\t\t\t
\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t{Array.from({ length: lineCount }).map((_, idx) => (\n\t\t\t\t\t\t{idx + 1} \n\t\t\t\t\t))}\n\t\t\t\t \n\t\t\t\t
\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t
\n\t\t
\n\t);\n}\n\nfunction markIntroduction() {\n\treturn (tree: { children: { type: string; tagName?: string; data?: Record }[] }) => {\n\t\tconst paragraph = tree.children.find(node => node.type === \"element\" && node.tagName === \"p\");\n\t\tif (paragraph) paragraph.data = { ...paragraph.data, btstIntroduction: true };\n\t};\n}\n\nexport type MarkdownContentProps = {\n\t/** The markdown string to render */\n\tmarkdown: string;\n\t/** Content inserted after the first top-level paragraph. Omitted when there is no paragraph. */\n\tafterFirstParagraph?: ReactNode;\n\t/** Additional CSS class name */\n\tclassName?: string;\n\t/** Variant for different use cases - 'default' for full blog posts, 'chat' for compact chat messages */\n\tvariant?: \"default\" | \"chat\";\n\t/** Custom Link component for navigation */\n\tLinkComponent?: ComponentType & Record>;\n\t/** Custom Image component for images */\n\tImageComponent?: ComponentType & Record>;\n};\n\nexport function MarkdownContent({\n\tmarkdown,\n\tafterFirstParagraph,\n\tclassName,\n\tvariant = \"default\",\n\tLinkComponent = DefaultLink,\n\tImageComponent = DefaultImage,\n}: MarkdownContentProps) {\n\tconst [mathPlugins, setMathPlugins] = useState<{\n\t\tremarkMath: unknown;\n\t\trehypeKatex: unknown;\n\t} | null>(null);\n\tconst [isLoadingMath, setIsLoadingMath] = useState(false);\n\n\tconst hasMath = useMemo(() => containsMath(markdown), [markdown]);\n\n\t// Dynamically load math plugins and CSS only if needed\n\tuseEffect(() => {\n\t\tif (!hasMath || mathPlugins || isLoadingMath) return;\n\n\t\tsetIsLoadingMath(true);\n\n\t\t// Dynamically inject KaTeX CSS into the document\n\t\tconst katexCSSId = \"katex-css\";\n\t\tif (typeof document !== \"undefined\" && !document.getElementById(katexCSSId)) {\n\t\t\tconst link = document.createElement(\"link\");\n\t\t\tlink.id = katexCSSId;\n\t\t\tlink.rel = \"stylesheet\";\n\t\t\tlink.href =\n\t\t\t\t\"https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css\";\n\t\t\tlink.integrity =\n\t\t\t\t\"sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+\";\n\t\t\tlink.crossOrigin = \"anonymous\";\n\t\t\tdocument.head.appendChild(link);\n\n\t\t\t// Add font-display override for KaTeX fonts\n\t\t\tconst fontDisplayStyleId = \"katex-font-display-override\";\n\t\t\tif (!document.getElementById(fontDisplayStyleId)) {\n\t\t\t\tconst style = document.createElement(\"style\");\n\t\t\t\tstyle.id = fontDisplayStyleId;\n\t\t\t\tstyle.textContent = `\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Main';\n\t\t\t\t\t\tfont-style: normal;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Main-Regular.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Math';\n\t\t\t\t\t\tfont-style: italic;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Math-Italic.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t`;\n\t\t\t\tdocument.head.appendChild(style);\n\t\t\t}\n\t\t}\n\n\t\tPromise.all([import(\"remark-math\"), import(\"rehype-katex\")])\n\t\t\t.then(([remarkMathModule, rehypeKatexModule]) => {\n\t\t\t\tsetMathPlugins({\n\t\t\t\t\tremarkMath: remarkMathModule.default,\n\t\t\t\t\trehypeKatex: rehypeKatexModule.default,\n\t\t\t\t});\n\t\t\t})\n\t\t\t.catch((error) => {\n\t\t\t\tconsole.error(\"Failed to load math plugins:\", error);\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\tsetIsLoadingMath(false);\n\t\t\t});\n\t}, [hasMath, mathPlugins, isLoadingMath]);\n\n\t// Create anchor renderer with custom Link component\n\tconst AnchorRenderer = useMemo(() => {\n\t\treturn function AnchorRendererComponent(\n\t\t\tprops: React.AnchorHTMLAttributes,\n\t\t) {\n\t\t\tconst { href = \"\", children, className: anchorClassName, ...rest } = props;\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t};\n\t}, [LinkComponent]);\n\n\t// Create image renderer with custom Image component\n\tconst ImgRenderer = useMemo(() => {\n\t\treturn function ImgRendererComponent(\n\t\t\tprops: React.ImgHTMLAttributes,\n\t\t) {\n\t\t\tconst {\n\t\t\t\tsrc = \"\",\n\t\t\t\talt = \"\",\n\t\t\t\tclassName: imgClassName,\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t\tstyle,\n\t\t\t\t...rest\n\t\t\t} = props;\n\n\t\t\tconst imageProps: React.ComponentProps = {\n\t\t\t\tsrc,\n\t\t\t\talt,\n\t\t\t\tclassName: imgClassName as string,\n\t\t\t\tstyle,\n\t\t\t\t...rest,\n\t\t\t};\n\n\t\t\tif (width != null && height != null) {\n\t\t\t\timageProps.width = width as number;\n\t\t\t\timageProps.height = height as number;\n\t\t\t} else {\n\t\t\t\treturn (\n\t\t\t\t\t\n\t\t\t\t\t\t \n\t\t\t\t\t \n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn ;\n\t\t};\n\t}, [ImageComponent]);\n\n\tconst components = useMemo(() => {\n\t\treturn {\n\t\t\tp: ({ node, children, ...props }) => (\n\t\t\t\t<>\n\t\t\t\t\t{children}
\n\t\t\t\t\t{node?.data && \"btstIntroduction\" in node.data && node.data.btstIntroduction === true ? afterFirstParagraph : null}\n\t\t\t\t>\n\t\t\t),\n\t\t\ta: AnchorRenderer,\n\t\t\timg: ImgRenderer,\n\t\t\tspan: ({ node: _node, className, ...props }) => (\n\t\t\t\t \n\t\t\t),\n\t\t\tcode: CodeRenderer,\n\t\t\tpre: PreRenderer,\n\t\t\th1: createHeadingRenderer(\"h1\", LinkComponent),\n\t\t\th2: createHeadingRenderer(\"h2\", LinkComponent),\n\t\t\th3: createHeadingRenderer(\"h3\", LinkComponent),\n\t\t\th4: createHeadingRenderer(\"h4\", LinkComponent),\n\t\t\th5: createHeadingRenderer(\"h5\", LinkComponent),\n\t\t\th6: createHeadingRenderer(\"h6\", LinkComponent),\n\t\t\tli: createTaskListItemRenderer(),\n\t\t};\n\t}, [AnchorRenderer, ImgRenderer, LinkComponent]);\n\n\tconst remarkPlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [remarkGfm];\n\t\tif (hasMath && mathPlugins?.remarkMath) {\n\t\t\tplugins.push(mathPlugins.remarkMath);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst rehypePlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [rehypeRaw, rehypeHighlight, markIntroduction];\n\t\tif (hasMath && mathPlugins?.rehypeKatex) {\n\t\t\tplugins.push(mathPlugins.rehypeKatex);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst variantClass = variant === \"chat\" ? \"markdown-content-chat\" : \"\";\n\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\n\t\t\t\t\t\t{markdown}\n\t\t\t\t\t \n\t\t\t\t
\n\t\t\t
\n\t\t
\n\t);\n}\n",
+ "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport {\n\tcreateElement,\n\tisValidElement,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n\tuseEffect,\n} from \"react\";\nimport type {\n\tComponentPropsWithoutRef,\n\tComponentType,\n\tMouseEventHandler,\n\tReactNode,\n} from \"react\";\nimport ReactMarkdown from \"react-markdown\";\nimport type { Components } from \"react-markdown\";\nimport rehypeHighlight from \"rehype-highlight\";\nimport rehypeRaw from \"rehype-raw\";\nimport remarkGfm from \"remark-gfm\";\nimport { CopyIcon, CheckIcon } from \"lucide-react\";\n\n// Default Link component\nconst DefaultLink = (props: React.ComponentProps<\"a\">) => {\n\treturn ;\n};\n\n// Default Image component\nconst DefaultImage = (props: React.ImgHTMLAttributes) => {\n\treturn ;\n};\n\n// Utility to detect if markdown contains math syntax\nfunction containsMath(markdown: string): boolean {\n\treturn (\n\t\t/\\$\\$[\\s\\S]+?\\$\\$/.test(markdown) || // Display math\n\t\t/(?;\n\t\treturn getNodeText(props.children as ReactNode);\n\t}\n\treturn \"\";\n}\n\n// Deterministic hash function for generating consistent IDs\nfunction simpleHash(str: string): string {\n\tlet hash = 0;\n\tfor (let i = 0; i < str.length; i++) {\n\t\tconst char = str.charCodeAt(i);\n\t\thash = (hash << 5) - hash + char;\n\t\thash = hash & hash;\n\t}\n\treturn Math.abs(hash).toString(36).slice(0, 6);\n}\n\nfunction isCheckboxElement(\n\tnode: ReactNode,\n): node is ReturnType {\n\treturn (\n\t\tisValidElement(node) &&\n\t\t(node.type as unknown) === \"input\" &&\n\t\t(node.props as { type?: string }).type === \"checkbox\"\n\t);\n}\n\nfunction createTaskListItemRenderer() {\n\treturn function LiRenderer(\n\t\tprops: React.LiHTMLAttributes & { children?: ReactNode },\n\t) {\n\t\tconst { className, children, ...rest } = props;\n\t\tconst isTaskItem = (className ?? \"\").split(\" \").includes(\"task-list-item\");\n\t\tif (!isTaskItem) {\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t}\n\n\t\tconst childArray = Array.isArray(children) ? children : [children];\n\t\tconst checkboxNode = childArray.find(isCheckboxElement);\n\t\tconst nonCheckboxChildren = childArray.filter((c) => !isCheckboxElement(c));\n\n\t\tconst labelText = getNodeText(nonCheckboxChildren as unknown as ReactNode);\n\t\tconst baseId = slugify(labelText || \"task-item\");\n\t\tconst hashSuffix = simpleHash(labelText || \"task-item\");\n\t\tconst uniqueId = `${baseId}-${hashSuffix}`;\n\n\t\treturn (\n\t\t\t\n\t\t\t\t{checkboxNode\n\t\t\t\t\t? createElement(checkboxNode.type, {\n\t\t\t\t\t\t\t...checkboxNode.props,\n\t\t\t\t\t\t\tid: uniqueId,\n\t\t\t\t\t\t\t\"aria-label\": labelText || \"Task item\",\n\t\t\t\t\t\t})\n\t\t\t\t\t: null}\n\t\t\t\t\n\t\t\t\t\t{nonCheckboxChildren as unknown as ReactNode}\n\t\t\t\t \n\t\t\t \n\t\t);\n\t};\n}\n\ntype HeadingTag = \"h1\" | \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\";\nfunction createHeadingRenderer(tag: T, LinkComponent: NonNullable) {\n\treturn function HeadingRenderer(props: ComponentPropsWithoutRef) {\n\t\tconst { children, ...rest } = props as { children: ReactNode };\n\t\tconst text = getNodeText(children);\n\t\tconst id = slugify(text);\n\t\treturn createElement(\n\t\t\ttag,\n\t\t\t{ id, ...(rest as object) },\n\t\t\tchildren,\n\t\t\ttext ? (\n\t\t\t\t\n\t\t\t\t\t#\n\t\t\t\t \n\t\t\t) : null,\n\t\t);\n\t};\n}\n\ntype CodeProps = React.HTMLAttributes & {\n\tinline?: boolean;\n\tnode?: unknown;\n};\n\nfunction CodeRenderer({ inline, className, children, ...rest }: CodeProps) {\n\tconst hasLanguage = /language-([a-z0-9-]+)/i.test(className ?? \"\");\n\tconst isInline = inline ?? !hasLanguage;\n\tif (isInline) {\n\t\treturn (\n\t\t\t\n\t\t\t\t{children}\n\t\t\t\n\t\t);\n\t}\n\treturn (\n\t\t\n\t\t\t{children}\n\t\t\n\t);\n}\n\nfunction PreRenderer(props: React.HTMLAttributes) {\n\tconst { children, ...rest } = props;\n\tconst child = Array.isArray(children) ? children[0] : children;\n\tlet language: string | undefined;\n\tif (isValidElement(child)) {\n\t\tconst className = (child.props as { className?: string }).className;\n\t\tconst match = /language-([a-z0-9-]+)/i.exec(className ?? \"\");\n\t\tlanguage = match?.[1];\n\t}\n\tconst label = (language ?? \"text\").toUpperCase();\n\n\tconst preRef = useRef(null);\n\tconst [copied, setCopied] = useState(false);\n\tconst resetTimerRef = useRef(null);\n\n\tlet codeText = \"\";\n\tif (isValidElement(child)) {\n\t\tconst childProps = child.props as { children?: ReactNode };\n\t\tcodeText = getNodeText(childProps.children as ReactNode);\n\t}\n\tconst normalized = codeText.endsWith(\"\\n\") ? codeText.slice(0, -1) : codeText;\n\tconst lineCount = Math.max(1, normalized.split(\"\\n\").length);\n\tconst digitCount = String(lineCount).length;\n\t\n\tconst onCopy: MouseEventHandler = async (e) => {\n\t\te.preventDefault();\n\t\te.stopPropagation();\n\t\ttry {\n\t\t\tconst text = preRef.current?.textContent ?? \"\";\n\t\t\tif (\n\t\t\t\ttext &&\n\t\t\t\ttypeof navigator !== \"undefined\" &&\n\t\t\t\tnavigator.clipboard?.writeText\n\t\t\t) {\n\t\t\t\tawait navigator.clipboard.writeText(text);\n\t\t\t\tsetCopied(true);\n\t\t\t\tif (resetTimerRef.current) window.clearTimeout(resetTimerRef.current);\n\t\t\t\tresetTimerRef.current = window.setTimeout(() => {\n\t\t\t\t\tsetCopied(false);\n\t\t\t\t\tresetTimerRef.current = null;\n\t\t\t\t}, 2000);\n\t\t\t}\n\t\t} catch {}\n\t};\n\t\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t{label} \n\t\t\t\t\n\t\t\t\t\t{copied ? : }\n\t\t\t\t \n\t\t\t
\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t{Array.from({ length: lineCount }).map((_, idx) => (\n\t\t\t\t\t\t{idx + 1} \n\t\t\t\t\t))}\n\t\t\t\t \n\t\t\t\t
\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t
\n\t\t
\n\t);\n}\n\nfunction markIntroduction() {\n\treturn (tree: { children: { type: string; tagName?: string; data?: Record }[] }) => {\n\t\tconst paragraph = tree.children.find(node => node.type === \"element\" && node.tagName === \"p\");\n\t\tif (paragraph) paragraph.data = { ...paragraph.data, btstIntroduction: true };\n\t};\n}\n\nexport type MarkdownContentProps = {\n\t/** The markdown string to render */\n\tmarkdown: string;\n\t/** Content inserted after the first top-level paragraph. Omitted when there is no paragraph. */\n\tafterFirstParagraph?: ReactNode;\n\t/** Additional CSS class name */\n\tclassName?: string;\n\t/** Variant for different use cases - 'default' for full blog posts, 'chat' for compact chat messages */\n\tvariant?: \"default\" | \"chat\";\n\t/** Custom Link component for navigation */\n\tLinkComponent?: ComponentType & Record>;\n\t/** Custom Image component for images */\n\tImageComponent?: ComponentType & Record>;\n};\n\nexport function MarkdownContent({\n\tmarkdown,\n\tafterFirstParagraph,\n\tclassName,\n\tvariant = \"default\",\n\tLinkComponent = DefaultLink,\n\tImageComponent = DefaultImage,\n}: MarkdownContentProps) {\n\tconst [mathPlugins, setMathPlugins] = useState<{\n\t\tremarkMath: unknown;\n\t\trehypeKatex: unknown;\n\t} | null>(null);\n\tconst [isLoadingMath, setIsLoadingMath] = useState(false);\n\n\tconst hasMath = useMemo(() => containsMath(markdown), [markdown]);\n\n\t// Dynamically load math plugins and CSS only if needed\n\tuseEffect(() => {\n\t\tif (!hasMath || mathPlugins || isLoadingMath) return;\n\n\t\tsetIsLoadingMath(true);\n\n\t\t// Dynamically inject KaTeX CSS into the document\n\t\tconst katexCSSId = \"katex-css\";\n\t\tif (typeof document !== \"undefined\" && !document.getElementById(katexCSSId)) {\n\t\t\tconst link = document.createElement(\"link\");\n\t\t\tlink.id = katexCSSId;\n\t\t\tlink.rel = \"stylesheet\";\n\t\t\tlink.href =\n\t\t\t\t\"https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css\";\n\t\t\tlink.integrity =\n\t\t\t\t\"sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+\";\n\t\t\tlink.crossOrigin = \"anonymous\";\n\t\t\tdocument.head.appendChild(link);\n\n\t\t\t// Add font-display override for KaTeX fonts\n\t\t\tconst fontDisplayStyleId = \"katex-font-display-override\";\n\t\t\tif (!document.getElementById(fontDisplayStyleId)) {\n\t\t\t\tconst style = document.createElement(\"style\");\n\t\t\t\tstyle.id = fontDisplayStyleId;\n\t\t\t\tstyle.textContent = `\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Main';\n\t\t\t\t\t\tfont-style: normal;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Main-Regular.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Math';\n\t\t\t\t\t\tfont-style: italic;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Math-Italic.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t`;\n\t\t\t\tdocument.head.appendChild(style);\n\t\t\t}\n\t\t}\n\n\t\tPromise.all([import(\"remark-math\"), import(\"rehype-katex\")])\n\t\t\t.then(([remarkMathModule, rehypeKatexModule]) => {\n\t\t\t\tsetMathPlugins({\n\t\t\t\t\tremarkMath: remarkMathModule.default,\n\t\t\t\t\trehypeKatex: rehypeKatexModule.default,\n\t\t\t\t});\n\t\t\t})\n\t\t\t.catch((error) => {\n\t\t\t\tconsole.error(\"Failed to load math plugins:\", error);\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\tsetIsLoadingMath(false);\n\t\t\t});\n\t}, [hasMath, mathPlugins, isLoadingMath]);\n\n\t// Create anchor renderer with custom Link component\n\tconst AnchorRenderer = useMemo(() => {\n\t\treturn function AnchorRendererComponent(\n\t\t\tprops: React.AnchorHTMLAttributes,\n\t\t) {\n\t\t\tconst { href = \"\", children, className: anchorClassName, ...rest } = props;\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t};\n\t}, [LinkComponent]);\n\n\t// Create image renderer with custom Image component\n\tconst ImgRenderer = useMemo(() => {\n\t\treturn function ImgRendererComponent(\n\t\t\tprops: React.ImgHTMLAttributes,\n\t\t) {\n\t\t\tconst {\n\t\t\t\tsrc = \"\",\n\t\t\t\talt = \"\",\n\t\t\t\tclassName: imgClassName,\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t\tstyle,\n\t\t\t\t...rest\n\t\t\t} = props;\n\n\t\t\tconst imageProps: React.ComponentProps = {\n\t\t\t\tsrc,\n\t\t\t\talt,\n\t\t\t\tclassName: imgClassName as string,\n\t\t\t\tstyle,\n\t\t\t\t...rest,\n\t\t\t};\n\n\t\t\tif (width != null && height != null) {\n\t\t\t\timageProps.width = width as number;\n\t\t\t\timageProps.height = height as number;\n\t\t\t} else {\n\t\t\t\treturn (\n\t\t\t\t\t\n\t\t\t\t\t\t \n\t\t\t\t\t \n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn ;\n\t\t};\n\t}, [ImageComponent]);\n\n\tconst components = useMemo(() => {\n\t\treturn {\n\t\t\tp: ({ node, children, ...props }) => (\n\t\t\t\t<>\n\t\t\t\t\t{children}
\n\t\t\t\t\t{node?.data && \"btstIntroduction\" in node.data && node.data.btstIntroduction === true ? afterFirstParagraph : null}\n\t\t\t\t>\n\t\t\t),\n\t\t\ta: AnchorRenderer,\n\t\t\timg: ImgRenderer,\n\t\t\tspan: ({ node: _node, className, ...props }) => (\n\t\t\t\t \n\t\t\t),\n\t\t\tcode: CodeRenderer,\n\t\t\tpre: PreRenderer,\n\t\t\th1: createHeadingRenderer(\"h1\", LinkComponent),\n\t\t\th2: createHeadingRenderer(\"h2\", LinkComponent),\n\t\t\th3: createHeadingRenderer(\"h3\", LinkComponent),\n\t\t\th4: createHeadingRenderer(\"h4\", LinkComponent),\n\t\t\th5: createHeadingRenderer(\"h5\", LinkComponent),\n\t\t\th6: createHeadingRenderer(\"h6\", LinkComponent),\n\t\t\tli: createTaskListItemRenderer(),\n\t\t};\n\t}, [AnchorRenderer, ImgRenderer, LinkComponent, afterFirstParagraph]);\n\n\tconst remarkPlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [remarkGfm];\n\t\tif (hasMath && mathPlugins?.remarkMath) {\n\t\t\tplugins.push(mathPlugins.remarkMath);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst rehypePlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [rehypeRaw, rehypeHighlight, markIntroduction];\n\t\tif (hasMath && mathPlugins?.rehypeKatex) {\n\t\t\tplugins.push(mathPlugins.rehypeKatex);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst variantClass = variant === \"chat\" ? \"markdown-content-chat\" : \"\";\n\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\n\t\t\t\t\t\t{markdown}\n\t\t\t\t\t \n\t\t\t\t
\n\t\t\t
\n\t\t
\n\t);\n}\n",
"target": "src/components/ui/markdown-content.tsx"
},
{
diff --git a/packages/stack/src/plugins/blog/__tests__/introduction-slot.test.tsx b/packages/stack/src/plugins/blog/__tests__/introduction-slot.test.tsx
index 909918cad..f3046151d 100644
--- a/packages/stack/src/plugins/blog/__tests__/introduction-slot.test.tsx
+++ b/packages/stack/src/plugins/blog/__tests__/introduction-slot.test.tsx
@@ -1,8 +1,40 @@
+// @vitest-environment jsdom
+import { act } from "react";
+import { createRoot } from "react-dom/client";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it } from "vitest";
import { MarkdownContent } from "@workspace/ui/components/markdown-content";
+(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
+
describe("Markdown introduction slot", () => {
+ it("updates the introduction when slot props change without remounting", async () => {
+ const container = document.createElement("div");
+ const root = createRoot(container);
+ try {
+ await act(async () => {
+ root.render(
+ First selection}
+ />,
+ );
+ });
+ expect(container.textContent).toContain("First selection");
+ await act(async () => {
+ root.render(
+ Updated selection}
+ />,
+ );
+ });
+ expect(container.textContent).toContain("Updated selection");
+ expect(container.textContent).not.toContain("First selection");
+ } finally {
+ await act(async () => root.unmount());
+ }
+ });
it("follows the first top-level paragraph without splitting references or footnotes", () => {
const html = renderToStaticMarkup(
{
const plugins: unknown[] = [remarkGfm];
From 7c9323477a04d798086bf873892c2bb7cbf2e227 Mon Sep 17 00:00:00 2001
From: olliethedev <5933733+olliethedev@users.noreply.github.com>
Date: Mon, 19 Oct 2026 11:44:43 -0400
Subject: [PATCH 3/6] fix(blog): retain slot state and focus when article props
refresh
---
packages/stack/registry/btst-ai-chat.json | 2 +-
packages/stack/registry/btst-blog.json | 2 +-
.../blog/__tests__/introduction-slot.test.tsx | 26 ++++++++--
.../ui/src/components/markdown-content.tsx | 49 +++++++++++++------
4 files changed, 57 insertions(+), 22 deletions(-)
diff --git a/packages/stack/registry/btst-ai-chat.json b/packages/stack/registry/btst-ai-chat.json
index 9a93c4dbd..1f7e2b049 100644
--- a/packages/stack/registry/btst-ai-chat.json
+++ b/packages/stack/registry/btst-ai-chat.json
@@ -182,7 +182,7 @@
{
"path": "ui/components/markdown-content.tsx",
"type": "registry:component",
- "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport {\n\tcreateElement,\n\tisValidElement,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n\tuseEffect,\n} from \"react\";\nimport type {\n\tComponentPropsWithoutRef,\n\tComponentType,\n\tMouseEventHandler,\n\tReactNode,\n} from \"react\";\nimport ReactMarkdown from \"react-markdown\";\nimport type { Components } from \"react-markdown\";\nimport rehypeHighlight from \"rehype-highlight\";\nimport rehypeRaw from \"rehype-raw\";\nimport remarkGfm from \"remark-gfm\";\nimport { CopyIcon, CheckIcon } from \"lucide-react\";\n\n// Default Link component\nconst DefaultLink = (props: React.ComponentProps<\"a\">) => {\n\treturn ;\n};\n\n// Default Image component\nconst DefaultImage = (props: React.ImgHTMLAttributes) => {\n\treturn ;\n};\n\n// Utility to detect if markdown contains math syntax\nfunction containsMath(markdown: string): boolean {\n\treturn (\n\t\t/\\$\\$[\\s\\S]+?\\$\\$/.test(markdown) || // Display math\n\t\t/(?;\n\t\treturn getNodeText(props.children as ReactNode);\n\t}\n\treturn \"\";\n}\n\n// Deterministic hash function for generating consistent IDs\nfunction simpleHash(str: string): string {\n\tlet hash = 0;\n\tfor (let i = 0; i < str.length; i++) {\n\t\tconst char = str.charCodeAt(i);\n\t\thash = (hash << 5) - hash + char;\n\t\thash = hash & hash;\n\t}\n\treturn Math.abs(hash).toString(36).slice(0, 6);\n}\n\nfunction isCheckboxElement(\n\tnode: ReactNode,\n): node is ReturnType {\n\treturn (\n\t\tisValidElement(node) &&\n\t\t(node.type as unknown) === \"input\" &&\n\t\t(node.props as { type?: string }).type === \"checkbox\"\n\t);\n}\n\nfunction createTaskListItemRenderer() {\n\treturn function LiRenderer(\n\t\tprops: React.LiHTMLAttributes & { children?: ReactNode },\n\t) {\n\t\tconst { className, children, ...rest } = props;\n\t\tconst isTaskItem = (className ?? \"\").split(\" \").includes(\"task-list-item\");\n\t\tif (!isTaskItem) {\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t}\n\n\t\tconst childArray = Array.isArray(children) ? children : [children];\n\t\tconst checkboxNode = childArray.find(isCheckboxElement);\n\t\tconst nonCheckboxChildren = childArray.filter((c) => !isCheckboxElement(c));\n\n\t\tconst labelText = getNodeText(nonCheckboxChildren as unknown as ReactNode);\n\t\tconst baseId = slugify(labelText || \"task-item\");\n\t\tconst hashSuffix = simpleHash(labelText || \"task-item\");\n\t\tconst uniqueId = `${baseId}-${hashSuffix}`;\n\n\t\treturn (\n\t\t\t\n\t\t\t\t{checkboxNode\n\t\t\t\t\t? createElement(checkboxNode.type, {\n\t\t\t\t\t\t\t...checkboxNode.props,\n\t\t\t\t\t\t\tid: uniqueId,\n\t\t\t\t\t\t\t\"aria-label\": labelText || \"Task item\",\n\t\t\t\t\t\t})\n\t\t\t\t\t: null}\n\t\t\t\t\n\t\t\t\t\t{nonCheckboxChildren as unknown as ReactNode}\n\t\t\t\t \n\t\t\t \n\t\t);\n\t};\n}\n\ntype HeadingTag = \"h1\" | \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\";\nfunction createHeadingRenderer(tag: T, LinkComponent: NonNullable) {\n\treturn function HeadingRenderer(props: ComponentPropsWithoutRef) {\n\t\tconst { children, ...rest } = props as { children: ReactNode };\n\t\tconst text = getNodeText(children);\n\t\tconst id = slugify(text);\n\t\treturn createElement(\n\t\t\ttag,\n\t\t\t{ id, ...(rest as object) },\n\t\t\tchildren,\n\t\t\ttext ? (\n\t\t\t\t\n\t\t\t\t\t#\n\t\t\t\t \n\t\t\t) : null,\n\t\t);\n\t};\n}\n\ntype CodeProps = React.HTMLAttributes & {\n\tinline?: boolean;\n\tnode?: unknown;\n};\n\nfunction CodeRenderer({ inline, className, children, ...rest }: CodeProps) {\n\tconst hasLanguage = /language-([a-z0-9-]+)/i.test(className ?? \"\");\n\tconst isInline = inline ?? !hasLanguage;\n\tif (isInline) {\n\t\treturn (\n\t\t\t\n\t\t\t\t{children}\n\t\t\t\n\t\t);\n\t}\n\treturn (\n\t\t\n\t\t\t{children}\n\t\t\n\t);\n}\n\nfunction PreRenderer(props: React.HTMLAttributes) {\n\tconst { children, ...rest } = props;\n\tconst child = Array.isArray(children) ? children[0] : children;\n\tlet language: string | undefined;\n\tif (isValidElement(child)) {\n\t\tconst className = (child.props as { className?: string }).className;\n\t\tconst match = /language-([a-z0-9-]+)/i.exec(className ?? \"\");\n\t\tlanguage = match?.[1];\n\t}\n\tconst label = (language ?? \"text\").toUpperCase();\n\n\tconst preRef = useRef(null);\n\tconst [copied, setCopied] = useState(false);\n\tconst resetTimerRef = useRef(null);\n\n\tlet codeText = \"\";\n\tif (isValidElement(child)) {\n\t\tconst childProps = child.props as { children?: ReactNode };\n\t\tcodeText = getNodeText(childProps.children as ReactNode);\n\t}\n\tconst normalized = codeText.endsWith(\"\\n\") ? codeText.slice(0, -1) : codeText;\n\tconst lineCount = Math.max(1, normalized.split(\"\\n\").length);\n\tconst digitCount = String(lineCount).length;\n\t\n\tconst onCopy: MouseEventHandler = async (e) => {\n\t\te.preventDefault();\n\t\te.stopPropagation();\n\t\ttry {\n\t\t\tconst text = preRef.current?.textContent ?? \"\";\n\t\t\tif (\n\t\t\t\ttext &&\n\t\t\t\ttypeof navigator !== \"undefined\" &&\n\t\t\t\tnavigator.clipboard?.writeText\n\t\t\t) {\n\t\t\t\tawait navigator.clipboard.writeText(text);\n\t\t\t\tsetCopied(true);\n\t\t\t\tif (resetTimerRef.current) window.clearTimeout(resetTimerRef.current);\n\t\t\t\tresetTimerRef.current = window.setTimeout(() => {\n\t\t\t\t\tsetCopied(false);\n\t\t\t\t\tresetTimerRef.current = null;\n\t\t\t\t}, 2000);\n\t\t\t}\n\t\t} catch {}\n\t};\n\t\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t{label} \n\t\t\t\t\n\t\t\t\t\t{copied ? : }\n\t\t\t\t \n\t\t\t
\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t{Array.from({ length: lineCount }).map((_, idx) => (\n\t\t\t\t\t\t{idx + 1} \n\t\t\t\t\t))}\n\t\t\t\t \n\t\t\t\t
\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t
\n\t\t
\n\t);\n}\n\nfunction markIntroduction() {\n\treturn (tree: { children: { type: string; tagName?: string; data?: Record }[] }) => {\n\t\tconst paragraph = tree.children.find(node => node.type === \"element\" && node.tagName === \"p\");\n\t\tif (paragraph) paragraph.data = { ...paragraph.data, btstIntroduction: true };\n\t};\n}\n\nexport type MarkdownContentProps = {\n\t/** The markdown string to render */\n\tmarkdown: string;\n\t/** Content inserted after the first top-level paragraph. Omitted when there is no paragraph. */\n\tafterFirstParagraph?: ReactNode;\n\t/** Additional CSS class name */\n\tclassName?: string;\n\t/** Variant for different use cases - 'default' for full blog posts, 'chat' for compact chat messages */\n\tvariant?: \"default\" | \"chat\";\n\t/** Custom Link component for navigation */\n\tLinkComponent?: ComponentType & Record>;\n\t/** Custom Image component for images */\n\tImageComponent?: ComponentType & Record>;\n};\n\nexport function MarkdownContent({\n\tmarkdown,\n\tafterFirstParagraph,\n\tclassName,\n\tvariant = \"default\",\n\tLinkComponent = DefaultLink,\n\tImageComponent = DefaultImage,\n}: MarkdownContentProps) {\n\tconst [mathPlugins, setMathPlugins] = useState<{\n\t\tremarkMath: unknown;\n\t\trehypeKatex: unknown;\n\t} | null>(null);\n\tconst [isLoadingMath, setIsLoadingMath] = useState(false);\n\n\tconst hasMath = useMemo(() => containsMath(markdown), [markdown]);\n\n\t// Dynamically load math plugins and CSS only if needed\n\tuseEffect(() => {\n\t\tif (!hasMath || mathPlugins || isLoadingMath) return;\n\n\t\tsetIsLoadingMath(true);\n\n\t\t// Dynamically inject KaTeX CSS into the document\n\t\tconst katexCSSId = \"katex-css\";\n\t\tif (typeof document !== \"undefined\" && !document.getElementById(katexCSSId)) {\n\t\t\tconst link = document.createElement(\"link\");\n\t\t\tlink.id = katexCSSId;\n\t\t\tlink.rel = \"stylesheet\";\n\t\t\tlink.href =\n\t\t\t\t\"https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css\";\n\t\t\tlink.integrity =\n\t\t\t\t\"sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+\";\n\t\t\tlink.crossOrigin = \"anonymous\";\n\t\t\tdocument.head.appendChild(link);\n\n\t\t\t// Add font-display override for KaTeX fonts\n\t\t\tconst fontDisplayStyleId = \"katex-font-display-override\";\n\t\t\tif (!document.getElementById(fontDisplayStyleId)) {\n\t\t\t\tconst style = document.createElement(\"style\");\n\t\t\t\tstyle.id = fontDisplayStyleId;\n\t\t\t\tstyle.textContent = `\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Main';\n\t\t\t\t\t\tfont-style: normal;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Main-Regular.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Math';\n\t\t\t\t\t\tfont-style: italic;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Math-Italic.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t`;\n\t\t\t\tdocument.head.appendChild(style);\n\t\t\t}\n\t\t}\n\n\t\tPromise.all([import(\"remark-math\"), import(\"rehype-katex\")])\n\t\t\t.then(([remarkMathModule, rehypeKatexModule]) => {\n\t\t\t\tsetMathPlugins({\n\t\t\t\t\tremarkMath: remarkMathModule.default,\n\t\t\t\t\trehypeKatex: rehypeKatexModule.default,\n\t\t\t\t});\n\t\t\t})\n\t\t\t.catch((error) => {\n\t\t\t\tconsole.error(\"Failed to load math plugins:\", error);\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\tsetIsLoadingMath(false);\n\t\t\t});\n\t}, [hasMath, mathPlugins, isLoadingMath]);\n\n\t// Create anchor renderer with custom Link component\n\tconst AnchorRenderer = useMemo(() => {\n\t\treturn function AnchorRendererComponent(\n\t\t\tprops: React.AnchorHTMLAttributes,\n\t\t) {\n\t\t\tconst { href = \"\", children, className: anchorClassName, ...rest } = props;\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t};\n\t}, [LinkComponent]);\n\n\t// Create image renderer with custom Image component\n\tconst ImgRenderer = useMemo(() => {\n\t\treturn function ImgRendererComponent(\n\t\t\tprops: React.ImgHTMLAttributes,\n\t\t) {\n\t\t\tconst {\n\t\t\t\tsrc = \"\",\n\t\t\t\talt = \"\",\n\t\t\t\tclassName: imgClassName,\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t\tstyle,\n\t\t\t\t...rest\n\t\t\t} = props;\n\n\t\t\tconst imageProps: React.ComponentProps = {\n\t\t\t\tsrc,\n\t\t\t\talt,\n\t\t\t\tclassName: imgClassName as string,\n\t\t\t\tstyle,\n\t\t\t\t...rest,\n\t\t\t};\n\n\t\t\tif (width != null && height != null) {\n\t\t\t\timageProps.width = width as number;\n\t\t\t\timageProps.height = height as number;\n\t\t\t} else {\n\t\t\t\treturn (\n\t\t\t\t\t\n\t\t\t\t\t\t \n\t\t\t\t\t \n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn ;\n\t\t};\n\t}, [ImageComponent]);\n\n\tconst components = useMemo(() => {\n\t\treturn {\n\t\t\tp: ({ node, children, ...props }) => (\n\t\t\t\t<>\n\t\t\t\t\t{children}
\n\t\t\t\t\t{node?.data && \"btstIntroduction\" in node.data && node.data.btstIntroduction === true ? afterFirstParagraph : null}\n\t\t\t\t>\n\t\t\t),\n\t\t\ta: AnchorRenderer,\n\t\t\timg: ImgRenderer,\n\t\t\tspan: ({ node: _node, className, ...props }) => (\n\t\t\t\t \n\t\t\t),\n\t\t\tcode: CodeRenderer,\n\t\t\tpre: PreRenderer,\n\t\t\th1: createHeadingRenderer(\"h1\", LinkComponent),\n\t\t\th2: createHeadingRenderer(\"h2\", LinkComponent),\n\t\t\th3: createHeadingRenderer(\"h3\", LinkComponent),\n\t\t\th4: createHeadingRenderer(\"h4\", LinkComponent),\n\t\t\th5: createHeadingRenderer(\"h5\", LinkComponent),\n\t\t\th6: createHeadingRenderer(\"h6\", LinkComponent),\n\t\t\tli: createTaskListItemRenderer(),\n\t\t};\n\t}, [AnchorRenderer, ImgRenderer, LinkComponent, afterFirstParagraph]);\n\n\tconst remarkPlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [remarkGfm];\n\t\tif (hasMath && mathPlugins?.remarkMath) {\n\t\t\tplugins.push(mathPlugins.remarkMath);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst rehypePlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [rehypeRaw, rehypeHighlight, markIntroduction];\n\t\tif (hasMath && mathPlugins?.rehypeKatex) {\n\t\t\tplugins.push(mathPlugins.rehypeKatex);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst variantClass = variant === \"chat\" ? \"markdown-content-chat\" : \"\";\n\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\n\t\t\t\t\t\t{markdown}\n\t\t\t\t\t \n\t\t\t\t
\n\t\t\t
\n\t\t
\n\t);\n}\n",
+ "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport {\n\tcreateElement,\n\tcreateContext,\n\tuseContext,\n\tisValidElement,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n\tuseEffect,\n} from \"react\";\nimport type {\n\tComponentPropsWithoutRef,\n\tComponentType,\n\tMouseEventHandler,\n\tReactNode,\n} from \"react\";\nimport ReactMarkdown from \"react-markdown\";\nimport type { Components, ExtraProps } from \"react-markdown\";\nimport rehypeHighlight from \"rehype-highlight\";\nimport rehypeRaw from \"rehype-raw\";\nimport remarkGfm from \"remark-gfm\";\nimport { CopyIcon, CheckIcon } from \"lucide-react\";\n\nconst IntroductionSlotContext = createContext(null);\n\nfunction ParagraphRenderer({\n\tnode,\n\tchildren,\n\t...props\n}: ComponentPropsWithoutRef<\"p\"> & ExtraProps) {\n\tconst slot = useContext(IntroductionSlotContext);\n\treturn (\n\t\t<>\n\t\t\t{children}
\n\t\t\t{node?.data &&\n\t\t\t\"btstIntroduction\" in node.data &&\n\t\t\tnode.data.btstIntroduction === true\n\t\t\t\t? slot\n\t\t\t\t: null}\n\t\t>\n\t);\n}\n\n// Default Link component\nconst DefaultLink = (props: React.ComponentProps<\"a\">) => {\n\treturn ;\n};\n\n// Default Image component\nconst DefaultImage = (props: React.ImgHTMLAttributes) => {\n\treturn ;\n};\n\n// Utility to detect if markdown contains math syntax\nfunction containsMath(markdown: string): boolean {\n\treturn (\n\t\t/\\$\\$[\\s\\S]+?\\$\\$/.test(markdown) || // Display math\n\t\t/(?;\n\t\treturn getNodeText(props.children as ReactNode);\n\t}\n\treturn \"\";\n}\n\n// Deterministic hash function for generating consistent IDs\nfunction simpleHash(str: string): string {\n\tlet hash = 0;\n\tfor (let i = 0; i < str.length; i++) {\n\t\tconst char = str.charCodeAt(i);\n\t\thash = (hash << 5) - hash + char;\n\t\thash = hash & hash;\n\t}\n\treturn Math.abs(hash).toString(36).slice(0, 6);\n}\n\nfunction isCheckboxElement(\n\tnode: ReactNode,\n): node is ReturnType {\n\treturn (\n\t\tisValidElement(node) &&\n\t\t(node.type as unknown) === \"input\" &&\n\t\t(node.props as { type?: string }).type === \"checkbox\"\n\t);\n}\n\nfunction createTaskListItemRenderer() {\n\treturn function LiRenderer(\n\t\tprops: React.LiHTMLAttributes & { children?: ReactNode },\n\t) {\n\t\tconst { className, children, ...rest } = props;\n\t\tconst isTaskItem = (className ?? \"\").split(\" \").includes(\"task-list-item\");\n\t\tif (!isTaskItem) {\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t}\n\n\t\tconst childArray = Array.isArray(children) ? children : [children];\n\t\tconst checkboxNode = childArray.find(isCheckboxElement);\n\t\tconst nonCheckboxChildren = childArray.filter((c) => !isCheckboxElement(c));\n\n\t\tconst labelText = getNodeText(nonCheckboxChildren as unknown as ReactNode);\n\t\tconst baseId = slugify(labelText || \"task-item\");\n\t\tconst hashSuffix = simpleHash(labelText || \"task-item\");\n\t\tconst uniqueId = `${baseId}-${hashSuffix}`;\n\n\t\treturn (\n\t\t\t\n\t\t\t\t{checkboxNode\n\t\t\t\t\t? createElement(checkboxNode.type, {\n\t\t\t\t\t\t\t...checkboxNode.props,\n\t\t\t\t\t\t\tid: uniqueId,\n\t\t\t\t\t\t\t\"aria-label\": labelText || \"Task item\",\n\t\t\t\t\t\t})\n\t\t\t\t\t: null}\n\t\t\t\t\n\t\t\t\t\t{nonCheckboxChildren as unknown as ReactNode}\n\t\t\t\t \n\t\t\t \n\t\t);\n\t};\n}\n\ntype HeadingTag = \"h1\" | \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\";\nfunction createHeadingRenderer(tag: T, LinkComponent: NonNullable) {\n\treturn function HeadingRenderer(props: ComponentPropsWithoutRef) {\n\t\tconst { children, ...rest } = props as { children: ReactNode };\n\t\tconst text = getNodeText(children);\n\t\tconst id = slugify(text);\n\t\treturn createElement(\n\t\t\ttag,\n\t\t\t{ id, ...(rest as object) },\n\t\t\tchildren,\n\t\t\ttext ? (\n\t\t\t\t\n\t\t\t\t\t#\n\t\t\t\t \n\t\t\t) : null,\n\t\t);\n\t};\n}\n\ntype CodeProps = React.HTMLAttributes & {\n\tinline?: boolean;\n\tnode?: unknown;\n};\n\nfunction CodeRenderer({ inline, className, children, ...rest }: CodeProps) {\n\tconst hasLanguage = /language-([a-z0-9-]+)/i.test(className ?? \"\");\n\tconst isInline = inline ?? !hasLanguage;\n\tif (isInline) {\n\t\treturn (\n\t\t\t\n\t\t\t\t{children}\n\t\t\t\n\t\t);\n\t}\n\treturn (\n\t\t\n\t\t\t{children}\n\t\t\n\t);\n}\n\nfunction PreRenderer(props: React.HTMLAttributes) {\n\tconst { children, ...rest } = props;\n\tconst child = Array.isArray(children) ? children[0] : children;\n\tlet language: string | undefined;\n\tif (isValidElement(child)) {\n\t\tconst className = (child.props as { className?: string }).className;\n\t\tconst match = /language-([a-z0-9-]+)/i.exec(className ?? \"\");\n\t\tlanguage = match?.[1];\n\t}\n\tconst label = (language ?? \"text\").toUpperCase();\n\n\tconst preRef = useRef(null);\n\tconst [copied, setCopied] = useState(false);\n\tconst resetTimerRef = useRef(null);\n\n\tlet codeText = \"\";\n\tif (isValidElement(child)) {\n\t\tconst childProps = child.props as { children?: ReactNode };\n\t\tcodeText = getNodeText(childProps.children as ReactNode);\n\t}\n\tconst normalized = codeText.endsWith(\"\\n\") ? codeText.slice(0, -1) : codeText;\n\tconst lineCount = Math.max(1, normalized.split(\"\\n\").length);\n\tconst digitCount = String(lineCount).length;\n\t\n\tconst onCopy: MouseEventHandler = async (e) => {\n\t\te.preventDefault();\n\t\te.stopPropagation();\n\t\ttry {\n\t\t\tconst text = preRef.current?.textContent ?? \"\";\n\t\t\tif (\n\t\t\t\ttext &&\n\t\t\t\ttypeof navigator !== \"undefined\" &&\n\t\t\t\tnavigator.clipboard?.writeText\n\t\t\t) {\n\t\t\t\tawait navigator.clipboard.writeText(text);\n\t\t\t\tsetCopied(true);\n\t\t\t\tif (resetTimerRef.current) window.clearTimeout(resetTimerRef.current);\n\t\t\t\tresetTimerRef.current = window.setTimeout(() => {\n\t\t\t\t\tsetCopied(false);\n\t\t\t\t\tresetTimerRef.current = null;\n\t\t\t\t}, 2000);\n\t\t\t}\n\t\t} catch {}\n\t};\n\t\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t{label} \n\t\t\t\t\n\t\t\t\t\t{copied ? : }\n\t\t\t\t \n\t\t\t
\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t{Array.from({ length: lineCount }).map((_, idx) => (\n\t\t\t\t\t\t{idx + 1} \n\t\t\t\t\t))}\n\t\t\t\t \n\t\t\t\t
\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t
\n\t\t
\n\t);\n}\n\nfunction markIntroduction() {\n\treturn (tree: { children: { type: string; tagName?: string; data?: Record }[] }) => {\n\t\tconst paragraph = tree.children.find(node => node.type === \"element\" && node.tagName === \"p\");\n\t\tif (paragraph) paragraph.data = { ...paragraph.data, btstIntroduction: true };\n\t};\n}\n\nexport type MarkdownContentProps = {\n\t/** The markdown string to render */\n\tmarkdown: string;\n\t/** Content inserted after the first top-level paragraph. Omitted when there is no paragraph. */\n\tafterFirstParagraph?: ReactNode;\n\t/** Additional CSS class name */\n\tclassName?: string;\n\t/** Variant for different use cases - 'default' for full blog posts, 'chat' for compact chat messages */\n\tvariant?: \"default\" | \"chat\";\n\t/** Custom Link component for navigation */\n\tLinkComponent?: ComponentType & Record>;\n\t/** Custom Image component for images */\n\tImageComponent?: ComponentType & Record>;\n};\n\nexport function MarkdownContent({\n\tmarkdown,\n\tafterFirstParagraph,\n\tclassName,\n\tvariant = \"default\",\n\tLinkComponent = DefaultLink,\n\tImageComponent = DefaultImage,\n}: MarkdownContentProps) {\n\tconst [mathPlugins, setMathPlugins] = useState<{\n\t\tremarkMath: unknown;\n\t\trehypeKatex: unknown;\n\t} | null>(null);\n\tconst [isLoadingMath, setIsLoadingMath] = useState(false);\n\n\tconst hasMath = useMemo(() => containsMath(markdown), [markdown]);\n\n\t// Dynamically load math plugins and CSS only if needed\n\tuseEffect(() => {\n\t\tif (!hasMath || mathPlugins || isLoadingMath) return;\n\n\t\tsetIsLoadingMath(true);\n\n\t\t// Dynamically inject KaTeX CSS into the document\n\t\tconst katexCSSId = \"katex-css\";\n\t\tif (typeof document !== \"undefined\" && !document.getElementById(katexCSSId)) {\n\t\t\tconst link = document.createElement(\"link\");\n\t\t\tlink.id = katexCSSId;\n\t\t\tlink.rel = \"stylesheet\";\n\t\t\tlink.href =\n\t\t\t\t\"https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css\";\n\t\t\tlink.integrity =\n\t\t\t\t\"sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+\";\n\t\t\tlink.crossOrigin = \"anonymous\";\n\t\t\tdocument.head.appendChild(link);\n\n\t\t\t// Add font-display override for KaTeX fonts\n\t\t\tconst fontDisplayStyleId = \"katex-font-display-override\";\n\t\t\tif (!document.getElementById(fontDisplayStyleId)) {\n\t\t\t\tconst style = document.createElement(\"style\");\n\t\t\t\tstyle.id = fontDisplayStyleId;\n\t\t\t\tstyle.textContent = `\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Main';\n\t\t\t\t\t\tfont-style: normal;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Main-Regular.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Math';\n\t\t\t\t\t\tfont-style: italic;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Math-Italic.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t`;\n\t\t\t\tdocument.head.appendChild(style);\n\t\t\t}\n\t\t}\n\n\t\tPromise.all([import(\"remark-math\"), import(\"rehype-katex\")])\n\t\t\t.then(([remarkMathModule, rehypeKatexModule]) => {\n\t\t\t\tsetMathPlugins({\n\t\t\t\t\tremarkMath: remarkMathModule.default,\n\t\t\t\t\trehypeKatex: rehypeKatexModule.default,\n\t\t\t\t});\n\t\t\t})\n\t\t\t.catch((error) => {\n\t\t\t\tconsole.error(\"Failed to load math plugins:\", error);\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\tsetIsLoadingMath(false);\n\t\t\t});\n\t}, [hasMath, mathPlugins, isLoadingMath]);\n\n\t// Create anchor renderer with custom Link component\n\tconst AnchorRenderer = useMemo(() => {\n\t\treturn function AnchorRendererComponent(\n\t\t\tprops: React.AnchorHTMLAttributes,\n\t\t) {\n\t\t\tconst { href = \"\", children, className: anchorClassName, ...rest } = props;\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t};\n\t}, [LinkComponent]);\n\n\t// Create image renderer with custom Image component\n\tconst ImgRenderer = useMemo(() => {\n\t\treturn function ImgRendererComponent(\n\t\t\tprops: React.ImgHTMLAttributes,\n\t\t) {\n\t\t\tconst {\n\t\t\t\tsrc = \"\",\n\t\t\t\talt = \"\",\n\t\t\t\tclassName: imgClassName,\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t\tstyle,\n\t\t\t\t...rest\n\t\t\t} = props;\n\n\t\t\tconst imageProps: React.ComponentProps = {\n\t\t\t\tsrc,\n\t\t\t\talt,\n\t\t\t\tclassName: imgClassName as string,\n\t\t\t\tstyle,\n\t\t\t\t...rest,\n\t\t\t};\n\n\t\t\tif (width != null && height != null) {\n\t\t\t\timageProps.width = width as number;\n\t\t\t\timageProps.height = height as number;\n\t\t\t} else {\n\t\t\t\treturn (\n\t\t\t\t\t\n\t\t\t\t\t\t \n\t\t\t\t\t \n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn ;\n\t\t};\n\t}, [ImageComponent]);\n\n\tconst components = useMemo(() => {\n\t\treturn {\n\t\t\tp: ParagraphRenderer,\n\t\t\ta: AnchorRenderer,\n\t\t\timg: ImgRenderer,\n\t\t\tspan: ({ node: _node, className, ...props }) => (\n\t\t\t\t \n\t\t\t),\n\t\t\tcode: CodeRenderer,\n\t\t\tpre: PreRenderer,\n\t\t\th1: createHeadingRenderer(\"h1\", LinkComponent),\n\t\t\th2: createHeadingRenderer(\"h2\", LinkComponent),\n\t\t\th3: createHeadingRenderer(\"h3\", LinkComponent),\n\t\t\th4: createHeadingRenderer(\"h4\", LinkComponent),\n\t\t\th5: createHeadingRenderer(\"h5\", LinkComponent),\n\t\t\th6: createHeadingRenderer(\"h6\", LinkComponent),\n\t\t\tli: createTaskListItemRenderer(),\n\t\t};\n\t}, [AnchorRenderer, ImgRenderer, LinkComponent]);\n\n\tconst remarkPlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [remarkGfm];\n\t\tif (hasMath && mathPlugins?.remarkMath) {\n\t\t\tplugins.push(mathPlugins.remarkMath);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst rehypePlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [rehypeRaw, rehypeHighlight, markIntroduction];\n\t\tif (hasMath && mathPlugins?.rehypeKatex) {\n\t\t\tplugins.push(mathPlugins.rehypeKatex);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst variantClass = variant === \"chat\" ? \"markdown-content-chat\" : \"\";\n\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t{markdown}\n\t\t\t\t\t\t \n\t\t\t\t\t \n\t\t\t\t
\n\t\t\t
\n\t\t
\n\t);\n}\n",
"target": "src/components/ui/markdown-content.tsx"
},
{
diff --git a/packages/stack/registry/btst-blog.json b/packages/stack/registry/btst-blog.json
index cfe3e15b1..455fa87f6 100644
--- a/packages/stack/registry/btst-blog.json
+++ b/packages/stack/registry/btst-blog.json
@@ -430,7 +430,7 @@
{
"path": "ui/components/markdown-content.tsx",
"type": "registry:component",
- "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport {\n\tcreateElement,\n\tisValidElement,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n\tuseEffect,\n} from \"react\";\nimport type {\n\tComponentPropsWithoutRef,\n\tComponentType,\n\tMouseEventHandler,\n\tReactNode,\n} from \"react\";\nimport ReactMarkdown from \"react-markdown\";\nimport type { Components } from \"react-markdown\";\nimport rehypeHighlight from \"rehype-highlight\";\nimport rehypeRaw from \"rehype-raw\";\nimport remarkGfm from \"remark-gfm\";\nimport { CopyIcon, CheckIcon } from \"lucide-react\";\n\n// Default Link component\nconst DefaultLink = (props: React.ComponentProps<\"a\">) => {\n\treturn ;\n};\n\n// Default Image component\nconst DefaultImage = (props: React.ImgHTMLAttributes) => {\n\treturn ;\n};\n\n// Utility to detect if markdown contains math syntax\nfunction containsMath(markdown: string): boolean {\n\treturn (\n\t\t/\\$\\$[\\s\\S]+?\\$\\$/.test(markdown) || // Display math\n\t\t/(?;\n\t\treturn getNodeText(props.children as ReactNode);\n\t}\n\treturn \"\";\n}\n\n// Deterministic hash function for generating consistent IDs\nfunction simpleHash(str: string): string {\n\tlet hash = 0;\n\tfor (let i = 0; i < str.length; i++) {\n\t\tconst char = str.charCodeAt(i);\n\t\thash = (hash << 5) - hash + char;\n\t\thash = hash & hash;\n\t}\n\treturn Math.abs(hash).toString(36).slice(0, 6);\n}\n\nfunction isCheckboxElement(\n\tnode: ReactNode,\n): node is ReturnType {\n\treturn (\n\t\tisValidElement(node) &&\n\t\t(node.type as unknown) === \"input\" &&\n\t\t(node.props as { type?: string }).type === \"checkbox\"\n\t);\n}\n\nfunction createTaskListItemRenderer() {\n\treturn function LiRenderer(\n\t\tprops: React.LiHTMLAttributes & { children?: ReactNode },\n\t) {\n\t\tconst { className, children, ...rest } = props;\n\t\tconst isTaskItem = (className ?? \"\").split(\" \").includes(\"task-list-item\");\n\t\tif (!isTaskItem) {\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t}\n\n\t\tconst childArray = Array.isArray(children) ? children : [children];\n\t\tconst checkboxNode = childArray.find(isCheckboxElement);\n\t\tconst nonCheckboxChildren = childArray.filter((c) => !isCheckboxElement(c));\n\n\t\tconst labelText = getNodeText(nonCheckboxChildren as unknown as ReactNode);\n\t\tconst baseId = slugify(labelText || \"task-item\");\n\t\tconst hashSuffix = simpleHash(labelText || \"task-item\");\n\t\tconst uniqueId = `${baseId}-${hashSuffix}`;\n\n\t\treturn (\n\t\t\t\n\t\t\t\t{checkboxNode\n\t\t\t\t\t? createElement(checkboxNode.type, {\n\t\t\t\t\t\t\t...checkboxNode.props,\n\t\t\t\t\t\t\tid: uniqueId,\n\t\t\t\t\t\t\t\"aria-label\": labelText || \"Task item\",\n\t\t\t\t\t\t})\n\t\t\t\t\t: null}\n\t\t\t\t\n\t\t\t\t\t{nonCheckboxChildren as unknown as ReactNode}\n\t\t\t\t \n\t\t\t \n\t\t);\n\t};\n}\n\ntype HeadingTag = \"h1\" | \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\";\nfunction createHeadingRenderer(tag: T, LinkComponent: NonNullable) {\n\treturn function HeadingRenderer(props: ComponentPropsWithoutRef) {\n\t\tconst { children, ...rest } = props as { children: ReactNode };\n\t\tconst text = getNodeText(children);\n\t\tconst id = slugify(text);\n\t\treturn createElement(\n\t\t\ttag,\n\t\t\t{ id, ...(rest as object) },\n\t\t\tchildren,\n\t\t\ttext ? (\n\t\t\t\t\n\t\t\t\t\t#\n\t\t\t\t \n\t\t\t) : null,\n\t\t);\n\t};\n}\n\ntype CodeProps = React.HTMLAttributes & {\n\tinline?: boolean;\n\tnode?: unknown;\n};\n\nfunction CodeRenderer({ inline, className, children, ...rest }: CodeProps) {\n\tconst hasLanguage = /language-([a-z0-9-]+)/i.test(className ?? \"\");\n\tconst isInline = inline ?? !hasLanguage;\n\tif (isInline) {\n\t\treturn (\n\t\t\t\n\t\t\t\t{children}\n\t\t\t\n\t\t);\n\t}\n\treturn (\n\t\t\n\t\t\t{children}\n\t\t\n\t);\n}\n\nfunction PreRenderer(props: React.HTMLAttributes) {\n\tconst { children, ...rest } = props;\n\tconst child = Array.isArray(children) ? children[0] : children;\n\tlet language: string | undefined;\n\tif (isValidElement(child)) {\n\t\tconst className = (child.props as { className?: string }).className;\n\t\tconst match = /language-([a-z0-9-]+)/i.exec(className ?? \"\");\n\t\tlanguage = match?.[1];\n\t}\n\tconst label = (language ?? \"text\").toUpperCase();\n\n\tconst preRef = useRef(null);\n\tconst [copied, setCopied] = useState(false);\n\tconst resetTimerRef = useRef(null);\n\n\tlet codeText = \"\";\n\tif (isValidElement(child)) {\n\t\tconst childProps = child.props as { children?: ReactNode };\n\t\tcodeText = getNodeText(childProps.children as ReactNode);\n\t}\n\tconst normalized = codeText.endsWith(\"\\n\") ? codeText.slice(0, -1) : codeText;\n\tconst lineCount = Math.max(1, normalized.split(\"\\n\").length);\n\tconst digitCount = String(lineCount).length;\n\t\n\tconst onCopy: MouseEventHandler = async (e) => {\n\t\te.preventDefault();\n\t\te.stopPropagation();\n\t\ttry {\n\t\t\tconst text = preRef.current?.textContent ?? \"\";\n\t\t\tif (\n\t\t\t\ttext &&\n\t\t\t\ttypeof navigator !== \"undefined\" &&\n\t\t\t\tnavigator.clipboard?.writeText\n\t\t\t) {\n\t\t\t\tawait navigator.clipboard.writeText(text);\n\t\t\t\tsetCopied(true);\n\t\t\t\tif (resetTimerRef.current) window.clearTimeout(resetTimerRef.current);\n\t\t\t\tresetTimerRef.current = window.setTimeout(() => {\n\t\t\t\t\tsetCopied(false);\n\t\t\t\t\tresetTimerRef.current = null;\n\t\t\t\t}, 2000);\n\t\t\t}\n\t\t} catch {}\n\t};\n\t\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t{label} \n\t\t\t\t\n\t\t\t\t\t{copied ? : }\n\t\t\t\t \n\t\t\t
\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t{Array.from({ length: lineCount }).map((_, idx) => (\n\t\t\t\t\t\t{idx + 1} \n\t\t\t\t\t))}\n\t\t\t\t \n\t\t\t\t
\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t
\n\t\t
\n\t);\n}\n\nfunction markIntroduction() {\n\treturn (tree: { children: { type: string; tagName?: string; data?: Record }[] }) => {\n\t\tconst paragraph = tree.children.find(node => node.type === \"element\" && node.tagName === \"p\");\n\t\tif (paragraph) paragraph.data = { ...paragraph.data, btstIntroduction: true };\n\t};\n}\n\nexport type MarkdownContentProps = {\n\t/** The markdown string to render */\n\tmarkdown: string;\n\t/** Content inserted after the first top-level paragraph. Omitted when there is no paragraph. */\n\tafterFirstParagraph?: ReactNode;\n\t/** Additional CSS class name */\n\tclassName?: string;\n\t/** Variant for different use cases - 'default' for full blog posts, 'chat' for compact chat messages */\n\tvariant?: \"default\" | \"chat\";\n\t/** Custom Link component for navigation */\n\tLinkComponent?: ComponentType & Record>;\n\t/** Custom Image component for images */\n\tImageComponent?: ComponentType & Record>;\n};\n\nexport function MarkdownContent({\n\tmarkdown,\n\tafterFirstParagraph,\n\tclassName,\n\tvariant = \"default\",\n\tLinkComponent = DefaultLink,\n\tImageComponent = DefaultImage,\n}: MarkdownContentProps) {\n\tconst [mathPlugins, setMathPlugins] = useState<{\n\t\tremarkMath: unknown;\n\t\trehypeKatex: unknown;\n\t} | null>(null);\n\tconst [isLoadingMath, setIsLoadingMath] = useState(false);\n\n\tconst hasMath = useMemo(() => containsMath(markdown), [markdown]);\n\n\t// Dynamically load math plugins and CSS only if needed\n\tuseEffect(() => {\n\t\tif (!hasMath || mathPlugins || isLoadingMath) return;\n\n\t\tsetIsLoadingMath(true);\n\n\t\t// Dynamically inject KaTeX CSS into the document\n\t\tconst katexCSSId = \"katex-css\";\n\t\tif (typeof document !== \"undefined\" && !document.getElementById(katexCSSId)) {\n\t\t\tconst link = document.createElement(\"link\");\n\t\t\tlink.id = katexCSSId;\n\t\t\tlink.rel = \"stylesheet\";\n\t\t\tlink.href =\n\t\t\t\t\"https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css\";\n\t\t\tlink.integrity =\n\t\t\t\t\"sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+\";\n\t\t\tlink.crossOrigin = \"anonymous\";\n\t\t\tdocument.head.appendChild(link);\n\n\t\t\t// Add font-display override for KaTeX fonts\n\t\t\tconst fontDisplayStyleId = \"katex-font-display-override\";\n\t\t\tif (!document.getElementById(fontDisplayStyleId)) {\n\t\t\t\tconst style = document.createElement(\"style\");\n\t\t\t\tstyle.id = fontDisplayStyleId;\n\t\t\t\tstyle.textContent = `\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Main';\n\t\t\t\t\t\tfont-style: normal;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Main-Regular.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t\t@font-face {\n\t\t\t\t\t\tfont-family: 'KaTeX_Math';\n\t\t\t\t\t\tfont-style: italic;\n\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\tfont-display: swap;\n\t\t\t\t\t\tsrc: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Math-Italic.woff2') format('woff2');\n\t\t\t\t\t}\n\t\t\t\t`;\n\t\t\t\tdocument.head.appendChild(style);\n\t\t\t}\n\t\t}\n\n\t\tPromise.all([import(\"remark-math\"), import(\"rehype-katex\")])\n\t\t\t.then(([remarkMathModule, rehypeKatexModule]) => {\n\t\t\t\tsetMathPlugins({\n\t\t\t\t\tremarkMath: remarkMathModule.default,\n\t\t\t\t\trehypeKatex: rehypeKatexModule.default,\n\t\t\t\t});\n\t\t\t})\n\t\t\t.catch((error) => {\n\t\t\t\tconsole.error(\"Failed to load math plugins:\", error);\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\tsetIsLoadingMath(false);\n\t\t\t});\n\t}, [hasMath, mathPlugins, isLoadingMath]);\n\n\t// Create anchor renderer with custom Link component\n\tconst AnchorRenderer = useMemo(() => {\n\t\treturn function AnchorRendererComponent(\n\t\t\tprops: React.AnchorHTMLAttributes,\n\t\t) {\n\t\t\tconst { href = \"\", children, className: anchorClassName, ...rest } = props;\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t};\n\t}, [LinkComponent]);\n\n\t// Create image renderer with custom Image component\n\tconst ImgRenderer = useMemo(() => {\n\t\treturn function ImgRendererComponent(\n\t\t\tprops: React.ImgHTMLAttributes,\n\t\t) {\n\t\t\tconst {\n\t\t\t\tsrc = \"\",\n\t\t\t\talt = \"\",\n\t\t\t\tclassName: imgClassName,\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t\tstyle,\n\t\t\t\t...rest\n\t\t\t} = props;\n\n\t\t\tconst imageProps: React.ComponentProps = {\n\t\t\t\tsrc,\n\t\t\t\talt,\n\t\t\t\tclassName: imgClassName as string,\n\t\t\t\tstyle,\n\t\t\t\t...rest,\n\t\t\t};\n\n\t\t\tif (width != null && height != null) {\n\t\t\t\timageProps.width = width as number;\n\t\t\t\timageProps.height = height as number;\n\t\t\t} else {\n\t\t\t\treturn (\n\t\t\t\t\t\n\t\t\t\t\t\t \n\t\t\t\t\t \n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn ;\n\t\t};\n\t}, [ImageComponent]);\n\n\tconst components = useMemo(() => {\n\t\treturn {\n\t\t\tp: ({ node, children, ...props }) => (\n\t\t\t\t<>\n\t\t\t\t\t{children}
\n\t\t\t\t\t{node?.data && \"btstIntroduction\" in node.data && node.data.btstIntroduction === true ? afterFirstParagraph : null}\n\t\t\t\t>\n\t\t\t),\n\t\t\ta: AnchorRenderer,\n\t\t\timg: ImgRenderer,\n\t\t\tspan: ({ node: _node, className, ...props }) => (\n\t\t\t\t \n\t\t\t),\n\t\t\tcode: CodeRenderer,\n\t\t\tpre: PreRenderer,\n\t\t\th1: createHeadingRenderer(\"h1\", LinkComponent),\n\t\t\th2: createHeadingRenderer(\"h2\", LinkComponent),\n\t\t\th3: createHeadingRenderer(\"h3\", LinkComponent),\n\t\t\th4: createHeadingRenderer(\"h4\", LinkComponent),\n\t\t\th5: createHeadingRenderer(\"h5\", LinkComponent),\n\t\t\th6: createHeadingRenderer(\"h6\", LinkComponent),\n\t\t\tli: createTaskListItemRenderer(),\n\t\t};\n\t}, [AnchorRenderer, ImgRenderer, LinkComponent, afterFirstParagraph]);\n\n\tconst remarkPlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [remarkGfm];\n\t\tif (hasMath && mathPlugins?.remarkMath) {\n\t\t\tplugins.push(mathPlugins.remarkMath);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst rehypePlugins = useMemo(() => {\n\t\tconst plugins: unknown[] = [rehypeRaw, rehypeHighlight, markIntroduction];\n\t\tif (hasMath && mathPlugins?.rehypeKatex) {\n\t\t\tplugins.push(mathPlugins.rehypeKatex);\n\t\t}\n\t\treturn plugins as never;\n\t}, [hasMath, mathPlugins]);\n\n\tconst variantClass = variant === \"chat\" ? \"markdown-content-chat\" : \"\";\n\n\treturn (\n\t\t\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\n\t\t\t\t\t\t{markdown}\n\t\t\t\t\t \n\t\t\t\t
\n\t\t\t
\n\t\t
\n\t);\n}\n",
+ "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport {\n\tcreateElement,\n\tcreateContext,\n\tuseContext,\n\tisValidElement,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n\tuseEffect,\n} from \"react\";\nimport type {\n\tComponentPropsWithoutRef,\n\tComponentType,\n\tMouseEventHandler,\n\tReactNode,\n} from \"react\";\nimport ReactMarkdown from \"react-markdown\";\nimport type { Components, ExtraProps } from \"react-markdown\";\nimport rehypeHighlight from \"rehype-highlight\";\nimport rehypeRaw from \"rehype-raw\";\nimport remarkGfm from \"remark-gfm\";\nimport { CopyIcon, CheckIcon } from \"lucide-react\";\n\nconst IntroductionSlotContext = createContext(null);\n\nfunction ParagraphRenderer({\n\tnode,\n\tchildren,\n\t...props\n}: ComponentPropsWithoutRef<\"p\"> & ExtraProps) {\n\tconst slot = useContext(IntroductionSlotContext);\n\treturn (\n\t\t<>\n\t\t\t{children}
\n\t\t\t{node?.data &&\n\t\t\t\"btstIntroduction\" in node.data &&\n\t\t\tnode.data.btstIntroduction === true\n\t\t\t\t? slot\n\t\t\t\t: null}\n\t\t>\n\t);\n}\n\n// Default Link component\nconst DefaultLink = (props: React.ComponentProps<\"a\">) => {\n\treturn ;\n};\n\n// Default Image component\nconst DefaultImage = (props: React.ImgHTMLAttributes) => {\n\treturn ;\n};\n\n// Utility to detect if markdown contains math syntax\nfunction containsMath(markdown: string): boolean {\n\treturn (\n\t\t/\\$\\$[\\s\\S]+?\\$\\$/.test(markdown) || // Display math\n\t\t/(?;\n\t\treturn getNodeText(props.children as ReactNode);\n\t}\n\treturn \"\";\n}\n\n// Deterministic hash function for generating consistent IDs\nfunction simpleHash(str: string): string {\n\tlet hash = 0;\n\tfor (let i = 0; i < str.length; i++) {\n\t\tconst char = str.charCodeAt(i);\n\t\thash = (hash << 5) - hash + char;\n\t\thash = hash & hash;\n\t}\n\treturn Math.abs(hash).toString(36).slice(0, 6);\n}\n\nfunction isCheckboxElement(\n\tnode: ReactNode,\n): node is ReturnType {\n\treturn (\n\t\tisValidElement(node) &&\n\t\t(node.type as unknown) === \"input\" &&\n\t\t(node.props as { type?: string }).type === \"checkbox\"\n\t);\n}\n\nfunction createTaskListItemRenderer() {\n\treturn function LiRenderer(\n\t\tprops: React.LiHTMLAttributes & { children?: ReactNode },\n\t) {\n\t\tconst { className, children, ...rest } = props;\n\t\tconst isTaskItem = (className ?? \"\").split(\" \").includes(\"task-list-item\");\n\t\tif (!isTaskItem) {\n\t\t\treturn (\n\t\t\t\t\n\t\t\t\t\t{children}\n\t\t\t\t \n\t\t\t);\n\t\t}\n\n\t\tconst childArray = Array.isArray(children) ? children : [children];\n\t\tconst checkboxNode = childArray.find(isCheckboxElement);\n\t\tconst nonCheckboxChildren = childArray.filter((c) => !isCheckboxElement(c));\n\n\t\tconst labelText = getNodeText(nonCheckboxChildren as unknown as ReactNode);\n\t\tconst baseId = slugify(labelText || \"task-item\");\n\t\tconst hashSuffix = simpleHash(labelText || \"task-item\");\n\t\tconst uniqueId = `${baseId}-${hashSuffix}`;\n\n\t\treturn (\n\t\t\t\n\t\t\t\t{checkboxNode\n\t\t\t\t\t? createElement(checkboxNode.type, {\n\t\t\t\t\t\t\t...checkboxNode.props,\n\t\t\t\t\t\t\tid: uniqueId,\n\t\t\t\t\t\t\t\"aria-label\": labelText || \"Task item\",\n\t\t\t\t\t\t})\n\t\t\t\t\t: null}\n\t\t\t\t\n\t\t\t\t\t{nonCheckboxChildren as unknown as ReactNode}\n\t\t\t\t \n\t\t\t \n\t\t);\n\t};\n}\n\ntype HeadingTag = \"h1\" | \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\";\nfunction createHeadingRenderer(tag: T, LinkComponent: NonNullable) {\n\treturn function HeadingRenderer(props: ComponentPropsWithoutRef