Skip to content

Commit 0f0c652

Browse files
test(mobile): pin which renderer each code fence gets
The mermaid/math-before-fallback precedence lived only inside codeBlockElement, so nothing caught the @pierre/highlights 0.5.0 regression where mermaid fences fell through to the code fallback. Move that decision into codeFenceRenderer in codeBlockMarkdown.ts, switch on it in codeBlockElement with no behaviour change, and table-test it. Note that the complete prop is always true in T3 today. AI-assisted; verified with the tests and tools listed above.
1 parent e455e68 commit 0f0c652

4 files changed

Lines changed: 73 additions & 46 deletions

File tree

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import {
2020
type MarkdownLinkOccurrence,
2121
type MarkdownStyle,
2222
} from "react-native-enriched-markdown";
23-
import { fallbackHighlightLanguage, HighlightedCodeBlock } from "./HighlightedCodeBlock";
23+
import { codeFenceRenderer } from "./codeBlockMarkdown";
24+
import { HighlightedCodeBlock } from "./HighlightedCodeBlock";
2425
import { MermaidCodeBlock } from "./MermaidCodeBlock";
2526
import type { SelectableMarkdownTextProps } from "./SelectableMarkdownText.types";
2627

@@ -179,12 +180,13 @@ function codeBlockElement(
179180
mathRenderingEnabled: boolean,
180181
scheme: HighlightScheme,
181182
): ReactElement | undefined {
182-
if (block.language === "mermaid") {
183+
const renderer = codeFenceRenderer(block.language, mathRenderingEnabled);
184+
if (renderer.kind === "mermaid") {
183185
return block.isComplete ? (
184186
<MermaidCodeBlock code={block.code} textStyle={textStyle} />
185187
) : undefined;
186188
}
187-
if (block.language === "math" && mathRenderingEnabled) {
189+
if (renderer.kind === "math") {
188190
return block.isComplete ? (
189191
<EnrichedMarkdownText
190192
// A blank line would end the paragraph, and so the math, early.
@@ -199,13 +201,12 @@ function codeBlockElement(
199201
/>
200202
) : undefined;
201203
}
202-
const language = fallbackHighlightLanguage(block.language);
203-
if (language && markdownStyle.codeBlock) {
204+
if (renderer.kind === "highlight" && markdownStyle.codeBlock) {
204205
return (
205206
<HighlightedCodeBlock
206207
code={block.code}
207208
fence={block.language}
208-
language={language}
209+
language={renderer.language}
209210
complete={block.isComplete}
210211
scheme={scheme}
211212
textStyle={textStyle}

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

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,11 @@ import type { MarkdownStyle } from "react-native-enriched-markdown";
55
import { Menu } from "../components/ui/Menu";
66
import { useMarkdownCodeHighlight } from "../features/threads/markdownCodeHighlightState";
77
import { copyTextWithHaptic } from "../lib/copyTextWithHaptic";
8-
import { resolveHighlightLanguage } from "../lib/highlightLanguage";
98
import type { HighlightScheme } from "../lib/highlights";
109
import { CodeBlockFrame, CodeBlockHeaderButton } from "./CodeBlockFrame";
1110
import { fencedCodeMarkdown } from "./codeBlockMarkdown";
1211
import type { NativeMarkdownTextStyle } from "./SelectableMarkdownText.types";
1312

14-
// Fences enriched-markdown colours itself: cpp/highlight/CodeBlockLanguages.cpp entries with a default grammar.
15-
const NATIVE_HIGHLIGHT_FENCES = new Set([
16-
"bash",
17-
"c",
18-
"css",
19-
"go",
20-
"golang",
21-
"html",
22-
"java",
23-
"javascript",
24-
"js",
25-
"json",
26-
"jsx",
27-
"markdown",
28-
"md",
29-
"py",
30-
"python",
31-
"rs",
32-
"rust",
33-
"sh",
34-
"shell",
35-
"ts",
36-
"tsx",
37-
"typescript",
38-
"yaml",
39-
"yml",
40-
"zsh",
41-
]);
42-
4313
// The same file's header names that are not the fence capitalized.
4414
const FENCE_DISPLAY_NAMES: Readonly<Record<string, string>> = {
4515
cc: "C++",
@@ -64,20 +34,12 @@ const CODE_MENU_ACTIONS = [
6434
{ id: "copy-markdown", title: "Copy as Markdown", image: "doc.text" },
6535
];
6636

67-
/** The highlights language for a fence enriched-markdown leaves plain, or null to keep its block. */
68-
export function fallbackHighlightLanguage(fence: string): string | null {
69-
const name = fence.toLowerCase();
70-
if (NATIVE_HIGHLIGHT_FENCES.has(name)) return null;
71-
const language = resolveHighlightLanguage({ language: name });
72-
return language === "text" ? null : language;
73-
}
74-
7537
/** A fence in a language enriched-markdown cannot colour, drawn like its native block. */
7638
export function HighlightedCodeBlock(props: {
7739
readonly code: string;
7840
readonly fence: string;
7941
readonly language: string;
80-
/** False only while the library withholds an unclosed fence (`streamingAnimation`); highlighting waits for it. */
42+
/** False only while the library withholds an unclosed fence (`streamingAnimation`, which T3 never enables, so always true today); highlighting waits for it. */
8143
readonly complete: boolean;
8244
readonly scheme: HighlightScheme;
8345
readonly textStyle: NativeMarkdownTextStyle;

‎apps/mobile/src/native/codeBlockMarkdown.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { fencedCodeMarkdown } from "./codeBlockMarkdown";
3+
import { codeFenceRenderer, fencedCodeMarkdown } from "./codeBlockMarkdown";
44

55
describe("fencedCodeMarkdown", () => {
66
it.each([
@@ -11,3 +11,20 @@ describe("fencedCodeMarkdown", () => {
1111
expect(fencedCodeMarkdown(code, language)).toBe(expected);
1212
});
1313
});
14+
15+
describe("codeFenceRenderer", () => {
16+
it.each([
17+
["mermaid", true, { kind: "mermaid" }],
18+
["math", true, { kind: "math" }],
19+
["math", false, { kind: "native" }],
20+
["kotlin", true, { kind: "highlight", language: "kotlin" }],
21+
["KOTLIN", true, { kind: "highlight", language: "kotlin" }],
22+
["diff", true, { kind: "highlight", language: "diff" }],
23+
["ts", true, { kind: "native" }],
24+
["python", true, { kind: "native" }],
25+
["", true, { kind: "native" }],
26+
["text", true, { kind: "native" }],
27+
])("draws %j with math rendering %s as %j", (fence, mathRenderingEnabled, expected) => {
28+
expect(codeFenceRenderer(fence, mathRenderingEnabled)).toEqual(expected);
29+
});
30+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,54 @@
1+
import { resolveHighlightLanguage } from "../lib/highlightLanguage";
2+
3+
// Fences enriched-markdown colours itself: cpp/highlight/CodeBlockLanguages.cpp entries with a default grammar.
4+
const NATIVE_HIGHLIGHT_FENCES = new Set([
5+
"bash",
6+
"c",
7+
"css",
8+
"go",
9+
"golang",
10+
"html",
11+
"java",
12+
"javascript",
13+
"js",
14+
"json",
15+
"jsx",
16+
"markdown",
17+
"md",
18+
"py",
19+
"python",
20+
"rs",
21+
"rust",
22+
"sh",
23+
"shell",
24+
"ts",
25+
"tsx",
26+
"typescript",
27+
"yaml",
28+
"yml",
29+
"zsh",
30+
]);
31+
132
/** A fenced code block for `code`, with a backtick fence longer than any backtick run inside it. */
233
export function fencedCodeMarkdown(code: string, language: string): string {
334
let longestRun = 0;
435
for (const [run] of code.matchAll(/`+/g)) longestRun = Math.max(longestRun, run.length);
536
const fence = "`".repeat(Math.max(3, longestRun + 1));
637
return `${fence}${language}\n${code}\n${fence}`;
738
}
39+
40+
type CodeFenceRenderer =
41+
| { readonly kind: "mermaid" }
42+
| { readonly kind: "math" }
43+
| { readonly kind: "highlight"; readonly language: string }
44+
| { readonly kind: "native" };
45+
46+
/** Mermaid and math fences first: highlights knows `mermaid` too, and would claim it as code. */
47+
export function codeFenceRenderer(fence: string, mathRenderingEnabled: boolean): CodeFenceRenderer {
48+
if (fence === "mermaid") return { kind: "mermaid" };
49+
if (fence === "math" && mathRenderingEnabled) return { kind: "math" };
50+
const name = fence.toLowerCase();
51+
if (NATIVE_HIGHLIGHT_FENCES.has(name)) return { kind: "native" };
52+
const language = resolveHighlightLanguage({ language: name });
53+
return language === "text" ? { kind: "native" } : { kind: "highlight", language };
54+
}

0 commit comments

Comments
 (0)