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
| 1 | /** |
| 2 | * Source rewriting for the dev-only copy editor. |
| 3 | * |
| 4 | * Elements marked with a bare `data-copy` attribute in HTML-like templates |
| 5 | * (`.astro`, `.html`, JSX, Vue, Svelte) are editable in dev. This module locates those elements in the raw |
| 6 | * source, stamps them with their location (dev), strips the attribute |
| 7 | * (build), and writes edited text back into the source. |
| 8 | * |
| 9 | * Only the text between tags is ever rewritten; tags, attributes, and |
| 10 | * unchanged words (including their line wrapping) are preserved verbatim. |
| 11 | */ |
| 12 | |
| 13 | import { marked } from "marked"; |
| 14 | import { |
| 15 | applyRuns, |
| 16 | CopyEditError, |
| 17 | decodeEntities, |
| 18 | htmlDialect, |
| 19 | type Run, |
| 20 | type TextEdit, |
| 21 | } from "./text.ts"; |
| 22 | |
| 23 | export const ATTR = "data-copy"; |
| 24 | |
| 25 | const VOID_ELEMENTS = new Set([ |
| 26 | "area", |
| 27 | "base", |
| 28 | "br", |
| 29 | "col", |
| 30 | "embed", |
| 31 | "hr", |
| 32 | "img", |
| 33 | "input", |
| 34 | "link", |
| 35 | "meta", |
| 36 | "param", |
| 37 | "source", |
| 38 | "track", |
| 39 | "wbr", |
| 40 | ]); |
| 41 | |
| 42 | interface Attr { |
| 43 | name: string; |
| 44 | /** Start of the attribute name. */ |
| 45 | start: number; |
| 46 | /** End of the attribute (after its value, if any). */ |
| 47 | end: number; |
| 48 | } |
| 49 | |
| 50 | type Token = |
| 51 | | { |
| 52 | kind: "open"; |
| 53 | name: string; |
| 54 | start: number; |
| 55 | end: number; |
| 56 | selfClosing: boolean; |
| 57 | attrs: Attr[]; |
| 58 | } |
| 59 | | { kind: "close"; name: string; start: number; end: number } |
| 60 | | { kind: "comment"; start: number; end: number }; |
| 61 | |
| 62 | const frontmatterEnd = (code: string): number => { |
| 63 | const match = /^\s*---\r?\n[\s\S]*?\r?\n---[^\n]*\n?/.exec(code); |
| 64 | return match ? match[0].length : 0; |
| 65 | }; |
| 66 | |
| 67 | /** Skips a balanced `{...}` expression starting at `i` (which must be `{`). */ |
| 68 | const skipBraces = (code: string, i: number): number => { |
| 69 | let depth = 0; |
| 70 | while (i < code.length) { |
| 71 | const ch = code[i]; |
| 72 | if (ch === '"' || ch === "'" || ch === "`") { |
| 73 | i = skipString(code, i); |
| 74 | continue; |
| 75 | } |
| 76 | if (ch === "{") depth++; |
| 77 | else if (ch === "}") { |
| 78 | depth--; |
| 79 | if (depth === 0) return i + 1; |
| 80 | } |
| 81 | i++; |
| 82 | } |
| 83 | return i; |
| 84 | }; |
| 85 | |
| 86 | const skipString = (code: string, i: number): number => { |
| 87 | const quote = code[i]; |
| 88 | i++; |
| 89 | while (i < code.length && code[i] !== quote) { |
| 90 | if (code[i] === "\\") i++; |
| 91 | i++; |
| 92 | } |
| 93 | return i + 1; |
| 94 | }; |
| 95 | |
| 96 | const NAME_START = /[A-Za-z]/; |
| 97 | |
| 98 | const parseOpenTag = (code: string, start: number): Token & { kind: "open" } => { |
| 99 | let i = start + 1; |
| 100 | const nameMatch = /^[A-Za-z][\w:.-]*/.exec(code.slice(i, i + 200)); |
| 101 | const name = nameMatch ? nameMatch[0] : ""; |
| 102 | i += name.length; |
| 103 | const attrs: Attr[] = []; |
| 104 | while (i < code.length) { |
| 105 | const ch = code[i]; |
| 106 | if (/\s/.test(ch)) { |
| 107 | i++; |
| 108 | } else if (ch === ">") { |
| 109 | return { |
| 110 | kind: "open", |
| 111 | name, |
| 112 | start, |
| 113 | end: i + 1, |
| 114 | selfClosing: false, |
| 115 | attrs, |
| 116 | }; |
| 117 | } else if (ch === "/" && code[i + 1] === ">") { |
| 118 | return { |
| 119 | kind: "open", |
| 120 | name, |
| 121 | start, |
| 122 | end: i + 2, |
| 123 | selfClosing: true, |
| 124 | attrs, |
| 125 | }; |
| 126 | } else if (ch === "{") { |
| 127 | i = skipBraces(code, i); |
| 128 | } else { |
| 129 | const attrStart = i; |
| 130 | while ( |
| 131 | i < code.length && |
| 132 | !/[\s=>{]/.test(code[i]) && |
| 133 | !(code[i] === "/" && code[i + 1] === ">") |
| 134 | ) { |
| 135 | i++; |
| 136 | } |
| 137 | const attrName = code.slice(attrStart, i); |
| 138 | let j = i; |
| 139 | while (/\s/.test(code[j] ?? "")) j++; |
| 140 | if (code[j] === "=") { |
| 141 | j++; |
| 142 | while (/\s/.test(code[j] ?? "")) j++; |
| 143 | const q = code[j]; |
| 144 | if (q === '"' || q === "'" || q === "`") j = skipString(code, j); |
| 145 | else if (q === "{") j = skipBraces(code, j); |
| 146 | else while (j < code.length && !/[\s>]/.test(code[j])) j++; |
| 147 | i = j; |
| 148 | } |
| 149 | attrs.push({ name: attrName, start: attrStart, end: i }); |
| 150 | if (i === attrStart) i++; |
| 151 | } |
| 152 | } |
| 153 | return { |
| 154 | kind: "open", |
| 155 | name, |
| 156 | start, |
| 157 | end: code.length, |
| 158 | selfClosing: true, |
| 159 | attrs, |
| 160 | }; |
| 161 | }; |
| 162 | |
| 163 | /** Tokenizes the template part of an `.astro` file into tags and comments. */ |
| 164 | const tokenize = (code: string): Token[] => { |
| 165 | const tokens: Token[] = []; |
| 166 | let i = frontmatterEnd(code); |
| 167 | while (i < code.length) { |
| 168 | if (code[i] !== "<") { |
| 169 | i++; |
| 170 | continue; |
| 171 | } |
| 172 | if (code.startsWith("<!--", i)) { |
| 173 | const end = code.indexOf("-->", i); |
| 174 | const stop = end === -1 ? code.length : end + 3; |
| 175 | tokens.push({ kind: "comment", start: i, end: stop }); |
| 176 | i = stop; |
| 177 | continue; |
| 178 | } |
| 179 | const next = code[i + 1] ?? ""; |
| 180 | if (next === "/") { |
| 181 | const m = /^<\/([A-Za-z][\w:.-]*)?\s*>/.exec(code.slice(i, i + 200)); |
| 182 | if (m) { |
| 183 | tokens.push({ |
| 184 | kind: "close", |
| 185 | name: m[1] ?? "", |
| 186 | start: i, |
| 187 | end: i + m[0].length, |
| 188 | }); |
| 189 | i += m[0].length; |
| 190 | continue; |
| 191 | } |
| 192 | i++; |
| 193 | continue; |
| 194 | } |
| 195 | if (next === ">" || NAME_START.test(next)) { |
| 196 | const tag = parseOpenTag(code, i); |
| 197 | if (VOID_ELEMENTS.has(tag.name.toLowerCase())) tag.selfClosing = true; |
| 198 | tokens.push(tag); |
| 199 | i = tag.end; |
| 200 | const raw = tag.name.toLowerCase(); |
| 201 | if (!tag.selfClosing && (raw === "script" || raw === "style")) { |
| 202 | const close = code.toLowerCase().indexOf(`</${raw}`, i); |
| 203 | i = close === -1 ? code.length : close; |
| 204 | } |
| 205 | continue; |
| 206 | } |
| 207 | i++; |
| 208 | } |
| 209 | return tokens; |
| 210 | }; |
| 211 | |
| 212 | const copyTags = (tokens: Token[]) => |
| 213 | tokens.flatMap((token, index) => { |
| 214 | if (token.kind !== "open") return []; |
| 215 | const attr = token.attrs.find((a) => a.name === ATTR); |
| 216 | return attr ? [{ token, index, attr }] : []; |
| 217 | }); |
| 218 | |
| 219 | /** |
| 220 | * Replaces each `data-copy` attribute with `data-copy="<file>#<n>"`, plus |
| 221 | * `data-copy-format="inline"` when the element's content can round-trip |
| 222 | * through inline markdown (text, `code`, `strong`, `em`, `del`, `a`). |
| 223 | */ |
| 224 | export const annotate = (code: string, file: string): string => { |
| 225 | if (!code.includes(ATTR)) return code; |
| 226 | const tags = copyTags(tokenize(code)); |
| 227 | let out = code; |
| 228 | for (let n = tags.length - 1; n >= 0; n--) { |
| 229 | const { attr } = tags[n]; |
| 230 | let inline = true; |
| 231 | try { |
| 232 | inlineSource(code, n); |
| 233 | } catch { |
| 234 | inline = false; |
| 235 | } |
| 236 | const format = inline ? ` ${ATTR}-format="inline"` : ""; |
| 237 | out = `${out.slice(0, attr.start)}${ATTR}="${file}#${n}"${format}${out.slice(attr.end)}`; |
| 238 | } |
| 239 | return out; |
| 240 | }; |
| 241 | |
| 242 | /** Removes every `data-copy` attribute (and the whitespace before it). */ |
| 243 | export const strip = (code: string): string => { |
| 244 | if (!code.includes(ATTR)) return code; |
| 245 | const tags = copyTags(tokenize(code)); |
| 246 | let out = code; |
| 247 | for (let n = tags.length - 1; n >= 0; n--) { |
| 248 | const { attr } = tags[n]; |
| 249 | let start = attr.start; |
| 250 | while (start > 0 && /\s/.test(out[start - 1])) start--; |
| 251 | out = out.slice(0, start) + out.slice(attr.end); |
| 252 | } |
| 253 | return out; |
| 254 | }; |
| 255 | |
| 256 | /** The `n`-th `data-copy` element: its open tag and the tokens inside it. */ |
| 257 | const elementOf = (code: string, n: number) => { |
| 258 | const tokens = tokenize(code); |
| 259 | const tags = copyTags(tokens); |
| 260 | const target = tags[n]; |
| 261 | if (!target) { |
| 262 | throw new CopyEditError(`No data-copy element #${n} in this file. Reload the page.`, 409); |
| 263 | } |
| 264 | const { token: open, index } = target; |
| 265 | if (open.selfClosing) throw new CopyEditError("data-copy is on an empty element."); |
| 266 | let depth = 1; |
| 267 | let closeIndex = -1; |
| 268 | for (let k = index + 1; k < tokens.length; k++) { |
| 269 | const t = tokens[k]; |
| 270 | if (t.kind === "open" && t.name === open.name && !t.selfClosing) depth++; |
| 271 | if (t.kind === "close" && t.name === open.name && --depth === 0) { |
| 272 | closeIndex = k; |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | if (closeIndex === -1) throw new CopyEditError(`No closing </${open.name}> found.`); |
| 277 | return { |
| 278 | open, |
| 279 | inner: tokens.slice(index + 1, closeIndex), |
| 280 | close: tokens[closeIndex] as Token & { kind: "close" }, |
| 281 | }; |
| 282 | }; |
| 283 | |
| 284 | /** The text runs between tags inside the `n`-th `data-copy` element. */ |
| 285 | const segmentsOf = (code: string, n: number): Run[] => { |
| 286 | const { open, inner, close } = elementOf(code, n); |
| 287 | const segments: Run[] = []; |
| 288 | let cursor = open.end; |
| 289 | for (const t of [...inner, close]) { |
| 290 | if (t.kind === "comment") throw new CopyEditError("Editable copy can't contain HTML comments."); |
| 291 | if ((t.kind === "open" || t.kind === "close") && /^[A-Z]/.test(t.name)) { |
| 292 | throw new CopyEditError(`Editable copy can't contain components (<${t.name}>).`); |
| 293 | } |
| 294 | if (t.kind === "open" && /^(script|style)$/i.test(t.name)) { |
| 295 | throw new CopyEditError("Editable copy can't contain <script> or <style>."); |
| 296 | } |
| 297 | segments.push({ start: cursor, end: t.start }); |
| 298 | cursor = t.end; |
| 299 | } |
| 300 | return segments; |
| 301 | }; |
| 302 | |
| 303 | export interface EditRequest extends TextEdit { |
| 304 | /** Index of the `data-copy` element within the file. */ |
| 305 | index: number; |
| 306 | } |
| 307 | |
| 308 | /** Applies an edit to an HTML-like template and returns the new source. */ |
| 309 | export const applyEdit = (code: string, { index, before, after }: EditRequest): string => |
| 310 | applyRuns(code, segmentsOf(code, index), htmlDialect, { before, after }); |
| 311 | |
| 312 | // ── inline markdown ─────────────────────────────────────────────────────── |
| 313 | |
| 314 | const INLINE_TAGS: Record<string, [open: string, close: string]> = { |
| 315 | code: ["`", "`"], |
| 316 | strong: ["**", "**"], |
| 317 | b: ["**", "**"], |
| 318 | em: ["*", "*"], |
| 319 | i: ["*", "*"], |
| 320 | del: ["~~", "~~"], |
| 321 | s: ["~~", "~~"], |
| 322 | a: ["[", "]"], |
| 323 | }; |
| 324 | /** The tag inline markdown produces for each markdown construct. */ |
| 325 | const MARKDOWN_TAG: Record<string, string> = { |
| 326 | b: "strong", |
| 327 | i: "em", |
| 328 | s: "del", |
| 329 | }; |
| 330 | |
| 331 | const escapeMarkdown = (text: string) => |
| 332 | text |
| 333 | .replace(/[\\`*[\]<]/g, "\\$&") |
| 334 | .replace(/(^|\W)_|_(?=\W|$)/g, (m) => m.replace("_", "\\_")) |
| 335 | .replace(/~~/g, "\\~\\~"); |
| 336 | |
| 337 | const hrefOf = (code: string, attrs: Attr[]) => { |
| 338 | const attr = attrs.find((a) => a.name === "href"); |
| 339 | if (!attr) return undefined; |
| 340 | const m = /^href\s*=\s*(["'])([^"'{}]*)\1$/.exec(code.slice(attr.start, attr.end)); |
| 341 | if (!m) throw new CopyEditError("Links need a literal href to edit as markdown."); |
| 342 | return decodeEntities(m[2]!); |
| 343 | }; |
| 344 | |
| 345 | /** |
| 346 | * The `n`-th `data-copy` element's content as inline markdown, plus what's |
| 347 | * needed to write markdown back: the content's source range and the |
| 348 | * attributes each tag carries in the source. |
| 349 | */ |
| 350 | export const inlineSource = (code: string, n: number) => { |
| 351 | const { open, inner, close } = elementOf(code, n); |
| 352 | const templates: Record<string, string> = {}; |
| 353 | const links: string[] = []; |
| 354 | let md = ""; |
| 355 | let inCode = false; |
| 356 | let skipSpace = false; |
| 357 | let cursor = open.end; |
| 358 | const text = (raw: string) => { |
| 359 | const words = htmlDialect |
| 360 | .tokenize(raw, "text") |
| 361 | .map((t) => t.text) |
| 362 | .join(""); |
| 363 | let collapsed = words.replace(/\s+/g, " "); |
| 364 | if (skipSpace) collapsed = collapsed.replace(/^ /, ""); |
| 365 | if (collapsed) skipSpace = false; |
| 366 | md += inCode ? collapsed : escapeMarkdown(collapsed); |
| 367 | }; |
| 368 | for (const t of inner) { |
| 369 | text(code.slice(cursor, t.start)); |
| 370 | cursor = t.end; |
| 371 | if (t.kind === "comment") throw new CopyEditError("HTML comments can't be edited as markdown."); |
| 372 | const name = t.name.toLowerCase(); |
| 373 | if (name === "br" && t.kind === "open" && !inCode) { |
| 374 | // A line break: the whitespace around it isn't content. |
| 375 | md = `${md.replace(/ +$/, "")}\n`; |
| 376 | skipSpace = true; |
| 377 | continue; |
| 378 | } |
| 379 | const syntax = INLINE_TAGS[name]; |
| 380 | if (!syntax || (inCode && t.kind === "open")) { |
| 381 | throw new CopyEditError(`<${t.name}> can't be edited as markdown.`); |
| 382 | } |
| 383 | if (t.kind === "open") { |
| 384 | if (t.selfClosing) throw new CopyEditError(`<${t.name}/> can't be edited as markdown.`); |
| 385 | const tag = MARKDOWN_TAG[name] ?? name; |
| 386 | if (!(tag in templates)) { |
| 387 | const attrs = t.attrs |
| 388 | .filter((a) => a.name !== "href") |
| 389 | .map((a) => code.slice(a.start, a.end)) |
| 390 | .join(" "); |
| 391 | templates[tag] = attrs ? ` ${attrs}` : ""; |
| 392 | } |
| 393 | if (name === "a") links.push(hrefOf(code, t.attrs) ?? ""); |
| 394 | if (name === "code") inCode = true; |
| 395 | md += syntax[0]; |
| 396 | } else { |
| 397 | if (name === "code") inCode = false; |
| 398 | md += name === "a" ? `](${links.pop() ?? ""})` : syntax[1]; |
| 399 | } |
| 400 | } |
| 401 | text(code.slice(cursor, close.start)); |
| 402 | const raw = code.slice(open.end, close.start); |
| 403 | return { |
| 404 | markdown: md.trim(), |
| 405 | start: open.end, |
| 406 | end: close.start, |
| 407 | lead: /^\s*/.exec(raw)![0], |
| 408 | trail: /\s*$/.exec(raw)![0], |
| 409 | templates, |
| 410 | }; |
| 411 | }; |
| 412 | |
| 413 | /** Inline markdown as template HTML, reusing each tag's source attributes. */ |
| 414 | export const inlineHtml = (markdown: string, templates: Record<string, string>) => { |
| 415 | // Each line break in the markdown becomes a <br>. |
| 416 | const html = marked.parseInline(markdown.replace(/[ \t]*\n\s*/g, "\n"), { |
| 417 | async: false, |
| 418 | gfm: true, |
| 419 | breaks: true, |
| 420 | }); |
| 421 | return html |
| 422 | .split(/(<[^>]+>)/) |
| 423 | .map((part, i) => { |
| 424 | if (i % 2 === 0) { |
| 425 | return part |
| 426 | .replace(/"/g, '"') |
| 427 | .replace(/'/g, "'") |
| 428 | .replace(/\{/g, "{") |
| 429 | .replace(/\}/g, "}"); |
| 430 | } |
| 431 | const tag = /^<\/?([a-z]+)(\s[^>]*)?>$/.exec(part); |
| 432 | if (!tag || !(tag[1]! in INLINE_TAGS || tag[1] === "br")) { |
| 433 | // Typed HTML that isn't an inline markdown construct stays text. |
| 434 | return part.replace(/</g, "<").replace(/>/g, ">"); |
| 435 | } |
| 436 | if (part.startsWith("</")) return part; |
| 437 | const [, name, attrs = ""] = tag; |
| 438 | if (!(name! in templates)) return part.replace(/[{}]/g, ""); |
| 439 | const href = /\shref="[^"]*"/.exec(attrs)?.[0] ?? ""; |
| 440 | return `<${name}${href}${templates[name!]}>`; |
| 441 | }) |
| 442 | .join(""); |
| 443 | }; |
| 444 | |
| 445 | /** Replaces the `n`-th element's content with `markdown` rendered as HTML. */ |
| 446 | export const writeInline = ( |
| 447 | code: string, |
| 448 | n: number, |
| 449 | { markdown, base }: { markdown: string; base: string }, |
| 450 | ): string => { |
| 451 | const current = inlineSource(code, n); |
| 452 | if (current.markdown !== base.trim()) { |
| 453 | throw new CopyEditError( |
| 454 | "This copy changed since you started editing. Copy your text, reload, and try again.", |
| 455 | 409, |
| 456 | ); |
| 457 | } |
| 458 | if (markdown.trim() === current.markdown) return code; |
| 459 | const html = inlineHtml(markdown.trim(), current.templates); |
| 460 | return ( |
| 461 | code.slice(0, current.start) + current.lead + html + current.trail + code.slice(current.end) |
| 462 | ); |
| 463 | }; |