@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 41 lines 1.29 KB
Raw
1 type HastNode = {
2 type?: string;
3 tagName?: string;
4 value?: string;
5 properties?: Record<string, unknown>;
6 children?: HastNode[];
7 };
8
9 const LEADING_PUNCTUATION = /^[.,:;!?…)\]}"'’”]+/;
10
11 // Browsers allow a wrap after inline-flex chips even before punctuation.
12 export function rehypeChipPunctuation() {
13 const visit = (node: HastNode): void => {
14 if (node.tagName === "pre" || !node.children) return;
15 const children: HastNode[] = [];
16 for (const child of node.children) {
17 visit(child);
18 const previous = children.at(-1);
19 const punctuation =
20 child.type === "text" && (previous?.tagName === "a" || previous?.tagName === "code")
21 ? LEADING_PUNCTUATION.exec(child.value ?? "")?.[0]
22 : undefined;
23 if (!previous || !punctuation) {
24 children.push(child);
25 continue;
26 }
27 children[children.length - 1] = {
28 type: "element",
29 tagName: "span",
30 properties: {
31 className: ["has-[[data-slot=context-chip-label]]:whitespace-nowrap"],
32 },
33 children: [previous, { type: "text", value: punctuation }],
34 };
35 const rest = (child.value ?? "").slice(punctuation.length);
36 if (rest) children.push({ ...child, value: rest });
37 }
38 node.children = children;
39 };
40 return visit;
41 }