Open History to see the last commit
1/**
2 * Detects markdown that the JS renderer draws as a block requiring a definite
3 * user-bubble width — fenced code blocks, GFM tables, and ordered lists.
4 *
5 * Fenced code blocks and tables report an intrinsic width equal to their
6 * widest line, which is effectively unbounded. A user bubble sizes itself
7 * from its content
8 * (`maxWidth` with no `width`), so Android lays the bubble's children out
9 * during the unclamped intrinsic pass — where the surrounding paragraphs
10 * collapse to a single line — and never repositions them once the width is
11 * clamped back to `maxWidth`. The result is siblings drawn on top of each
12 * other inside an over-tall bubble. Pinning the bubble's width removes the
13 * intrinsic pass entirely, which is the same reason review-comment bubbles
14 * already carry an explicit width.
15 *
16 * Ordered lists hit the same Android layout bug because each item contains a
17 * flexing content column inside a shrink-to-fit row.
18 *
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.
22 */
23
24const FENCED_CODE_BLOCK = /^ {0,3}(?:```|~~~)/m;
25// Trades some precision for a simple check, favoring false positives over false
26// negatives: list-shaped paragraph may get a wider bubble
27const ORDERED_LIST_ITEM = /^ {0,3}\d{1,9}[.)](?:[ \t]+|$)/;
28const INDENTED_ORDERED_LIST_ITEM = /^( {4,})\d{1,9}[.)](?:[ \t]+|$)/;
29const ANY_LIST_ITEM = /^( *)(?:[-+*]|\d{1,9}[.)])(?:[ \t]+|$)/;
30const BLOCKQUOTE_PREFIX = /^ {0,3}>[ \t]?/;
31
32export interface WideMarkdownBlockOptions {
33 readonly includeOrderedLists?: boolean;
34}
35
36function 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
44function 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}
67
68function isTableDelimiterRow(line: string): boolean {
69 const trimmed = line.trim();
70 if (!trimmed.includes("|") || !trimmed.includes("-")) {
71 return false;
72 }
73 return /^[|\-: \t]+$/.test(trimmed);
74}
75
76export 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)) {
84 return true;
85 }
86 if (!text.includes("|")) {
87 return false;
88 }
89 return text.split("\n").some(isTableDelimiterRow);
90}