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

main
ts 284 lines 8.44 KB
Raw
1 /**
2 * Markdown prose as an editable source.
3 *
4 * {@link markdownRuns} splits one block of inline markdown (a paragraph,
5 * heading, or list item) into the runs its rendered text nodes come from:
6 * text between emphasis delimiters, code spans, link text, and so on. The
7 * markdown can live inside another file (a JSDoc comment, a string) — the
8 * `linePrefix` option describes text that starts each continuation line.
9 */
10 import { CopyEditError, decodeEntities, type Dialect, type Run, type TextToken } from "./text.ts";
11
12 /** A syntax whose rendered text isn't literal source text (e.g. `{@link X}`). */
13 export interface Atomic {
14 /** Matched at the current position (compiled with the sticky flag). */
15 pattern: RegExp;
16 /** The rendered text for a match (continuation-line prefixes removed). */
17 text: (match: RegExpExecArray) => string;
18 }
19
20 export interface MarkdownOptions {
21 /**
22 * Source of a regex matching what follows `\n` on continuation lines and
23 * isn't content, e.g. `"[ \\t]*\\*(?!/)[ \\t]?"` for JSDoc's ` * `.
24 */
25 linePrefix?: string;
26 /** Extra inline syntaxes rendered as generated, non-editable text. */
27 atomics?: Atomic[];
28 }
29
30 const PUNCT = /[!-/:-@[-`{-~\p{P}\p{S}]/u;
31 const isWs = (ch: string | undefined) => ch === undefined || /\s/.test(ch);
32 const isPunct = (ch: string | undefined) => ch !== undefined && PUNCT.test(ch);
33
34 /** Straight quotes, dashes, and dots: how smart punctuation renders them. */
35 const canon = (text: string) =>
36 text
37 .replace(/[“”„]/g, '"')
38 .replace(/[‘’‚]/g, "'")
39 .replace(/—/g, "---")
40 .replace(/–/g, "--")
41 .replace(/…/g, "...")
42 .replace(/\u00a0/g, " ");
43
44 const decodeMarkdown = (raw: string) => decodeEntities(raw.replace(/\\([!-/:-@[-`{-~])/g, "$1"));
45
46 export const markdownDialect = (options: MarkdownOptions = {}): Dialect => {
47 const ws = new RegExp(
48 `(?:[ \\t\\r\\f]|\\n${options.linePrefix ? `(?:${options.linePrefix})?` : ""})+`,
49 "y",
50 );
51 return {
52 tokenize(raw, kind) {
53 const tokens: TextToken[] = [];
54 let i = 0;
55 while (i < raw.length) {
56 ws.lastIndex = i;
57 const m = ws.exec(raw);
58 if (m && m[0].length > 0) {
59 tokens.push({ raw: m[0], ws: true, text: " " });
60 i += m[0].length;
61 continue;
62 }
63 let j = i;
64 while (j < raw.length && !/[ \t\r\f\n]/.test(raw[j])) j++;
65 const word = raw.slice(i, j);
66 tokens.push({
67 raw: word,
68 ws: false,
69 text: kind === "code" ? word : decodeMarkdown(word),
70 });
71 i = j;
72 }
73 return tokens;
74 },
75 compare: canon,
76 escape(word, { kind, lineStart }) {
77 if (kind === "code") {
78 if (word.includes("`") || word.includes("*/")) {
79 throw new CopyEditError("Code spans can't contain ` or */.");
80 }
81 return word;
82 }
83 let out = word
84 .replace(/[“”]/g, '"')
85 .replace(/[‘’]/g, "'")
86 .replace(/…/g, "...")
87 .replace(/[\\`*[\]<]/g, "\\$&")
88 .replace(/^_|_$/g, "\\_")
89 .replace(/~~/g, "\\~\\~")
90 .replace(/\{@/g, "{\\@");
91 if (lineStart) {
92 out = out.replace(/^[#>+\-=|:@]/, "\\$&").replace(/^(\d+)([.)])/, "$1\\$2");
93 }
94 // Never let prose close a surrounding block comment.
95 return out.replace(/\*\//g, "*\\/");
96 },
97 };
98 };
99
100 /**
101 * Splits the inline markdown in `code[start, end)` into runs matching its
102 * rendered text nodes. The first run starts a line.
103 */
104 export const markdownRuns = (
105 code: string,
106 start: number,
107 end: number,
108 options: MarkdownOptions = {},
109 ): Run[] => {
110 const prefix = options.linePrefix ? new RegExp(`\\n(?:${options.linePrefix})`, "g") : undefined;
111 const clean = (raw: string) => (prefix ? raw.replace(prefix, "\n") : raw);
112 const atomics = (options.atomics ?? []).map(({ pattern, text }) => ({
113 pattern: new RegExp(pattern.source, `${pattern.flags.replace(/[gy]/g, "")}y`),
114 text,
115 }));
116
117 const runs: Run[] = [];
118 let runStart = start;
119 const boundary = (at: number, resume: number) => {
120 runs.push({ start: runStart, end: at, lineStart: runs.length === 0 });
121 runStart = resume;
122 };
123 const locked = (at: number, resume: number, expected: string) => {
124 boundary(at, at);
125 runs.push({ start: at, end: resume, kind: "locked", expected });
126 runStart = resume;
127 };
128 // Positions of `]` that close link text, mapped to the end of `](...)`.
129 const linkEnds = new Map<number, number>();
130
131 let i = start;
132 while (i < end) {
133 const ch = code[i];
134
135 if (ch === "\\" && i + 1 < end && /[!-/:-@[-`{-~]/.test(code[i + 1])) {
136 i += 2;
137 continue;
138 }
139
140 const atomic = atomics.find((a) => {
141 a.pattern.lastIndex = i;
142 return a.pattern.test(code) && a.pattern.lastIndex <= end;
143 });
144 if (atomic) {
145 atomic.pattern.lastIndex = i;
146 const m = atomic.pattern.exec(code)!;
147 const cleaned = Object.assign(m.map(clean), {
148 index: m.index,
149 input: m.input,
150 });
151 locked(i, i + m[0].length, atomic.text(cleaned as RegExpExecArray));
152 i += m[0].length;
153 continue;
154 }
155
156 if (ch === "`") {
157 let n = 1;
158 while (code[i + n] === "`") n++;
159 const fence = "`".repeat(n);
160 let close = code.indexOf(fence, i + n);
161 while (close !== -1 && close < end && code[close + n] === "`") {
162 close = code.indexOf(fence, close + n + 1);
163 }
164 if (close !== -1 && close + n <= end) {
165 boundary(i, i + n);
166 runs.push({ start: i + n, end: close, kind: "code" });
167 runStart = close + n;
168 i = close + n;
169 continue;
170 }
171 i += n;
172 continue;
173 }
174
175 if (ch === "<") {
176 const autolink = /^<(https?:\/\/[^\s<>]+)>/.exec(code.slice(i, end));
177 if (autolink) {
178 locked(i, i + autolink[0].length, autolink[1]);
179 i += autolink[0].length;
180 continue;
181 }
182 const tag = /^<\/?[A-Za-z][^<>]*>|^<!--[\s\S]*?-->/.exec(code.slice(i, end));
183 if (tag) {
184 boundary(i, i + tag[0].length);
185 i += tag[0].length;
186 continue;
187 }
188 }
189
190 if (
191 ((ch === "h" || ch === "w") && isWs(code[i - 1])) ||
192 (i === start && (ch === "h" || ch === "w"))
193 ) {
194 const url = /^(?:https?:\/\/|www\.)[^\s<]*[^\s<.,:;"')\]!?*_~]/.exec(code.slice(i, end));
195 if (url) {
196 locked(i, i + url[0].length, url[0]);
197 i += url[0].length;
198 continue;
199 }
200 }
201
202 if (ch === "!" && code[i + 1] === "[") {
203 const img = /^!\[[^\]]*\]\([^)]*\)/.exec(code.slice(i, end));
204 if (img) {
205 boundary(i, i + img[0].length);
206 i += img[0].length;
207 continue;
208 }
209 }
210
211 if (ch === "[") {
212 const close = matchLink(code, i, end);
213 if (close) {
214 linkEnds.set(close.bracket, close.end);
215 boundary(i, i + 1);
216 i += 1;
217 continue;
218 }
219 }
220 if (ch === "]" && linkEnds.has(i)) {
221 const after = linkEnds.get(i)!;
222 boundary(i, after);
223 i = after;
224 continue;
225 }
226
227 if (ch === "*" || ch === "_" || ch === "~") {
228 let n = 1;
229 while (code[i + n] === ch) n++;
230 const prev = code[i - 1];
231 const next = code[i + n];
232 const left = !isWs(next) && (!isPunct(next) || isWs(prev) || isPunct(prev));
233 const right = !isWs(prev) && (!isPunct(prev) || isWs(next) || isPunct(next));
234 const delimiter =
235 ch === "~"
236 ? n === 2 && (left || right)
237 : ch === "*"
238 ? left || right
239 : (left && (!right || isPunct(prev))) || (right && (!left || isPunct(next)));
240 if (delimiter) {
241 boundary(i, i + n);
242 i += n;
243 continue;
244 }
245 i += n;
246 continue;
247 }
248
249 i++;
250 }
251 boundary(end, end);
252 return runs;
253 };
254
255 /** Finds the `]` closing `[` at `open` when followed by `(destination)`. */
256 const matchLink = (code: string, open: number, end: number) => {
257 let depth = 0;
258 for (let i = open; i < end; i++) {
259 const ch = code[i];
260 if (ch === "\\") {
261 i++;
262 continue;
263 }
264 if (ch === "`") {
265 const close = code.indexOf("`", i + 1);
266 if (close === -1 || close >= end) return undefined;
267 i = close;
268 continue;
269 }
270 if (ch === "[") depth++;
271 else if (ch === "]" && --depth === 0) {
272 if (code[i + 1] !== "(") return undefined;
273 let parens = 0;
274 for (let j = i + 1; j < end; j++) {
275 if (code[j] === "(") parens++;
276 else if (code[j] === ")" && --parens === 0) {
277 return { bracket: i, end: j + 1 };
278 }
279 }
280 return undefined;
281 }
282 }
283 return undefined;
284 };