Open History to see the last commit
1import type { ComposerContextRecord } from "@t3tools/contracts";
2import { contextChipPresentation } from "./composerChipPresentation";
3import type { MarkdownFileIcon } from "./markdownLinks";
4import type { SelectableMarkdownSkill } from "../native/SelectableMarkdownText.types";
5import { parseComposerContextHref } from "@t3tools/shared/composerContextReferences";
6
7const CONTEXT_ICONS: Readonly<Record<string, MarkdownFileIcon>> = {
8 photo: "image",
9 "play.rectangle": "video",
10 terminal: "bash",
11 "cursorarrow.click": "react",
12 "text.bubble": "markdown",
13 "git-pull-request": "git",
14 cube: "mcp",
15 doc: "default",
16};
17
18export function enrichedContextLinkPresentation(
19 href: string,
20 records?: ReadonlyArray<ComposerContextRecord>,
21) {
22 const reference = parseComposerContextHref(href);
23 if (!reference) return undefined;
24 const record = records?.find((candidate) => candidate.contextId === reference.contextId);
25 const presentation = contextChipPresentation(reference.kind, record);
26 return {
27 color: presentation.accent,
28 icon: CONTEXT_ICONS[presentation.symbol] ?? "default",
29 };
30}
31
32export function enrichedLinkVariantPattern(url: string) {
33 return `^${url.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`;
34}
35
36export const ENRICHED_INLINE_FILE_LINK_REGEX =
37 /^(?!\S*:\/\/)(?:(?:\.{1,2}[\\/]|~[\\/]|\/(?:Users|home|tmp|var|etc|opt|mnt|Volumes|private|root|usr|bin|sbin|lib|lib64|srv|dev|proc|sys|run|boot|media|workspace|workspaces)\/|[A-Za-z]:[\\/])[^\s`]+|(?:[A-Za-z0-9._-]+[\\/])+[A-Za-z0-9._-]+\.[A-Za-z0-9_-]+(?::\d+){0,2}|[A-Za-z0-9._-]+\.[A-Za-z0-9_-]+:\d+(?::\d+)?)$/;
38
39export function enrichedSkillLinkRegex(skills?: ReadonlyArray<SelectableMarkdownSkill>) {
40 const names = [...new Set(skills?.map((skill) => skill.name) ?? [])].filter(Boolean);
41 if (names.length === 0) return null;
42 names.sort((a, b) => b.length - a.length);
43 const alternatives = names.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
44 return new RegExp(
45 `(?<![A-Za-z0-9_-])(?<!\\p{Sc})\\p{Sc}(?:${alternatives.join("|")})(?![A-Za-z0-9_-])`,
46 "u",
47 );
48}
49
50export function enrichedSkillDisplayName(skill: SelectableMarkdownSkill) {
51 return (
52 skill.displayName?.trim() ||
53 skill.name
54 .split(/[\s:_-]+/)
55 .filter(Boolean)
56 .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
57 .join(" ")
58 );
59}
60
61export function enrichedSkillReference(
62 url: string,
63 skills?: ReadonlyArray<SelectableMarkdownSkill>,
64) {
65 const match = /^\p{Sc}(.+)$/u.exec(url);
66 return match ? skills?.find((skill) => skill.name === match[1]) : undefined;
67}