Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
17cb87e
fix(mobile): chat code blocks colour diff, kotlin, sql and other lang…
juliusmarminge Nov 10, 2026
a230081
fix(mobile): keep the fallback code block mounted while its fence str…
juliusmarminge Nov 10, 2026
33151da
docs(mobile): say when a code fence counts as complete
juliusmarminge Nov 10, 2026
558ee8d
Merge remote-tracking branch 'origin/main' into fix/issue-33638
juliusmarminge Nov 10, 2026
0c6994d
fix(mobile): mermaid and math fences keep their renderers ahead of th…
juliusmarminge Nov 10, 2026
f2f0112
Merge remote-tracking branch 'origin/main' into fix/issue-33638
juliusmarminge Nov 10, 2026
bda955f
fix(mobile): highlighted code fences keep the long-press copy menu
juliusmarminge Nov 10, 2026
25fea91
fix(mobile): highlighted code fences scroll sideways again on iOS
juliusmarminge Nov 10, 2026
e455e68
Merge remote-tracking branch 'origin/main' into pr-33680
juliusmarminge Nov 10, 2026
0f0c652
test(mobile): pin which renderer each code fence gets
juliusmarminge Nov 10, 2026
6c54d4a
docs(mobile): name the file the fence display names come from
juliusmarminge Nov 10, 2026
6e5d2a0
fix(mobile): Android menus stay on screen when their anchor is taller…
juliusmarminge Nov 10, 2026
63069af
fix(mobile): first long-press on a scrolled code block keeps the keyb…
juliusmarminge Nov 10, 2026
c6afa44
docs(mobile): say what keeping taps on the code scroller does
juliusmarminge Nov 10, 2026
06c360d
fix(mobile): code block headers grow with large text instead of clipp…
juliusmarminge Nov 10, 2026
b00d15f
Merge remote-tracking branch 'origin/main' into fix/issue-33638
juliusmarminge Nov 10, 2026
e0bc83b
refactor(mobile): drop the Android menu's duplicate root height
juliusmarminge Nov 10, 2026
b4ca413
Merge remote-tracking branch 'origin/main' into fix/issue-33638
juliusmarminge Nov 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions apps/mobile/src/native/CodeBlockFrame.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import type { ReactNode } from "react";
import { Pressable, StyleSheet, Text, View } from "react-native";

import { SymbolView, type AppSymbolName } from "../components/ui/AppSymbol";
import { themeColorWithAlpha } from "../lib/mobileTheme";
import type { NativeMarkdownTextStyle } from "./SelectableMarkdownText.types";

/**
* The native code block's border and header, for a fence drawn in React once it closes.
* The block slot already applies the code block's outer margins.
*/
export function CodeBlockFrame(props: {
readonly label: string;
readonly textStyle: NativeMarkdownTextStyle;
readonly actions: ReactNode;
readonly children: ReactNode;
}) {
const { textStyle } = props;
return (
<View
style={[
styles.block,
{
backgroundColor: textStyle.codeBlockBackgroundColor,
borderColor: textStyle.dividerColor,
},
]}
>
<View
style={[
styles.header,
{ borderBottomColor: themeColorWithAlpha(textStyle.codeColor, 0.2) },
]}
>
<Text
style={[
styles.label,
{
color: themeColorWithAlpha(textStyle.codeColor, 0.6),
fontSize: textStyle.fontSize * 0.9 * 0.85,
},
]}
>
{props.label}
</Text>
<View style={styles.actions}>{props.actions}</View>
</View>
{props.children}
</View>
);
}

/** A header icon; `active` draws it at full strength, as the native wrap toggle when on. */
export function CodeBlockHeaderButton(props: {
readonly icon: AppSymbolName;
readonly label: string;
readonly textStyle: NativeMarkdownTextStyle;
readonly active?: boolean;
readonly onPress: () => void;
}) {
return (
<Pressable
accessibilityRole="button"
accessibilityLabel={props.label}
hitSlop={10}
onPress={props.onPress}
>
<SymbolView
name={props.icon}
size={14}
tintColor={
props.active
? props.textStyle.codeColor
: themeColorWithAlpha(props.textStyle.codeColor, 0.6)
}
type="monochrome"
/>
</Pressable>
);
}

const styles = StyleSheet.create({
block: {
borderRadius: 8,
borderWidth: StyleSheet.hairlineWidth,
overflow: "hidden",
},
header: {
height: 32,
paddingHorizontal: 12,
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
borderBottomWidth: StyleSheet.hairlineWidth,
},
label: { fontWeight: "500" },
actions: { flexDirection: "row", alignItems: "center", gap: 14 },
});
30 changes: 28 additions & 2 deletions apps/mobile/src/native/EnrichedMarkdownText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
type MarkdownLinkOccurrence,
type MarkdownStyle,
} from "react-native-enriched-markdown";
import { fallbackHighlightLanguage, HighlightedCodeBlock } from "./HighlightedCodeBlock";
import { MermaidCodeBlock } from "./MermaidCodeBlock";
import type { SelectableMarkdownTextProps } from "./SelectableMarkdownText.types";

Expand All @@ -29,6 +30,7 @@ import { useEnrichedLinkVariants } from "./useEnrichedLinkVariants";
import { useRetryingImageSource } from "./useRetryingImageSource";
import * as Dialog from "../components/ui/Dialog";
import { copyTextWithHaptic } from "../lib/copyTextWithHaptic";
import type { HighlightScheme } from "../lib/highlights";
import {
ENRICHED_INLINE_FILE_LINK_REGEX,
enrichedTextLinkRegex,
Expand Down Expand Up @@ -166,13 +168,31 @@ function enrichedStyle(
};
}

/** Diagram and math fences, as web draws them. A fence streams as native code until it closes. */
/**
* Diagram and math fences, as web draws them, and code enriched-markdown cannot colour.
* Diagrams and math stay native until the library reports them complete; fallback code keeps one block throughout.
*/
function codeBlockElement(
block: MarkdownCodeBlock,
textStyle: SelectableMarkdownTextProps["textStyle"],
markdownStyle: MarkdownStyle,
mathRenderingEnabled: boolean,
scheme: HighlightScheme,
): ReactElement | undefined {
const language = fallbackHighlightLanguage(block.language);
if (language && markdownStyle.codeBlock) {
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Resolved
return (
<HighlightedCodeBlock
code={block.code}
fence={block.language}
language={language}
complete={block.isComplete}
scheme={scheme}
textStyle={textStyle}
codeStyle={markdownStyle.codeBlock}
/>
);
}
if (!block.isComplete) return undefined;
if (block.language === "mermaid") {
return <MermaidCodeBlock code={block.code} textStyle={textStyle} />;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Expand Down Expand Up @@ -347,7 +367,13 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
}
: undefined,
code: (block: MarkdownCodeBlock) =>
codeBlockElement(block, props.textStyle, baseMarkdownStyle, mathRenderingEnabled),
codeBlockElement(
block,
props.textStyle,
baseMarkdownStyle,
mathRenderingEnabled,
themeAppearance,
),
};
const resolveNativeImageSource = useRetryingImageSource(resolveImageSource);
const renderMarkdown = (openAndroidMenu?: () => void) => {
Expand Down
148 changes: 148 additions & 0 deletions apps/mobile/src/native/HighlightedCodeBlock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { useState } from "react";
import { ScrollView, Text, View } from "react-native";
import type { MarkdownStyle } from "react-native-enriched-markdown";

import { useMarkdownCodeHighlight } from "../features/threads/markdownCodeHighlightState";
import { copyTextWithHaptic } from "../lib/copyTextWithHaptic";
import { resolveHighlightLanguage } from "../lib/highlightLanguage";
import type { HighlightScheme } from "../lib/highlights";
import { CodeBlockFrame, CodeBlockHeaderButton } from "./CodeBlockFrame";
import type { NativeMarkdownTextStyle } from "./SelectableMarkdownText.types";

// Fences enriched-markdown colours itself: cpp/highlight/CodeBlockLanguages.cpp entries with a default grammar.
const NATIVE_HIGHLIGHT_FENCES = new Set([
"bash",
"c",
"css",
"go",
"golang",
"html",
"java",
"javascript",
"js",
"json",
"jsx",
"markdown",
"md",
"py",
"python",
"rs",
"rust",
"sh",
"shell",
"ts",
"tsx",
"typescript",
"yaml",
"yml",
"zsh",
]);

// The same file's header names that are not the fence capitalized.
const FENCE_DISPLAY_NAMES: Readonly<Record<string, string>> = {
cc: "C++",
cpp: "C++",
cxx: "C++",
cs: "C#",
csharp: "C#",
graphql: "GraphQL",
objc: "Objective-C",
objectivec: "Objective-C",
php: "PHP",
rb: "Ruby",
scss: "SCSS",
sql: "SQL",
toml: "TOML",
xml: "XML",
};

/** The highlights language for a fence enriched-markdown leaves plain, or null to keep its block. */
export function fallbackHighlightLanguage(fence: string): string | null {
const name = fence.toLowerCase();
if (NATIVE_HIGHLIGHT_FENCES.has(name)) return null;
const language = resolveHighlightLanguage({ language: name });
return language === "text" ? null : language;
}

/** A fence in a language enriched-markdown cannot colour, drawn like its native block. */
export function HighlightedCodeBlock(props: {
readonly code: string;
readonly fence: string;
readonly language: string;
/** False only while the library withholds an unclosed fence (`streamingAnimation`); highlighting waits for it. */
readonly complete: boolean;
readonly scheme: HighlightScheme;
readonly textStyle: NativeMarkdownTextStyle;
readonly codeStyle: NonNullable<MarkdownStyle["codeBlock"]>;
}) {
const { code, codeStyle, textStyle } = props;
const [wrapped, setWrapped] = useState(false);
const lines = useMarkdownCodeHighlight({
code,
language: props.language,
theme: props.scheme,
enabled: props.complete,
});
const fence = props.fence.toLowerCase();
let offset = 0;
const text = (
<Text
selectable
style={{
color: codeStyle.color,
fontFamily: codeStyle.fontFamily,
fontSize: codeStyle.fontSize,
lineHeight: codeStyle.lineHeight,
}}
>
{lines?.flatMap((line, lineIndex) => {
const separator = lineIndex > 0 ? "\n" : "";
offset += separator.length;
return [
separator,
...line.map((token) => {
const start = offset;
offset += token.content.length;
return (
<Text key={start} style={token.color ? { color: token.color } : undefined}>
{token.content}
</Text>
);
}),
];
}) ?? code}
</Text>
);
const padding = { padding: codeStyle.padding };
return (
<CodeBlockFrame
label={FENCE_DISPLAY_NAMES[fence] ?? fence.charAt(0).toUpperCase() + fence.slice(1)}
textStyle={textStyle}
actions={
<>
<CodeBlockHeaderButton
icon="text.alignleft"
label={wrapped ? "Disable line wrap" : "Wrap lines"}
textStyle={textStyle}
active={wrapped}
onPress={() => setWrapped((value) => !value)}
/>
<CodeBlockHeaderButton
icon="doc.on.doc"
label="Copy code"
textStyle={textStyle}
onPress={() => copyTextWithHaptic(code)}
/>
</>
}
>
{wrapped ? (
<View style={padding}>{text}</View>
) : (
<ScrollView horizontal contentContainerStyle={padding}>
{text}
</ScrollView>
)}
</CodeBlockFrame>
);
}
Loading
Loading