Skip to content

Commit 058ed89

Browse files
committed
fix(mobile): render Mermaid diagrams in chat
Mobile showed ```mermaid fences as raw code because the native markdown renderer has no Mermaid support. Diagrams are now rendered with pele to an SVG string in JS and drawn with react-native-svg in the markdown media slot. pele loads on the first diagram and results are cached. Touch and hold a diagram to copy its source. Closes #24212 Model: Claude Opus 5.5 (claude-opus-5-5) via Claude Code in T3 Code
1 parent a43b11d commit 058ed89

2 files changed

Lines changed: 149 additions & 15 deletions

File tree

‎apps/mobile/src/native/EnrichedMarkdownText.tsx‎

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import {
77
import {
88
markdownWithHtml,
99
markdownWithInlineFileLinks,
10+
markdownWithMermaidDiagrams,
1011
markdownWithTableBreaks,
1112
} from "../lib/markdownDocument";
13+
import type { MermaidPalette } from "../lib/mermaidSvg";
14+
import { themeColorWithAlpha } from "../lib/mobileTheme";
1215
import { useCallback, useMemo, useState, type ReactNode } from "react";
1316
import { Platform, Share, StyleSheet, View } from "react-native";
1417
import {
@@ -21,6 +24,7 @@ import type { SelectableMarkdownTextProps } from "./SelectableMarkdownText.types
2124

2225
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
2326
import { Menu } from "../components/ui/Menu";
27+
import { MermaidDiagramView, renderMermaidDiagram } from "./MermaidDiagram";
2428
import { useEnrichedLinkVariants } from "./useEnrichedLinkVariants";
2529
import { copyTextWithHaptic } from "../lib/copyTextWithHaptic";
2630
import {
@@ -152,18 +156,40 @@ function enrichedStyle(
152156
}
153157

154158
export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
155-
const { renderImage, resolveImageSource } = props;
159+
const { renderImage, resolveImageSource, textStyle } = props;
156160
const { themeAppearance, mathRenderingEnabled } = useAppearancePreferences();
157-
const markdown = useMemo(
158-
() =>
159-
markdownWithTableBreaks(
161+
const mermaidPalette = useMemo(
162+
(): MermaidPalette => ({
163+
background: textStyle.codeBlockBackgroundColor,
164+
foreground: textStyle.color,
165+
muted: textStyle.mutedColor,
166+
surface: themeColorWithAlpha(textStyle.color, 0.08),
167+
surfaceAlt: themeColorWithAlpha(textStyle.color, 0.16),
168+
border: themeColorWithAlpha(textStyle.color, 0.28),
169+
accent: textStyle.linkColor,
170+
monoFontFamily: Platform.OS === "ios" ? "Menlo" : "monospace",
171+
}),
172+
[
173+
textStyle.codeBlockBackgroundColor,
174+
textStyle.color,
175+
textStyle.mutedColor,
176+
textStyle.linkColor,
177+
],
178+
);
179+
const { markdown, mermaidDiagrams } = useMemo(() => {
180+
const withDiagrams = markdownWithMermaidDiagrams(props.markdown, (source) =>
181+
renderMermaidDiagram(source, mermaidPalette),
182+
);
183+
return {
184+
markdown: markdownWithTableBreaks(
160185
markdownWithInlineFileLinks(
161-
markdownWithHtml(props.markdown),
186+
markdownWithHtml(withDiagrams.markdown),
162187
ENRICHED_INLINE_FILE_LINK_REGEX,
163188
),
164189
),
165-
[props.markdown],
166-
);
190+
mermaidDiagrams: withDiagrams.diagrams,
191+
};
192+
}, [props.markdown, mermaidPalette]);
167193
const [documentAssets, setDocumentAssets] = useState<DocumentAssetsEvent["assets"]>([]);
168194
const onDocumentAssets = useCallback(({ assets }: DocumentAssetsEvent) => {
169195
setDocumentAssets((previous) =>
@@ -265,13 +291,25 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
265291
[props.textStyle, themeAppearance, linkVariants],
266292
);
267293
const renderMedia = useCallback(
268-
(asset: MarkdownMediaAsset) =>
269-
renderImage?.({
270-
href: asset.url,
271-
alt: asset.altText || null,
272-
title: asset.title || null,
273-
}) ?? null,
274-
[renderImage],
294+
(asset: MarkdownMediaAsset) => {
295+
const diagram = mermaidDiagrams.get(asset.url);
296+
if (diagram)
297+
return (
298+
<MermaidDiagramView
299+
diagram={diagram}
300+
backgroundColor={mermaidPalette.background}
301+
borderColor={textStyle.dividerColor}
302+
/>
303+
);
304+
return (
305+
renderImage?.({
306+
href: asset.url,
307+
alt: asset.altText || null,
308+
title: asset.title || null,
309+
}) ?? null
310+
);
311+
},
312+
[mermaidDiagrams, mermaidPalette.background, textStyle.dividerColor, renderImage],
275313
);
276314
const resolveNativeImageSource = useCallback(
277315
(asset: MarkdownMediaAsset) =>
@@ -311,7 +349,7 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
311349
markdown={markdown}
312350
markdownStyle={markdownStyle}
313351
onDocumentAssets={onDocumentAssets}
314-
renderMedia={renderImage ? renderMedia : undefined}
352+
renderMedia={renderImage || mermaidDiagrams.size > 0 ? renderMedia : undefined}
315353
resolveImageSource={resolveImageSource ? resolveNativeImageSource : undefined}
316354
onImagePress={
317355
props.onImagePress || props.onLinkPress
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { Pressable, StyleSheet, View } from "react-native";
2+
import { parse, SvgAst, type JsxAST, type XmlAST } from "react-native-svg";
3+
4+
import { copyTextWithHaptic } from "../lib/copyTextWithHaptic";
5+
import { nativeMermaidSvg, type MermaidPalette } from "../lib/mermaidSvg";
6+
7+
export interface MermaidDiagram {
8+
readonly source: string;
9+
readonly ast: JsxAST;
10+
readonly width: number;
11+
readonly height: number;
12+
}
13+
14+
type Pele = typeof import("pele");
15+
let pele: Pele | undefined;
16+
const renderedDiagrams = new Map<string, MermaidDiagram | null>();
17+
const RENDERED_DIAGRAM_LIMIT = 64;
18+
19+
const XML_ENTITIES: Record<string, string> = {
20+
"&amp;": "&",
21+
"&lt;": "<",
22+
"&gt;": ">",
23+
"&quot;": '"',
24+
"&#39;": "'",
25+
};
26+
27+
// react-native-svg draws text nodes verbatim, so `a &amp; b` would show its escape.
28+
function decodeTextEntities(node: XmlAST): XmlAST {
29+
node.children = node.children.map((child) =>
30+
typeof child === "string"
31+
? child.replace(/&(?:amp|lt|gt|quot|#39);/g, (entity) => XML_ENTITIES[entity]!)
32+
: decodeTextEntities(child),
33+
);
34+
return node;
35+
}
36+
37+
/** Returns null when the source is not a diagram Pele or react-native-svg can draw. */
38+
export function renderMermaidDiagram(
39+
source: string,
40+
palette: MermaidPalette,
41+
): MermaidDiagram | null {
42+
const key = `${Object.values(palette).join("|")}\n${source}`;
43+
const cached = renderedDiagrams.get(key);
44+
if (cached !== undefined) return cached;
45+
let diagram: MermaidDiagram | null = null;
46+
try {
47+
// Deferred so the renderer only loads once a thread actually contains a diagram.
48+
pele ??= require("pele") as Pele;
49+
// Links and images stay off: diagrams can come from untrusted PR descriptions.
50+
const result = pele.render(source, { links: false, images: false, autoDirection: false });
51+
const ast = parse(nativeMermaidSvg(result.svg, palette), decodeTextEntities);
52+
if (ast && result.width > 0 && result.height > 0)
53+
diagram = { source, ast, width: result.width, height: result.height };
54+
} catch {
55+
// Unsupported or invalid diagrams stay readable as their code block.
56+
}
57+
if (renderedDiagrams.size >= RENDERED_DIAGRAM_LIMIT)
58+
renderedDiagrams.delete(renderedDiagrams.keys().next().value!);
59+
renderedDiagrams.set(key, diagram);
60+
return diagram;
61+
}
62+
63+
export function MermaidDiagramView(props: {
64+
readonly diagram: MermaidDiagram;
65+
readonly backgroundColor: string;
66+
readonly borderColor: string;
67+
}) {
68+
const { diagram } = props;
69+
return (
70+
<Pressable
71+
accessibilityRole="image"
72+
accessibilityLabel="Mermaid diagram"
73+
accessibilityHint="Touch and hold to copy the Mermaid source"
74+
onLongPress={() => copyTextWithHaptic(diagram.source, { target: "Mermaid source" })}
75+
style={{
76+
alignItems: "center",
77+
padding: 12,
78+
borderRadius: 8,
79+
borderWidth: StyleSheet.hairlineWidth,
80+
borderColor: props.borderColor,
81+
backgroundColor: props.backgroundColor,
82+
}}
83+
>
84+
{/* A known aspect ratio sizes the slot in one pass; wide diagrams scale down to fit. */}
85+
<View
86+
style={{
87+
width: "100%",
88+
maxWidth: diagram.width,
89+
aspectRatio: diagram.width / diagram.height,
90+
}}
91+
>
92+
<SvgAst ast={diagram.ast} override={{ width: "100%", height: "100%", accessible: false }} />
93+
</View>
94+
</Pressable>
95+
);
96+
}

0 commit comments

Comments
 (0)