Skip to content

Commit c9c5f0a

Browse files
fix(mobile): Mermaid labels keep var() text and streaming keeps rendered blocks
- Resolve Pele's CSS variables in attribute values only, so a label that reads var(--x) keeps its text. - The code renderer reads the base Markdown style, not the link variants that change as a message streams, so the library keeps its rendered blocks. - A failed diagram export resets, so the next tap can try again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bf38c10 commit c9c5f0a

4 files changed

Lines changed: 32 additions & 10 deletions

File tree

‎apps/mobile/src/lib/mermaidSvg.test.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ import { mermaidSvgForNative, withDiagramBackground } from "./mermaidSvg";
55

66
describe("mermaidSvgForNative", () => {
77
it("turns Pele's CSS variables and styles into attributes react-native-svg draws", () => {
8-
const { svg } = render("flowchart LR\n A[Client] -->|ws| B(Server)", {
9-
links: false,
10-
images: false,
11-
});
8+
const { svg } = render(
9+
'flowchart LR\n A[Client] -->|ws| B(Server)\n B --> C["var(--pele-fg)"]',
10+
{
11+
links: false,
12+
images: false,
13+
},
14+
);
1215

1316
const native = mermaidSvgForNative(svg, {
1417
"--pele-fg": "#111111",
1518
"--pele-line": "#222222",
1619
"--pele-font": "Helvetica",
1720
});
1821

19-
expect(native).not.toMatch(/var\(|style=/);
22+
expect(native).not.toMatch(/="[^"]*var\(|style=/);
23+
// A label that reads like a variable keeps its text.
24+
expect(native).toContain(">var(--pele-fg)<");
2025
expect(native).toMatch(
2126
/^<svg[^>]* fill="#111111"[^>]* font-family="Helvetica" font-size="16">/,
2227
);

‎apps/mobile/src/lib/mermaidSvg.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ export function mermaidSvgForNative(svg: string, theme: Readonly<Record<string,
2929
.map(([name, value]) => ` ${name}="${resolve(value).replace(/^(-?[\d.]+)px$/, "$1")}"`)
3030
.join(""),
3131
);
32-
return root + resolve(body);
32+
// Attribute values only: a label's text can itself read `var(--…)`.
33+
return (
34+
root +
35+
body.replace(
36+
/(\s[\w:-]+=")([^"]*)"/g,
37+
(_match, prefix: string, value: string) => `${prefix}${resolve(value)}"`,
38+
)
39+
);
3340
}
3441

3542
/** Fills the diagram's viewBox, so an exported image reads on any background. */

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,21 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
295295
};
296296
}, [props.contextClipboardFragment, props.markdown]);
297297
const { fontScale } = useWindowDimensions();
298-
const markdownStyle = useMemo(() => {
298+
const baseMarkdownStyle = useMemo(() => {
299299
const style = enrichedStyle(props.textStyle, themeAppearance === "dark", fontScale);
300300
return {
301301
...style,
302302
paragraph: props.textAlign
303303
? { ...style.paragraph, textAlign: props.textAlign }
304304
: style.paragraph,
305-
linkVariants,
306305
};
307-
}, [props.textStyle, props.textAlign, themeAppearance, fontScale, linkVariants]);
306+
}, [props.textStyle, props.textAlign, themeAppearance, fontScale]);
307+
// Link variants change as a message streams. The code renderer reads only the base style, so
308+
// it stays the same function and the library keeps its rendered blocks.
309+
const markdownStyle = useMemo(
310+
() => ({ ...baseMarkdownStyle, linkVariants }),
311+
[baseMarkdownStyle, linkVariants],
312+
);
308313
const renderers = {
309314
image: renderImage
310315
? (block: MarkdownImageBlock) => {
@@ -317,7 +322,7 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
317322
}
318323
: undefined,
319324
code: (block: MarkdownCodeBlock) =>
320-
codeBlockElement(block, props.textStyle, markdownStyle, mathRenderingEnabled),
325+
codeBlockElement(block, props.textStyle, baseMarkdownStyle, mathRenderingEnabled),
321326
};
322327
const resolveNativeImageSource = useRetryingImageSource(resolveImageSource);
323328
const renderMarkdown = (openAndroidMenu?: () => void) => {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ function MermaidBlock(props: MermaidCodeBlockProps & { readonly diagram: Diagram
173173
const pixels = Platform.OS === "ios" ? 1 : PixelRatio.get();
174174
exportSvg.current.toDataURL(
175175
(base64) => {
176+
// A failed capture calls back with nothing; let the next tap try again.
177+
if (!base64) {
178+
setExpansion(null);
179+
return;
180+
}
176181
// iOS wraps the encoding in line feeds.
177182
writeDiagramImage(exportXml, base64.replace(/\s/g, "")).then(
178183
(uri) => setExpansion({ status: "open", uri }),

0 commit comments

Comments
 (0)