@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 104 lines 2.79 KB
Raw
1 /** Android never repositions children after maxWidth clamps; wide blocks and ordered lists need definite width. */
2
3 const FENCED_CODE_BLOCK = /^ {0,3}(?:```|~~~)/m;
4 const ORDERED_LIST_ITEM = /^ {0,3}\d{1,9}[.)](?:[ \t]+|$)/;
5 const INDENTED_ORDERED_LIST_ITEM = /^( {4,})\d{1,9}[.)](?:[ \t]+|$)/;
6 const ANY_LIST_ITEM = /^( *)(?:[-+*]|\d{1,9}[.)])(?:[ \t]+|$)/;
7 const BLOCKQUOTE_PREFIX = /^ {0,3}>[ \t]?/;
8
9 export interface WideMarkdownBlockOptions {
10 readonly includeOrderedLists?: boolean;
11 readonly includeBlockquotes?: boolean;
12 }
13
14 function stripBlockquotePrefixes(line: string): string {
15 let content = line;
16 while (BLOCKQUOTE_PREFIX.test(content)) {
17 content = content.replace(BLOCKQUOTE_PREFIX, "");
18 }
19 return content;
20 }
21
22 function hasIndentedCodeBlock(text: string): boolean {
23 return text.split("\n").some((rawLine) => {
24 const line = stripBlockquotePrefixes(rawLine);
25 let column = 0;
26 let index = 0;
27
28 // Markdown tabs advance to the next four-column stop.
29 while (index < line.length) {
30 if (line[index] === " ") {
31 column += 1;
32 } else if (line[index] === "\t") {
33 column += 4 - (column % 4);
34 } else {
35 break;
36 }
37 index += 1;
38 }
39
40 return column >= 4 && index < line.length && line[index] !== "\r";
41 });
42 }
43
44 function hasBlockquote(text: string): boolean {
45 return text.split("\n").some((line) => BLOCKQUOTE_PREFIX.test(line));
46 }
47
48 function hasOrderedListItem(text: string): boolean {
49 let previousNonEmptyLine: string | null = null;
50
51 for (const rawLine of text.split("\n")) {
52 const line = stripBlockquotePrefixes(rawLine);
53 if (ORDERED_LIST_ITEM.test(line)) {
54 return true;
55 }
56
57 const nestedMatch = INDENTED_ORDERED_LIST_ITEM.exec(line);
58 const parentMatch =
59 previousNonEmptyLine === null ? null : ANY_LIST_ITEM.exec(previousNonEmptyLine);
60 if (
61 nestedMatch?.[1] !== undefined &&
62 parentMatch?.[1] !== undefined &&
63 parentMatch[1].length < nestedMatch[1].length
64 ) {
65 return true;
66 }
67
68 if (line.trim().length > 0) {
69 previousNonEmptyLine = line;
70 }
71 }
72
73 return false;
74 }
75
76 function isTableDelimiterRow(line: string): boolean {
77 const trimmed = line.trim();
78 if (!trimmed.includes("|") || !trimmed.includes("-")) {
79 return false;
80 }
81 return /^[|\-: \t]+$/.test(trimmed);
82 }
83
84 export function hasWideMarkdownBlock(
85 text: string,
86 options: WideMarkdownBlockOptions = {},
87 ): boolean {
88 if (FENCED_CODE_BLOCK.test(text)) {
89 return true;
90 }
91 if (options.includeBlockquotes === true && hasBlockquote(text)) {
92 return true;
93 }
94 if (hasIndentedCodeBlock(text)) {
95 return true;
96 }
97 if (options.includeOrderedLists !== false && hasOrderedListItem(text)) {
98 return true;
99 }
100 if (!text.includes("|")) {
101 return false;
102 }
103 return text.split("\n").some(isTableDelimiterRow);
104 }