Skip to content

Commit 3b35142

Browse files
committed
fix(mobile): cover ordered list bubble edge cases
1 parent 377162c commit 3b35142

3 files changed

Lines changed: 75 additions & 8 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ import { useMarkdownCodeHighlight } from "./markdownCodeHighlightState";
104104
import { useAssetUrl } from "../../state/assets";
105105
import { resolveWorkspaceRelativeFilePath } from "../files/filePath";
106106

107+
const WIDE_MARKDOWN_BLOCK_OPTIONS = {
108+
includeOrderedLists: Platform.OS === "android",
109+
} as const;
110+
107111
const MESSAGE_TIME_FORMATTER = new Intl.DateTimeFormat(undefined, {
108112
hour: "numeric",
109113
minute: "2-digit",
@@ -888,7 +892,7 @@ function renderFeedEntry(
888892
// children during the unclamped pass and never moves them once the width
889893
// is clamped, so the paragraphs around the block end up drawn on top of
890894
// each other. Pinning the width removes that pass.
891-
const hasWideBlock = hasWideMarkdownBlock(message.text);
895+
const hasWideBlock = hasWideMarkdownBlock(message.text, WIDE_MARKDOWN_BLOCK_OPTIONS);
892896
const assistantTurnStillInProgress =
893897
message.role === "assistant" &&
894898
props.unsettledTurnId !== null &&

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,28 @@ describe("hasWideMarkdownBlock", () => {
1717
expect(hasWideMarkdownBlock(" ```\ncode\n```")).toBe(true);
1818
});
1919

20-
it("detects ordered-list markers with a simple heuristic", () => {
20+
it("detects top-level and blockquoted ordered-list markers", () => {
2121
expect(hasWideMarkdownBlock("1. One\n2. Two\n3. Three\n4. Four\n5. Five")).toBe(true);
2222
expect(hasWideMarkdownBlock("before\n3) Three")).toBe(true);
23+
expect(hasWideMarkdownBlock("> 1. One\n> 2. Two")).toBe(true);
24+
expect(hasWideMarkdownBlock("> > 3) Three")).toBe(true);
25+
});
26+
27+
it("detects nested ordered lists without treating indented code as a list", () => {
28+
expect(hasWideMarkdownBlock("- Parent\n 1. Child\n 2. Child")).toBe(true);
29+
expect(hasWideMarkdownBlock("> - Parent\n> 1. Child")).toBe(true);
2330
expect(hasWideMarkdownBlock(" 1. indented code")).toBe(false);
31+
expect(hasWideMarkdownBlock(" - code-like bullet\n 1. indented code")).toBe(false);
32+
});
33+
34+
it("can limit ordered-list width pinning to Android", () => {
35+
const orderedList = "1. One\n2. Two";
36+
expect(hasWideMarkdownBlock(orderedList, { includeOrderedLists: true })).toBe(true);
37+
expect(hasWideMarkdownBlock(orderedList, { includeOrderedLists: false })).toBe(false);
38+
expect(hasWideMarkdownBlock("```\ncode\n```", { includeOrderedLists: false })).toBe(true);
39+
expect(hasWideMarkdownBlock("| a | b |\n| --- | --- |", { includeOrderedLists: false })).toBe(
40+
true,
41+
);
2442
});
2543

2644
it("detects GFM tables", () => {

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

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,54 @@
1616
* Ordered lists hit the same Android layout bug because each item contains a
1717
* flexing content column inside a shrink-to-fit row.
1818
*
19-
* Indented (four-space) code blocks are deliberately not detected: they are
20-
* vanishingly rare in chat input and the check would fire on ordinary nested
21-
* list continuations.
19+
* A four-space ordered marker is only treated as a list when the preceding
20+
* non-empty line is a less-indented list item. This keeps standalone indented
21+
* code out of the heuristic while covering nested lists.
2222
*/
2323

2424
const FENCED_CODE_BLOCK = /^ {0,3}(?:```|~~~)/m;
2525
// Trades some precision for a simple check, favoring false positives over false
2626
// negatives: list-shaped paragraph may get a wider bubble
27-
const ORDERED_LIST_ITEM = /^ {0,3}\d{1,9}[.)](?:[ \t]+|$)/m;
27+
const ORDERED_LIST_ITEM = /^ {0,3}\d{1,9}[.)](?:[ \t]+|$)/;
28+
const INDENTED_ORDERED_LIST_ITEM = /^( {4,})\d{1,9}[.)](?:[ \t]+|$)/;
29+
const ANY_LIST_ITEM = /^( *)(?:[-+*]|\d{1,9}[.)])(?:[ \t]+|$)/;
30+
const BLOCKQUOTE_PREFIX = /^ {0,3}>[ \t]?/;
31+
32+
export interface WideMarkdownBlockOptions {
33+
readonly includeOrderedLists?: boolean;
34+
}
35+
36+
function stripBlockquotePrefixes(line: string): string {
37+
let content = line;
38+
while (BLOCKQUOTE_PREFIX.test(content)) {
39+
content = content.replace(BLOCKQUOTE_PREFIX, "");
40+
}
41+
return content;
42+
}
43+
44+
function hasOrderedListItem(text: string): boolean {
45+
let previousNonEmptyLine: string | null = null;
46+
47+
for (const rawLine of text.split("\n")) {
48+
const line = stripBlockquotePrefixes(rawLine);
49+
if (ORDERED_LIST_ITEM.test(line)) {
50+
return true;
51+
}
52+
53+
const nestedMatch = INDENTED_ORDERED_LIST_ITEM.exec(line);
54+
const parentMatch =
55+
previousNonEmptyLine === null ? null : ANY_LIST_ITEM.exec(previousNonEmptyLine);
56+
if (nestedMatch && parentMatch && parentMatch[1].length < nestedMatch[1].length) {
57+
return true;
58+
}
59+
60+
if (line.trim().length > 0) {
61+
previousNonEmptyLine = line;
62+
}
63+
}
64+
65+
return false;
66+
}
2867

2968
function isTableDelimiterRow(line: string): boolean {
3069
const trimmed = line.trim();
@@ -34,8 +73,14 @@ function isTableDelimiterRow(line: string): boolean {
3473
return /^[|\-: \t]+$/.test(trimmed);
3574
}
3675

37-
export function hasWideMarkdownBlock(text: string): boolean {
38-
if (FENCED_CODE_BLOCK.test(text) || ORDERED_LIST_ITEM.test(text)) {
76+
export function hasWideMarkdownBlock(
77+
text: string,
78+
options: WideMarkdownBlockOptions = {},
79+
): boolean {
80+
if (FENCED_CODE_BLOCK.test(text)) {
81+
return true;
82+
}
83+
if (options.includeOrderedLists !== false && hasOrderedListItem(text)) {
3984
return true;
4085
}
4186
if (!text.includes("|")) {

0 commit comments

Comments
 (0)