Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

wideMarkdownBlocks.ts

99 lines3.3 KB

1/**
2 * Detects markdown that the renderer draws as a block requiring a definite
3 * user-bubble width: fenced code blocks, GFM tables, ordered lists, and
4 * blockquotes when requested by the caller.
5 *
6 * Fenced code blocks and tables report an intrinsic width equal to their
7 * widest line, which is effectively unbounded. A user bubble sizes itself
8 * from its content
9 * (`maxWidth` with no `width`), so Android lays the bubble's children out
10 * during the unclamped intrinsic pass — where the surrounding paragraphs
11 * collapse to a single line — and never repositions them once the width is
12 * clamped back to `maxWidth`. The result is siblings drawn on top of each
13 * other inside an over-tall bubble. Pinning the bubble's width removes the
14 * intrinsic pass entirely, which is the same reason review-comment bubbles
15 * already carry an explicit width.
16 *
17 * Ordered lists hit the same Android layout bug because each item contains a
18 * flexing content column inside a shrink-to-fit row.
19 *
20 * A four-space ordered marker is only treated as a list when the preceding
21 * non-empty line is a less-indented list item. This keeps standalone indented
22 * code out of the heuristic while covering nested lists.
23 */
24​
25const FENCED_CODE_BLOCK = /^ {0,3}(?:```|~~~)/m;
26// Trades some precision for a simple check, favoring false positives over false
27// negatives: list-shaped paragraph may get a wider bubble
28const ORDERED_LIST_ITEM = /^ {0,3}\d{1,9}[.)](?:[ \t]+|$)/;
29const INDENTED_ORDERED_LIST_ITEM = /^( {4,})\d{1,9}[.)](?:[ \t]+|$)/;
30const ANY_LIST_ITEM = /^( *)(?:[-+*]|\d{1,9}[.)])(?:[ \t]+|$)/;
31const BLOCKQUOTE_PREFIX = /^ {0,3}>[ \t]?/;
32​
33export interface WideMarkdownBlockOptions {
34 readonly includeOrderedLists?: boolean;
35 readonly includeBlockquotes?: boolean;
37​
38function stripBlockquotePrefixes(line: string): string {
39 let content = line;
40 while (BLOCKQUOTE_PREFIX.test(content)) {
41 content = content.replace(BLOCKQUOTE_PREFIX, "");
42 }
43 return content;
45​
46function hasBlockquote(text: string): boolean {
47 return text.split("\n").some((line) => BLOCKQUOTE_PREFIX.test(line));
49​
50function hasOrderedListItem(text: string): boolean {
51 let previousNonEmptyLine: string | null = null;
52​
53 for (const rawLine of text.split("\n")) {
54 const line = stripBlockquotePrefixes(rawLine);
55 if (ORDERED_LIST_ITEM.test(line)) {
56 return true;
57 }
58​
59 const nestedMatch = INDENTED_ORDERED_LIST_ITEM.exec(line);
60 const parentMatch =
61 previousNonEmptyLine === null ? null : ANY_LIST_ITEM.exec(previousNonEmptyLine);
62 if (nestedMatch && parentMatch && parentMatch[1].length < nestedMatch[1].length) {
63 return true;
64 }
65​
66 if (line.trim().length > 0) {
67 previousNonEmptyLine = line;
68 }
69 }
70​
71 return false;
73​
74function isTableDelimiterRow(line: string): boolean {
75 const trimmed = line.trim();
76 if (!trimmed.includes("|") || !trimmed.includes("-")) {
77 return false;
78 }
79 return /^[|\-: \t]+$/.test(trimmed);
81​
82export function hasWideMarkdownBlock(
83 text: string,
84 options: WideMarkdownBlockOptions = {},
85): boolean {
86 if (FENCED_CODE_BLOCK.test(text)) {
87 return true;
88 }
89 if (options.includeBlockquotes === true && hasBlockquote(text)) {
90 return true;
91 }
92 if (options.includeOrderedLists !== false && hasOrderedListItem(text)) {
93 return true;
94 }
95 if (!text.includes("|")) {
96 return false;
97 }
98 return text.split("\n").some(isTableDelimiterRow);