Open History to see the last commit
1/**
2 * A user bubble sizes itself from its content (`maxWidth` with no `width`), so
3 * Android lays the bubble's children out during the unclamped intrinsic pass —
4 * where the surrounding paragraphs collapse to a single line — and never
5 * repositions them once the width is clamped back to `maxWidth`. Ordered lists
6 * hit the same Android layout bug because each item contains a flexing content
7 * column inside a shrink-to-fit row.
8 */
9
10const FENCED_CODE_BLOCK = /^ {0,3}(?:```|~~~)/m;
11const ORDERED_LIST_ITEM = /^ {0,3}\d{1,9}[.)](?:[ \t]+|$)/;
12const INDENTED_ORDERED_LIST_ITEM = /^( {4,})\d{1,9}[.)](?:[ \t]+|$)/;
13const ANY_LIST_ITEM = /^( *)(?:[-+*]|\d{1,9}[.)])(?:[ \t]+|$)/;
14const BLOCKQUOTE_PREFIX = /^ {0,3}>[ \t]?/;
15
16export interface WideMarkdownBlockOptions {
17 readonly includeOrderedLists?: boolean;
18 readonly includeBlockquotes?: boolean;
19}
20
21function stripBlockquotePrefixes(line: string): string {
22 let content = line;
23 while (BLOCKQUOTE_PREFIX.test(content)) {
24 content = content.replace(BLOCKQUOTE_PREFIX, "");
25 }
26 return content;
27}
28
29function hasIndentedCodeBlock(text: string): boolean {
30 return text.split("\n").some((rawLine) => {
31 const line = stripBlockquotePrefixes(rawLine);
32 let column = 0;
33 let index = 0;
34
35 // Markdown tabs advance to the next four-column stop.
36 while (index < line.length) {
37 if (line[index] === " ") {
38 column += 1;
39 } else if (line[index] === "\t") {
40 column += 4 - (column % 4);
41 } else {
42 break;
43 }
44 index += 1;
45 }
46
47 return column >= 4 && index < line.length && line[index] !== "\r";
48 });
49}
50
51function hasBlockquote(text: string): boolean {
52 return text.split("\n").some((line) => BLOCKQUOTE_PREFIX.test(line));
53}
54
55function hasOrderedListItem(text: string): boolean {
56 let previousNonEmptyLine: string | null = null;
57
58 for (const rawLine of text.split("\n")) {
59 const line = stripBlockquotePrefixes(rawLine);
60 if (ORDERED_LIST_ITEM.test(line)) {
61 return true;
62 }
63
64 const nestedMatch = INDENTED_ORDERED_LIST_ITEM.exec(line);
65 const parentMatch =
66 previousNonEmptyLine === null ? null : ANY_LIST_ITEM.exec(previousNonEmptyLine);
67 if (
68 nestedMatch?.[1] !== undefined &&
69 parentMatch?.[1] !== undefined &&
70 parentMatch[1].length < nestedMatch[1].length
71 ) {
72 return true;
73 }
74
75 if (line.trim().length > 0) {
76 previousNonEmptyLine = line;
77 }
78 }
79
80 return false;
81}
82
83function isTableDelimiterRow(line: string): boolean {
84 const trimmed = line.trim();
85 if (!trimmed.includes("|") || !trimmed.includes("-")) {
86 return false;
87 }
88 return /^[|\-: \t]+$/.test(trimmed);
89}
90
91export function hasWideMarkdownBlock(
92 text: string,
93 options: WideMarkdownBlockOptions = {},
94): boolean {
95 if (FENCED_CODE_BLOCK.test(text)) {
96 return true;
97 }
98 if (options.includeBlockquotes === true && hasBlockquote(text)) {
99 return true;
100 }
101 if (hasIndentedCodeBlock(text)) {
102 return true;
103 }
104 if (options.includeOrderedLists !== false && hasOrderedListItem(text)) {
105 return true;
106 }
107 if (!text.includes("|")) {
108 return false;
109 }
110 return text.split("\n").some(isTableDelimiterRow);
111}