Open History to see the last commit
1import { decodeHTMLStrict } from "entities";
2import {
3 codexArtifactTemplatePresentationLabel,
4 resolveCodexArtifactTemplate,
5} from "@t3tools/shared/codexArtifactTemplates";
6import {
7 codexFileCitationMarkdown,
8 resolveCodexFileCitationLink,
9} from "@t3tools/shared/codexFileCitations";
10import { isWindowsAbsolutePath } from "@t3tools/shared/path";
11
12import { codexDirectiveSources, type CodexDirectiveSource } from "./wordsHast";
13
14const CODEX_DIRECTIVE = /:codex-file-citation|:codex-followup|::artifact-template/;
15
16/** Replaces each directive with what `replacement` returns for it; undefined keeps it. */
17function replaceDirectives(
18 markdown: string,
19 replacement: (directive: CodexDirectiveSource) => string | undefined,
20): string {
21 if (!CODEX_DIRECTIVE.test(markdown)) return markdown;
22 let rendered = markdown;
23 const changes: Array<{ start: number; delta: number }> = [];
24 const directives = codexDirectiveSources(markdown).sort(
25 (left, right) => right.start - left.start,
26 );
27 for (const directive of directives) {
28 // Consume child edits so this directive's original end and label still locate the rendered text.
29 let delta = 0;
30 while (changes.length && changes.at(-1)!.start < directive.end) {
31 delta += changes.pop()!.delta;
32 }
33 const labelStart = markdown.indexOf("[", directive.start) + 1;
34 const label = rendered.slice(labelStart, labelStart + directive.label.length + delta);
35 const value =
36 replacement({ ...directive, label }) ??
37 rendered.slice(directive.start, directive.end + delta);
38 rendered = rendered.slice(0, directive.start) + value + rendered.slice(directive.end + delta);
39 changes.push({
40 start: directive.start,
41 delta: value.length - (directive.end - directive.start),
42 });
43 }
44 return rendered;
45}
46
47/** A file citation as the Markdown link it shows as, and a follow-up as its label. */
48function inlineDirectiveMarkdown(directive: CodexDirectiveSource): string | undefined {
49 if (directive.name === "codex-file-citation") {
50 const citation = directive.inLink ? null : resolveCodexFileCitationLink(directive.attributes);
51 return citation ? codexFileCitationMarkdown(citation) : undefined;
52 }
53 if (directive.name === "codex-followup") {
54 return directive.attributes.prompt?.trim() && directive.label.trim()
55 ? directive.label
56 : undefined;
57 }
58 return undefined;
59}
60
61export function renderCodexInlineDirectivesAsMarkdown(markdown: string): string {
62 return replaceDirectives(markdown, inlineDirectiveMarkdown);
63}
64
65/** What copying a message writes: inline directives as Markdown, a template as its name. */
66export function renderCodexDirectivesForCopy(markdown: string): string {
67 return replaceDirectives(markdown, (directive) => {
68 if (directive.name !== "artifact-template") return inlineDirectiveMarkdown(directive);
69 const template = resolveCodexArtifactTemplate(directive.attributes);
70 return template
71 ? `${template.displayName} (${codexArtifactTemplatePresentationLabel(template.artifactKind)})`
72 : undefined;
73 });
74}
75
76/** A link destination as written, with its character references and escapes resolved. */
77export function decodeMarkdownLinkEscapes(href: string): string {
78 const decoded = href.includes("&") ? decodeHTMLStrict(href) : href;
79 return isWindowsAbsolutePath(decoded)
80 ? decoded
81 : href.replace(
82 /\\([!-/:-@[-`{-~])|&(?:#(?:[xX][\da-fA-F]+|\d+)|[a-zA-Z][a-zA-Z\d]*);/g,
83 (match, escaped: string | undefined) => escaped ?? decodeHTMLStrict(match),
84 );
85}