Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

enrichedLinkPresentation.ts

71 lines2.8 KB

1import type { ComposerContextRecord } from "@t3tools/contracts";
2import {
3 composerChipSizeSuffix,
4 contextChipIcon,
5 contextChipPresentation,
6 contextRecordFileName,
7 type ContextChipIconRef,
8 UNAVAILABLE_CONTEXT_CHIP_PRESENTATION,
9} from "./composerChipPresentation";
10import type { SelectableMarkdownSkill } from "../native/SelectableMarkdownText.types";
11import { parseComposerContextHref } from "@t3tools/shared/composerContextReferences";
12​
13export function enrichedContextLinkPresentation(
14 href: string,
15 records?: ReadonlyArray<ComposerContextRecord>,
16): { accent?: string; icon: ContextChipIconRef; label?: string } | undefined {
17 const reference = parseComposerContextHref(href);
18 if (!reference) return undefined;
19 const record = records?.find((candidate) => candidate.contextId === reference.contextId);
20 const presentation =
21 records && !record
22 ? UNAVAILABLE_CONTEXT_CHIP_PRESENTATION
23 : contextChipPresentation(reference.kind, record);
24 const size = composerChipSizeSuffix(record);
25 return {
26 ...(presentation.accent ? { accent: presentation.accent } : {}),
27 icon: contextChipIcon(presentation, contextRecordFileName(record)),
28 ...(record && size ? { label: `${record.label} · ${size}` } : {}),
29 };
31​
32export function enrichedLinkVariantPattern(url: string) {
33 return `^${url.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`;
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 );
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 );
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;
68​
69export function enrichedSkillInstructionsPath(skill: SelectableMarkdownSkill) {
70 return skill.path && !/^[a-z][a-z\d+.-]*:\/\//i.test(skill.path) ? skill.path : undefined;