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

SkillInlineText.tsx

89 lines2.8 KB

1import { Children, cloneElement, isValidElement, type ReactNode } from "react";
2import type { ServerProviderSkill } from "@t3tools/contracts";
3import { formatProviderSkillDisplayName } from "@t3tools/client-runtime/providerSkills";
4​
5import { SKILL_CHIP_ICON_SVG } from "../composerInlineChip";
6import { ContextChip, ContextChipLabel } from "../ContextChip";
7​
8const SKILL_TOKEN_REGEX =
9 /(^|\s)\p{Sc}(?![0-9][0-9_]*(?:[kKmMbBtT]|[eE][0-9]+)?(?:\s|$))(?=[a-zA-Z0-9:_-]*[a-zA-Z])([a-zA-Z0-9][a-zA-Z0-9:_-]*)(?=\s|$)/gu;
10​
11type InlineSkill = Pick<ServerProviderSkill, "name" | "displayName">;
12​
13export function SkillInlineText(props: { text: string; skills: ReadonlyArray<InlineSkill> }) {
14 const nodes: ReactNode[] = [];
15 let cursor = 0;
16​
17 for (const match of props.text.matchAll(SKILL_TOKEN_REGEX)) {
18 const prefix = match[1] ?? "";
19 const name = match[2] ?? "";
20 const start = (match.index ?? 0) + prefix.length;
21 const rawText = `$${name}`;
22 const skill = props.skills.find((candidate) => candidate.name === name);
23 if (!skill) {
24 continue;
25 }
26​
27 if (start > cursor) {
28 nodes.push(props.text.slice(cursor, start));
29 }
30 nodes.push(<SkillChip key={`${start}:${name}`} skill={skill} rawText={rawText} />);
31 cursor = (match.index ?? 0) + match[0].length;
32 }
33​
34 if (cursor === 0) {
35 return <>{props.text}</>;
36 }
37 if (cursor < props.text.length) {
38 nodes.push(props.text.slice(cursor));
39 }
40 return <>{nodes}</>;
42​
43export function renderSkillInlineMarkdownChildren(
44 children: ReactNode,
45 skills: ReadonlyArray<InlineSkill>,
46): ReactNode {
47 return Children.map(children, (child) => {
48 if (typeof child === "string") {
49 return <SkillInlineText text={child} skills={skills} />;
50 }
51 if (!isValidElement<{ children?: ReactNode; node?: { tagName?: string } }>(child)) {
52 return child;
53 }
54 // Custom react-markdown components replace the intrinsic type, so also
55 // check the hast node they carry.
56 const markdownTagName = typeof child.type === "string" ? child.type : child.props.node?.tagName;
57 if (markdownTagName === "code" || markdownTagName === "a") {
58 return child;
59 }
60 if (!("children" in child.props)) {
61 return child;
62 }
63 return cloneElement(
64 child,
65 undefined,
66 renderSkillInlineMarkdownChildren(child.props.children, skills),
67 );
68 });
70​
71function SkillChip(props: { skill: InlineSkill; rawText: string }) {
72 return (
73 <ContextChip kind="skill" data-markdown-copy={props.rawText}>
74 <SkillChipIcon />
75 <ContextChipLabel>{formatProviderSkillDisplayName(props.skill)}</ContextChipLabel>
76 </ContextChip>
77 );
79​
80/** The skill glyph; the surrounding chip sizes its svg. */
81export function SkillChipIcon() {
82 return (
83 <span
84 aria-hidden="true"
85 className="contents"
86 dangerouslySetInnerHTML={{ __html: SKILL_CHIP_ICON_SVG }}
87 />
88 );