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

main
ts 922 lines 34.5 KB
Raw
1 import { isLocalEditorFileUrl, LOCAL_FILE_LINK_SCHEMES } from "@t3tools/contracts";
2 import type {
3 Heading,
4 Link,
5 Paragraph,
6 PhrasingContent,
7 Root,
8 RootContent,
9 TableCell,
10 Text,
11 } from "mdast";
12 import { remarkKeepWindowsPaths } from "./markdownWindowsPaths.ts";
13 import { type PluggableList, type Processor, type Transformer, unified } from "unified";
14 import remarkParse from "remark-parse";
15 import remarkRehype from "remark-rehype";
16 import { find, html } from "property-information";
17 import rehypeRaw from "rehype-raw";
18 import rehypeSanitize, { defaultSchema } from "rehype-sanitize";
19 import remarkBreaks from "remark-breaks";
20 import remarkGfm from "remark-gfm";
21 import { remarkGithubAlerts } from "./markdownGithubAlerts.ts";
22 import { remarkNormalizeListItemIndentation } from "./markdownListIndentation.ts";
23 import { remarkKeepOrderedListNumbers } from "./markdownOrderedListNumbers.ts";
24 import {
25 remarkEnableLeadingWhitespace,
26 remarkKeepLeadingWhitespace,
27 } from "./markdownLeadingWhitespace.ts";
28 import {
29 CODEX_ARTIFACT_TEMPLATE_HAST_PROPERTIES,
30 remarkCodexDirectives,
31 } from "./codexMarkdownDirectives.ts";
32 import {
33 absoluteFilePathMatches,
34 isWindowsDrivePathHref,
35 normalizeMarkdownLinkDestination,
36 } from "./markdownLinks.ts";
37 import { isWindowsAbsolutePath } from "./path.ts";
38 import { THREAD_LINK_PROTOCOL } from "./threadLinks.ts";
39
40 export { remarkEnableOrderedListNumbers } from "./markdownOrderedListNumbers.ts";
41
42 type MarkdownImageHastNode = {
43 type?: string;
44 value?: string;
45 tagName?: string;
46 properties?: Record<string, unknown>;
47 children?: MarkdownImageHastNode[];
48 };
49
50 function meaningfulHastChildren(node: MarkdownImageHastNode): MarkdownImageHastNode[] {
51 return (node.children ?? []).filter(
52 (child) => !(child.type === "text" && (child as { value?: string }).value?.trim() === ""),
53 );
54 }
55
56 const STANDALONE_IMAGE_BLOCKS = new Set([
57 "p",
58 "div",
59 "li",
60 "td",
61 "th",
62 "figure",
63 "center",
64 "blockquote",
65 ]);
66
67 function soleImageDescendant(node: MarkdownImageHastNode): MarkdownImageHastNode | undefined {
68 const children = meaningfulHastChildren(node);
69 if (children.length !== 1) return undefined;
70 const only = children[0];
71 if (only?.type !== "element") return undefined;
72 if (only.tagName === "img") return only;
73 return only.tagName === "a" || only.tagName === "strong" || only.tagName === "em"
74 ? soleImageDescendant(only)
75 : undefined;
76 }
77
78 function markStandaloneImages(node: MarkdownImageHastNode) {
79 if (node.type === "root" || (node.tagName && STANDALONE_IMAGE_BLOCKS.has(node.tagName))) {
80 const image = soleImageDescendant(node);
81 if (image) image.properties = { ...image.properties, dataStandalone: true };
82 }
83 node.children?.forEach((child) => {
84 if (child.type === "element") markStandaloneImages(child);
85 });
86 }
87
88 // Whole tags, attributes, and comments, so closing-anchor text inside them never counts as a tag.
89 const RAW_HTML_TAG_PATTERN = /<!--[\s\S]*?(?:-->|$)|<\/?[A-Za-z](?:[^"'<>]|"[^"]*"|'[^']*')*>/g;
90
91 function rehypePreserveBareAnchorPlaceholders() {
92 return (tree: MarkdownImageHastNode) => {
93 const anchors: Array<MarkdownImageHastNode | null> = [];
94 let rawTextTag: string | undefined;
95 const visit = (node: MarkdownImageHastNode) => {
96 if (node.type === "raw" && typeof node.value === "string") {
97 const tags = new RegExp(RAW_HTML_TAG_PATTERN);
98 let offset = 0;
99 while (rawTextTag !== "plaintext") {
100 // Raw text ends at its closing tag even inside comment-looking text.
101 const matcher = rawTextTag ? new RegExp(`</${rawTextTag}\\s*>`, "gi") : tags;
102 matcher.lastIndex = offset;
103 const match = matcher.exec(node.value);
104 if (!match) break;
105 const [tag] = match;
106 offset = matcher.lastIndex;
107 if (rawTextTag) {
108 rawTextTag = undefined;
109 continue;
110 }
111 if (tag.startsWith("<!--")) continue;
112 const closing = /^<\/([a-z]+)\s*>$/i.exec(tag)?.[1]?.toLowerCase();
113 const opening = /^<([a-z]+)(?:\s|\/?>)/i.exec(tag)?.[1]?.toLowerCase();
114 if (
115 opening &&
116 /^(?:script|style|textarea|title|xmp|iframe|noembed|noframes|plaintext)$/.test(opening)
117 ) {
118 rawTextTag = opening;
119 } else if (opening === "a") {
120 anchors.push(node.value === tag && /^<a\s*\/?>$/i.test(tag) ? node : null);
121 } else if (closing === "a") {
122 anchors.pop();
123 }
124 }
125 }
126 node.children?.forEach(visit);
127 };
128
129 visit(tree);
130 for (const anchor of anchors) {
131 if (anchor) anchor.type = "text";
132 }
133 };
134 }
135
136 const COLOR_SCHEME_MEDIA = /^\(\s*prefers-color-scheme\s*:\s*(dark|light)\s*\)$/i;
137
138 /**
139 * The renderer resolves an `<img>`'s `src` (workspace files, host media) but never a `<source>`'s
140 * `srcset`, which would load unresolved whenever its media matched. A color-scheme source moves
141 * onto the `<img>` for the renderer to pick by theme; every source is then dropped.
142 */
143 function moveColorSchemeSourcesToImage(picture: MarkdownImageHastNode) {
144 if (!picture.children) return;
145 const image = picture.children.find((child) => child.tagName === "img");
146 for (const source of picture.children) {
147 if (source.tagName !== "source" || !image) continue;
148 const media = source.properties?.media;
149 const scheme = typeof media === "string" ? COLOR_SCHEME_MEDIA.exec(media)?.[1] : undefined;
150 const srcSet = source.properties?.srcSet;
151 const candidate = Array.isArray(srcSet) ? srcSet[0] : srcSet;
152 // A candidate's URL runs to the first space (a descriptor such as `2x` may follow).
153 const url =
154 typeof candidate === "string" ? candidate.trim().split(/\s/, 1)[0]?.replace(/,+$/, "") : "";
155 const key = scheme?.toLowerCase() === "dark" ? "dataDarkSrc" : "dataLightSrc";
156 // As in a browser, the first source that matches a scheme is the one shown.
157 if (scheme && url && image.properties?.[key] === undefined) {
158 // The sanitizer reads a drive letter as a protocol; as a file URL it resolves to the same path.
159 const value = isWindowsDrivePathHref(url) ? `file:///${url.replaceAll("\\", "/")}` : url;
160 image.properties = { ...image.properties, [key]: value };
161 }
162 }
163 picture.children = picture.children.filter((child) => child.tagName !== "source");
164 }
165
166 function rehypePreserveImageSourceMeta() {
167 return (tree: MarkdownImageHastNode) => {
168 const visit = (node: MarkdownImageHastNode) => {
169 const src = node.properties?.src;
170 const title = node.properties?.title;
171 if (node.type === "element" && node.tagName === "picture") {
172 moveColorSchemeSourcesToImage(node);
173 }
174 if (node.type === "element" && node.tagName === "img") {
175 node.properties = {
176 ...node.properties,
177 ...(typeof src === "string" && isWindowsDrivePathHref(src) ? { dataLocalSrc: src } : {}),
178 ...(typeof title === "string" ? { dataMarkdownTitle: title } : {}),
179 };
180 }
181 node.children?.forEach(visit);
182 };
183
184 visit(tree);
185 markStandaloneImages(tree);
186 };
187 }
188
189 const IMAGE_SRC_PROTOCOLS = [...(defaultSchema.protocols?.src ?? []), "file", "t3-context"];
190
191 // The sanitizer compares schemes case-sensitively, so lowercase accepted editor links first.
192 function rehypeNormalizeLocalEditorLinks() {
193 return (tree: MarkdownImageHastNode) => {
194 const visit = (node: MarkdownImageHastNode) => {
195 const href = node.properties?.href;
196 if (node.type === "element" && node.tagName === "a" && typeof href === "string") {
197 try {
198 const url = new URL(href);
199 if (isLocalEditorFileUrl(url)) node.properties = { ...node.properties, href: url.href };
200 } catch {
201 // Not an absolute URL; the sanitizer decides.
202 }
203 }
204 node.children?.forEach(visit);
205 };
206
207 visit(tree);
208 };
209 }
210
211 const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
212 ...defaultSchema,
213 attributes: {
214 ...defaultSchema.attributes,
215 "*": (defaultSchema.attributes?.["*"] ?? []).filter((attribute) => attribute !== "title"),
216 // Fence languages plus the two math classes the web chat's math plugin emits.
217 code: [
218 ["className", /^language-./, "math-inline", "math-display"],
219 "dataCodeMeta",
220 "dataInlineCode",
221 ],
222 blockquote: [...(defaultSchema.attributes?.blockquote ?? []), "dataAlert"],
223 div: [...(defaultSchema.attributes?.div ?? []), ...CODEX_ARTIFACT_TEMPLATE_HAST_PROPERTIES],
224 a: [...(defaultSchema.attributes?.a ?? []), "dataPullRequestAutolink"],
225 img: [
226 ...(defaultSchema.attributes?.img ?? []),
227 "dataDarkSrc",
228 "dataLightSrc",
229 "dataLocalSrc",
230 "dataMarkdownTitle",
231 "dataStandalone",
232 ],
233 },
234 protocols: {
235 ...defaultSchema.protocols,
236 href: [
237 ...(defaultSchema.protocols?.href ?? []),
238 "file",
239 "t3-citation",
240 "t3-context",
241 THREAD_LINK_PROTOCOL,
242 // Scheme only: the renderer's URL transform checks the rest of the link shape.
243 ...LOCAL_FILE_LINK_SCHEMES,
244 ],
245 src: IMAGE_SRC_PROTOCOLS,
246 dataDarkSrc: IMAGE_SRC_PROTOCOLS,
247 dataLightSrc: IMAGE_SRC_PROTOCOLS,
248 },
249 } satisfies Parameters<typeof rehypeSanitize>[0];
250
251 export const CHAT_MARKDOWN_REMARK_PLUGINS: PluggableList = [
252 remarkGfm,
253 remarkEndAutolinksAtScriptPunctuation,
254 remarkKeepAutolinkSuffixes,
255 remarkLinkAbsoluteFilePaths,
256 remarkKeepWindowsPaths,
257 remarkGithubAlerts,
258 remarkKeepOrderedListNumbers,
259 remarkNormalizeListItemIndentation,
260 remarkCodexDirectives,
261 remarkBreakAroundImageLines,
262 remarkPreserveCodeMeta,
263 remarkNormalizeLinksAndTagInlineCode,
264 ];
265
266 // Leading whitespace is kept only where each line renders as a line.
267 export const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS: PluggableList = [
268 remarkGfm,
269 remarkEndAutolinksAtScriptPunctuation,
270 remarkKeepAutolinkSuffixes,
271 remarkKeepLeadingWhitespace,
272 remarkLinkAbsoluteFilePaths,
273 remarkKeepWindowsPaths,
274 remarkGithubAlerts,
275 remarkKeepOrderedListNumbers,
276 remarkNormalizeListItemIndentation,
277 remarkCodexDirectives,
278 remarkBreakAroundImageLines,
279 remarkBreaks,
280 remarkPreserveCodeMeta,
281 remarkNormalizeLinksAndTagInlineCode,
282 ];
283
284 // Users type paths like ~/a and (~/b, which GFM's single-tilde strikethrough hides as <del>.
285 // Appended after the base list, this reconfigures its remark-gfm entry; ~~double~~ still strikes.
286 // Users also indent lines with spaces, which CommonMark drops from a paragraph.
287 export const CHAT_USER_MARKDOWN_REMARK_PLUGINS: PluggableList = [
288 [remarkGfm, { singleTilde: false }],
289 remarkEnableLeadingWhitespace,
290 ];
291
292 // Start tags an HTML parser closes an open `<p>` for.
293 const CLOSES_PARAGRAPH = new Set(
294 "address article aside blockquote center details dialog dir div dl fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr li listing main menu nav ol p plaintext pre search section summary table ul xmp dd dt".split(
295 " ",
296 ),
297 );
298 const HEADING = /^h[1-6]$/;
299
300 interface HtmlScope {
301 readonly anchor: boolean;
302 readonly paragraph: boolean;
303 readonly heading: boolean;
304 }
305
306 /**
307 * Whether the tree needs the HTML parser: raw HTML, or nesting it repairs (a footnote call in a
308 * link label nests `<a>`). Otherwise it only rewrites properties as hast-util-to-parse5 does and
309 * drops `data`, both repeated here, and moves blank text that renders the same.
310 */
311 function needsHtmlParser(node: MarkdownImageHastNode, scope: HtmlScope): boolean {
312 if (node.type === "raw") return true;
313 let inner = scope;
314 if (node.type === "element" && node.tagName) {
315 const { tagName } = node;
316 const heading = HEADING.test(tagName);
317 if (
318 (tagName === "a" && scope.anchor) ||
319 (scope.paragraph && CLOSES_PARAGRAPH.has(tagName)) ||
320 (heading && scope.heading)
321 ) {
322 return true;
323 }
324 inner = {
325 anchor: scope.anchor || tagName === "a",
326 paragraph: scope.paragraph || tagName === "p",
327 heading: scope.heading || heading,
328 };
329 const properties = node.properties ?? {};
330 for (const key of Object.keys(properties)) {
331 const value = properties[key];
332 const info = find(html, key);
333 if (
334 value === false ||
335 value === null ||
336 value === undefined ||
337 Number.isNaN(value) ||
338 (!value && info.boolean)
339 ) {
340 delete properties[key];
341 } else if (value === true && !info.boolean && !info.overloadedBoolean) {
342 properties[key] = "";
343 }
344 }
345 delete (node as { data?: unknown }).data;
346 }
347 return (node.children ?? []).some((child) => needsHtmlParser(child, inner));
348 }
349
350 const TOP_SCOPE: HtmlScope = { anchor: false, paragraph: false, heading: false };
351
352 /** rehype-raw reparses the whole tree through an HTML parser, which most trees can skip. */
353 function rehypeRawWhenPresent(this: Processor) {
354 const transform = rehypeRaw.call(this) as unknown as (
355 tree: MarkdownImageHastNode,
356 file: unknown,
357 ) => unknown;
358 return (tree: MarkdownImageHastNode, file: unknown) =>
359 needsHtmlParser(tree, TOP_SCOPE) ? transform(tree, file) : undefined;
360 }
361
362 export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [
363 rehypePreserveBareAnchorPlaceholders,
364 rehypeRawWhenPresent,
365 rehypePreserveImageSourceMeta,
366 rehypeNormalizeLocalEditorLinks,
367 [rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
368 ];
369
370 const isImageNode = (node: PhrasingContent | undefined): boolean =>
371 node?.type === "image" ||
372 node?.type === "imageReference" ||
373 ((node?.type === "link" || node?.type === "linkReference") &&
374 node.children.length === 1 &&
375 isImageNode(node.children[0]));
376 const endsLine = (node: PhrasingContent | undefined) =>
377 node === undefined ||
378 node.type === "break" ||
379 (node.type === "text" && /\n[ \t]*$/.test(node.value));
380 const startsLine = (node: PhrasingContent | undefined) =>
381 node === undefined ||
382 node.type === "break" ||
383 (node.type === "text" && /^[ \t]*\n/.test(node.value));
384
385 function breakLinesAroundImages(paragraph: Paragraph) {
386 const children = paragraph.children;
387 if (!children.some(isImageNode)) return;
388 const ownsLine = (index: number) =>
389 isImageNode(children[index]) &&
390 endsLine(children[index - 1]) &&
391 startsLine(children[index + 1]);
392 paragraph.children = children.flatMap((child, index): PhrasingContent[] => {
393 if (child.type !== "text") return [child];
394 const afterImage = ownsLine(index - 1);
395 const beforeImage = ownsLine(index + 1);
396 if (!afterImage && !beforeImage) return [child];
397 const parts: PhrasingContent[] = [];
398 let value = child.value;
399 if (afterImage) {
400 value = value.replace(/^[ \t]*\n/, "");
401 parts.push({ type: "break" });
402 }
403 const breakAfter = beforeImage && /\n[ \t]*$/.test(value);
404 if (breakAfter) value = value.replace(/[ \t]*\n[ \t]*$/, "");
405 if (value) parts.push({ type: "text", value });
406 if (breakAfter) parts.push({ type: "break" });
407 return parts;
408 });
409 }
410
411 /**
412 * Agents label screenshots with a line like `**Palette**` right above the image. CommonMark
413 * joins those lines into one paragraph, which puts the image inline beside its label. An
414 * image alone on its source line keeps that line; prose and inline badges still wrap.
415 */
416 function remarkBreakAroundImageLines() {
417 return (tree: Root) => {
418 const visit = (node: Root | RootContent) => {
419 if (node.type === "paragraph") breakLinesAroundImages(node);
420 else if ("children" in node) node.children.forEach(visit);
421 };
422 visit(tree);
423 };
424 }
425
426 type MarkdownAstNode = {
427 type?: string;
428 meta?: unknown;
429 url?: string;
430 data?: {
431 hProperties?: Record<string, unknown>;
432 };
433 children?: MarkdownAstNode[];
434 };
435
436 function remarkKeepAutolinkSuffixes(): Transformer<Root> {
437 return (tree, file) => {
438 const source = String(file);
439 const visit = (node: Root | RootContent) => {
440 if (!("children" in node) || node.type === "link" || node.type === "linkReference") return;
441 for (let index = 0; index < node.children.length; index++) {
442 const child = node.children[index];
443 const next = node.children[index + 1];
444 if (child?.type === "link" && /^https?:\/\//i.test(child.url) && next?.type === "text") {
445 const suffix = /^[_~]+/.exec(next.value)?.[0];
446 if (!suffix) continue;
447 const label = child.children.length === 1 ? child.children[0] : undefined;
448 if (label?.type !== "text") continue;
449 if (label.value !== child.url && `http://${label.value}` !== child.url) continue;
450 const start = child.position?.start.offset;
451 const end = child.position?.end.offset;
452 const bare =
453 !child.position ||
454 (start !== undefined &&
455 end !== undefined &&
456 source.slice(start, end) === label.value &&
457 source.slice(end, end + suffix.length) === suffix);
458 if (bare) {
459 child.url += suffix;
460 label.value += suffix;
461 next.value = next.value.slice(suffix.length);
462 if (child.position && end !== undefined) {
463 const boundary = {
464 ...child.position.end,
465 column: child.position.end.column + suffix.length,
466 offset: end + suffix.length,
467 };
468 child.position.end = boundary;
469 if (label.position) label.position.end = boundary;
470 if (next.position) next.position.start = boundary;
471 }
472 if (!next.value) node.children.splice(index + 1, 1);
473 }
474 }
475 if (child) visit(child);
476 }
477 };
478 visit(tree);
479 };
480 }
481
482 // Punctuation and symbols from CJK Symbols and Punctuation and from Halfwidth and Fullwidth Forms,
483 // plus the Arabic-script comma, semicolon, question mark, and full stop.
484 const SCRIPT_PUNCTUATION = /(?=[ -〿＀-￯،؛؟۔])[\p{P}\p{S}]/u;
485
486 /**
487 * GFM runs a bare URL on until whitespace, so Chinese, Japanese, and Arabic punctuation joins it,
488 * along with the rest of the sentence and any `**` that should have closed around it. A URL that is
489 * ASCII up to such a mark ends there. Unicode earlier in the URL means the mark may belong to it, so it stays.
490 */
491 function scriptPunctuationIndex(link: Link): number {
492 const label = link.children.length === 1 ? link.children[0] : undefined;
493 if (label?.type !== "text" || !/^https?:\/\//i.test(link.url)) return -1;
494 if (link.url !== label.value && link.url !== `http://${label.value}`) return -1;
495 const index = label.value.search(/[^!-~]/u);
496 return index > 0 && SCRIPT_PUNCTUATION.test(label.value.charAt(index)) ? index : -1;
497 }
498
499 /** GFM's text pass links bare URLs without positions; its tokenizer's links span their URL. */
500 function isBareLink(link: Link, source: string): boolean {
501 const start = link.position?.start.offset;
502 const end = link.position?.end.offset;
503 const label = link.children[0];
504 return (
505 !link.position ||
506 (start !== undefined &&
507 end !== undefined &&
508 label?.type === "text" &&
509 source.slice(start, end) === label.value)
510 );
511 }
512
513 /** Source offsets of the marks that tokenized bare URLs run past. */
514 function scriptPunctuationOffsets(node: RootContent, source: string): number[] {
515 if (node.type === "link") {
516 const index = scriptPunctuationIndex(node);
517 const start = node.position?.start.offset;
518 return index > 0 && start !== undefined && isBareLink(node, source) ? [start + index] : [];
519 }
520 return "children" in node && node.type !== "linkReference"
521 ? (node.children as RootContent[]).flatMap((child) => scriptPunctuationOffsets(child, source))
522 : [];
523 }
524
525 const hasReference = (node: RootContent): boolean =>
526 node.type === "linkReference" ||
527 node.type === "imageReference" ||
528 node.type === "footnoteReference" ||
529 ("children" in node && (node.children as RootContent[]).some(hasReference));
530
531 /** How many of the ascending numbers are below `value`. */
532 function countBelow(sorted: readonly number[], value: number): number {
533 let low = 0;
534 let high = sorted.length;
535 while (low < high) {
536 const middle = (low + high) >> 1;
537 if (sorted[middle]! < value) low = middle + 1;
538 else high = middle;
539 }
540 return low;
541 }
542
543 /**
544 * Parses the block's text again with a tab before each mark. The tab ends the URL as a space would,
545 * so emphasis around the URL closes and later URLs and code get their own nodes. The tabs are then
546 * dropped and positions mapped back to the source. Returns false and leaves the block alone when its
547 * text would parse differently on its own, as a quote's continuation lines do.
548 */
549 function reparseAtScriptPunctuation(
550 processor: Processor,
551 block: Paragraph | Heading | TableCell,
552 source: string,
553 offsets: number[],
554 ): boolean {
555 // A task item's paragraph starts at its checkbox; its first child starts after it.
556 const from = block.children[0]?.position?.start;
557 const to = block.children.at(-1)?.position?.end;
558 if (from?.offset === undefined || to?.offset === undefined || hasReference(block)) return false;
559 const base = from.offset;
560 const text = source.slice(base, to.offset);
561 // A tab is the marker, a quote's `>` on a later line would read as a new quote, a list's
562 // indentation would join a code span, and only a table unescapes `\|` in code.
563 if (
564 text.includes("\t") ||
565 /[\r\n][ ]*>/.test(text) ||
566 (/[\r\n] /.test(text) && text.includes("`")) ||
567 (block.type === "tableCell" && text.includes("\\|"))
568 ) {
569 return false;
570 }
571 const marks = offsets.map((offset) => offset - base);
572 // Each pass finds the next URL in a run with no spaces, like `http://a,http://b,http://c`.
573 for (let attempt = 0; attempt < 16; attempt++) {
574 marks.sort((a, b) => a - b);
575 const tabs = marks.map((mark, index) => mark + index);
576 const unmarked = (offset: number) => offset - countBelow(tabs, offset);
577 const marked = [0, ...marks]
578 .map((start, index) => text.slice(start, marks[index] ?? text.length))
579 .join("\t");
580 const [paragraph, ...rest] = (processor.parse(marked) as Root).children;
581 if (paragraph?.type !== "paragraph" || rest.length > 0) return false;
582 const more = scriptPunctuationOffsets(paragraph, marked);
583 if (more.length > 0) {
584 marks.push(...more.map(unmarked));
585 continue;
586 }
587
588 let tabCount = 0;
589 const moved = new Set<object>();
590 const restore = (node: RootContent): boolean => {
591 for (const point of node.position ? [node.position.start, node.position.end] : []) {
592 const offset = point.offset;
593 if (offset === undefined || moved.has(point)) continue;
594 moved.add(point);
595 const tabsOnLine = countBelow(tabs, offset) - countBelow(tabs, offset - point.column + 1);
596 point.column = (point.line === 1 ? from.column - 1 : 0) + point.column - tabsOnLine;
597 point.line += from.line - 1;
598 point.offset = base + unmarked(offset);
599 }
600 if (node.type === "text") {
601 tabCount += node.value.split("\t").length - 1;
602 node.value = node.value.replaceAll("\t", "");
603 } else if (
604 ("value" in node && node.value.includes("\t")) ||
605 ("url" in node && node.url.includes("\t"))
606 ) {
607 return false;
608 }
609 if (!("children" in node)) return true;
610 const children = node.children as RootContent[];
611 if (!children.every(restore)) return false;
612 node.children = children.filter(
613 (child) => child.type !== "text" || child.value !== "",
614 ) as typeof node.children;
615 return true;
616 };
617 if (!restore(paragraph) || tabCount !== tabs.length) return false;
618 block.children = paragraph.children;
619 return true;
620 }
621 return false;
622 }
623
624 /** Ends the bare URLs a reparse could not, and those GFM's text pass linked after emphasis. */
625 function splitAtScriptPunctuation(node: RootContent, source: string) {
626 if (!("children" in node) || node.type === "link" || node.type === "linkReference") return;
627 const children = node.children as RootContent[];
628 for (let index = 0; index < children.length; index++) {
629 const child = children[index]!;
630 if (child.type !== "link") {
631 splitAtScriptPunctuation(child, source);
632 continue;
633 }
634 const at = isBareLink(child, source) ? scriptPunctuationIndex(child) : -1;
635 const label = child.children[0];
636 if (at < 0 || label?.type !== "text") continue;
637 // GFM drops this punctuation from the end of a URL; remarkKeepAutolinkSuffixes restores `_` and `~`.
638 const length = label.value.slice(0, at).replace(/[!"'*,.:;?_~]+$/, "").length;
639 const tail: Text = { type: "text", value: label.value.slice(length) };
640 const start = child.position?.start;
641 if (child.position && start?.offset !== undefined) {
642 const end = {
643 line: start.line,
644 column: start.column + length,
645 offset: start.offset + length,
646 };
647 tail.position = { start: end, end: child.position.end };
648 child.position = { start, end };
649 label.position = { start, end };
650 }
651 child.url = child.url.slice(0, child.url.length - label.value.length + length);
652 label.value = label.value.slice(0, length);
653 children.splice(index + 1, 0, tail);
654 index++;
655 }
656 }
657
658 function remarkEndAutolinksAtScriptPunctuation(this: Processor): Transformer<Root> {
659 return (tree, file) => {
660 const source = String(file);
661 if (!SCRIPT_PUNCTUATION.test(source)) return;
662 const visit = (node: RootContent) => {
663 if (node.type === "paragraph" || node.type === "heading" || node.type === "tableCell") {
664 const offsets = scriptPunctuationOffsets(node, source);
665 if (offsets.length > 0) reparseAtScriptPunctuation(this, node, source, offsets);
666 splitAtScriptPunctuation(node, source);
667 } else if ("children" in node) {
668 (node.children as RootContent[]).forEach(visit);
669 }
670 };
671 tree.children.forEach(visit);
672 };
673 }
674
675 function remarkLinkAbsoluteFilePaths(): Transformer<Root> {
676 return (tree) => {
677 // Raw `<a>` tags are sibling html nodes, so text inside one is not under a link node.
678 let rawAnchorDepth = 0;
679 const visit = (node: Root | RootContent) => {
680 if (!("children" in node) || node.type === "link" || node.type === "linkReference") return;
681 node.children = node.children.flatMap((child): typeof node.children => {
682 if (child.type === "html") {
683 for (const [tag] of child.value.matchAll(RAW_HTML_TAG_PATTERN)) {
684 if (/^<a[\s/>]/i.test(tag)) rawAnchorDepth += 1;
685 else if (/^<\/a[\s>]/i.test(tag)) rawAnchorDepth = Math.max(0, rawAnchorDepth - 1);
686 }
687 }
688 if (child.type !== "text" || rawAnchorDepth > 0) {
689 visit(child);
690 return [child];
691 }
692 const parts: PhrasingContent[] = [];
693 let offset = 0;
694 for (const { index, path } of absoluteFilePathMatches(child.value)) {
695 if (index > offset) parts.push({ type: "text", value: child.value.slice(offset, index) });
696 parts.push({ type: "link", url: path, children: [{ type: "text", value: path }] });
697 offset = index + path.length;
698 }
699 if (offset === 0) return [child];
700 if (offset < child.value.length)
701 parts.push({ type: "text", value: child.value.slice(offset) });
702 return parts;
703 });
704 };
705 visit(tree);
706 };
707 }
708
709 function remarkPreserveCodeMeta() {
710 return (tree: MarkdownAstNode) => {
711 const visit = (node: MarkdownAstNode) => {
712 if (node.type === "code" && typeof node.meta === "string" && node.meta.trim().length > 0) {
713 node.data = {
714 ...node.data,
715 hProperties: {
716 ...node.data?.hProperties,
717 dataCodeMeta: node.meta.trim(),
718 },
719 };
720 }
721 node.children?.forEach(visit);
722 };
723
724 visit(tree);
725 };
726 }
727
728 function remarkNormalizeLinksAndTagInlineCode() {
729 return (tree: MarkdownAstNode) => {
730 const visit = (node: MarkdownAstNode, insideLink: boolean) => {
731 if (
732 (node.type === "link" || node.type === "definition") &&
733 typeof node.url === "string" &&
734 /^[A-Za-z]:[\\/]/.test(node.url)
735 ) {
736 node.url = `file:///${node.url.replaceAll("\\", "/")}`;
737 }
738 if (node.type === "inlineCode" && !insideLink) {
739 node.data = {
740 ...node.data,
741 hProperties: {
742 ...node.data?.hProperties,
743 dataInlineCode: "",
744 },
745 };
746 }
747 const childInsideLink = insideLink || node.type === "link" || node.type === "linkReference";
748 node.children?.forEach((child) => visit(child, childInsideLink));
749 };
750
751 visit(tree, false);
752 };
753 }
754
755 export function shouldPreserveAssistantLineBreaks(text: string): boolean {
756 return /^★ Insight(?:\s|─)/mu.test(text);
757 }
758
759 export function remarkAssistantInsights(this: Processor) {
760 return (tree: Root, file: { value: unknown }) => {
761 const source = String(file.value);
762 if (!source.includes("★ Insight")) return;
763 const protectedRanges: { start: number; end: number; code: boolean }[] = [];
764 const inspect = (node: RootContent | PhrasingContent) => {
765 const start = node.position?.start.offset;
766 const end = node.position?.end.offset;
767 if (start !== undefined && end !== undefined) {
768 const code =
769 node.type === "code" ||
770 node.type === "html" ||
771 (node.type === "inlineCode" && node.position?.start.line !== node.position?.end.line);
772 if (code || node.type === "list" || node.type === "blockquote")
773 protectedRanges.push({ start, end, code });
774 }
775 if ("children" in node) node.children.forEach(inspect);
776 };
777 tree.children.forEach(inspect);
778 const starts = new Set<number>();
779 const offsets: { original: number; rewritten: number; prefix: number }[] = [];
780 let indent: string | undefined;
781 let original = 0;
782 let rewritten = 0;
783 const lines = source.split(/(?<=\n)|(?<=\r)(?!\n)/u).map((line, index) => {
784 const value = line.replace(/[\r\n]+$/u, "");
785 const blocked = protectedRanges.some(
786 (range) =>
787 original >= range.start && original < range.end && (indent === undefined || range.code),
788 );
789 const opening =
790 indent === undefined && !blocked
791 ? /^( {0,3})(`?)★ Insight[ \t]+─{3,}\2[ \t]*$/u.exec(value)
792 : null;
793 const closing =
794 indent !== undefined && !blocked ? /^( {0,3})(`?)─{3,}\2[ \t]*$/u.exec(value) : null;
795 let prefix = 0;
796 let output = line;
797 if (opening) {
798 indent = opening[1] ?? "";
799 starts.add(index + 1);
800 output = ">" + line.slice(value.length);
801 } else if (closing && closing[1] === indent) {
802 indent = undefined;
803 output = line.slice(value.length);
804 } else if (indent !== undefined) {
805 prefix = 2;
806 output = "> " + line;
807 }
808 offsets.push({ original, rewritten, prefix });
809 original += line.length;
810 rewritten += output.length;
811 return output;
812 });
813 if (starts.size === 0) return;
814 const parsed = this.parse(lines.join("")) as Root;
815 const restorePosition = (node: RootContent | PhrasingContent) => {
816 if (node.position) {
817 for (const point of [node.position.start, node.position.end]) {
818 const offset = offsets[point.line - 1];
819 if (!offset) continue;
820 point.column = Math.max(1, point.column - offset.prefix);
821 if (point.offset !== undefined)
822 point.offset += offset.original - offset.rewritten - offset.prefix;
823 }
824 }
825 if ("children" in node) node.children.forEach(restorePosition);
826 };
827 for (const node of parsed.children) {
828 if (node.type === "blockquote" && starts.has(node.position?.start.line ?? 0)) {
829 node.data = { ...node.data, hProperties: { dataAlert: "insight" } };
830 }
831 restorePosition(node);
832 }
833 tree.children = parsed.children;
834 };
835 }
836
837 const imageSourceProcessor = unified()
838 .use(remarkParse)
839 .use(CHAT_MARKDOWN_REMARK_PLUGINS)
840 .use(remarkRehype, { allowDangerousHtml: true })
841 .use(CHAT_MARKDOWN_REHYPE_PLUGINS);
842
843 export type ChatMarkdownImageProperties = {
844 readonly src?: unknown;
845 readonly dataLocalSrc?: unknown;
846 readonly dataDarkSrc?: unknown;
847 readonly dataLightSrc?: unknown;
848 };
849
850 /** Keep the authored path for file links and normalize Windows separators for classification. */
851 export function chatMarkdownImageSource(
852 properties?: ChatMarkdownImageProperties,
853 theme?: "dark" | "light",
854 ) {
855 const themeSrc =
856 theme === undefined
857 ? undefined
858 : properties?.[theme === "dark" ? "dataDarkSrc" : "dataLightSrc"];
859 const localSrc = typeof themeSrc === "string" ? undefined : properties?.dataLocalSrc;
860 const source =
861 typeof themeSrc === "string"
862 ? themeSrc
863 : typeof localSrc === "string"
864 ? localSrc
865 : properties?.src;
866 if (typeof source !== "string") return null;
867 const authoredSrc = normalizeMarkdownLinkDestination(source);
868 return {
869 authoredSrc,
870 classifiedSrc: typeof localSrc === "string" ? authoredSrc.replaceAll("\\", "/") : authoredSrc,
871 };
872 }
873
874 interface ImageSourceNode {
875 readonly type: string;
876 readonly tagName?: string;
877 readonly properties?: ChatMarkdownImageProperties;
878 readonly children?: ReadonlyArray<ImageSourceNode>;
879 }
880
881 /**
882 * Collect the fallback and every theme variant so images are available to all viewers.
883 */
884 export function chatMarkdownImageSources(markdown: string): Array<string> {
885 const sources: Array<string> = [];
886 const visit = (node: ImageSourceNode) => {
887 if (node.type === "element" && node.tagName === "img") {
888 const properties = node.properties;
889 const fallback = chatMarkdownImageSource(properties);
890 if (fallback) sources.push(fallback.classifiedSrc);
891 for (const [theme, variant] of [
892 ["dark", properties?.dataDarkSrc],
893 ["light", properties?.dataLightSrc],
894 ] as const) {
895 if (typeof variant !== "string") continue;
896 const source = chatMarkdownImageSource(properties, theme);
897 if (source) sources.push(source.classifiedSrc);
898 }
899 }
900 node.children?.forEach(visit);
901 };
902 visit(imageSourceProcessor.runSync(imageSourceProcessor.parse(markdown)) as ImageSourceNode);
903 return sources;
904 }
905
906 const linkDestinationProcessor = unified().use(remarkParse).use(remarkKeepWindowsPaths);
907
908 // An angle destination cannot hold an unescaped `<` or `>`, or end on a lone backslash.
909 const ANGLE_DESTINATION_CONTENT = /^(?:[^\\<>]|\\.)*$/;
910
911 /** Reads a destination from `extractMarkdownLinkHrefs` as the renderer's parser does. */
912 export function decodeMarkdownLinkEscapes(href: string): string {
913 // Only the parser knows character references, so destinations that may have one go through it.
914 if (href.includes("&")) {
915 // The extractor drops angle brackets, and only that form keeps an unmatched `)` in the path.
916 const destination = ANGLE_DESTINATION_CONTENT.test(href) ? `<${href}>` : href;
917 const paragraph = linkDestinationProcessor.parse(`[](${destination})`).children[0];
918 const link = paragraph?.type === "paragraph" ? paragraph.children[0] : undefined;
919 if (link?.type === "link") return link.url;
920 }
921 return isWindowsAbsolutePath(href) ? href : href.replace(/\\([!-/:-@[-`{-~])/g, "$1");
922 }