Skip to content

Commit 372b024

Browse files
only21milclaude
andcommitted
fix(composer): keep mentions of files like posts.$postId.tsx or [id].tsx as chips
A file mention repeats the file name in its label and its link, so one `$` in the name gives the math parser a pair, and an escaped `\[` in the label reads as display math. The composer then dropped the mention and showed the raw link. Mask file links, including a confirmed chip kept at the end of the text, before looking for formulas, so only real TeX counts. Bare `@path` mentions stay unmasked: one can run into a formula's closing delimiter, as in `$x + @README.md$`, and a mention inside a formula stays covered by it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent cfa76ea commit 372b024

4 files changed

Lines changed: 97 additions & 4 deletions

File tree

‎apps/web/src/composer-rich-text-doc.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Node as ProseMirrorNode } from "@tiptap/pm/model";
55
import { EditorState, TextSelection } from "@tiptap/pm/state";
66
import { splitBlockKeepMarks } from "@tiptap/pm/commands";
77
import { describe, expect, it, onTestFinished } from "vite-plus/test";
8+
import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger";
89

910
import {
1011
collapseExpandedComposerCursor,
@@ -91,6 +92,28 @@ function roundTrip(value: string) {
9192
}
9293

9394
describe("editable math source", () => {
95+
it.each(["app/routes/posts.$postId.tsx", "pages/posts/[id].tsx"])(
96+
"keeps a mentioned file whose name looks like TeX as a chip: %s",
97+
(path) => {
98+
const source = `fix ${serializeComposerFileLink(path)} please`;
99+
const map = roundTrip(source);
100+
expect(map.value).toBe(source);
101+
expect(
102+
map.runs.filter((run) => run.kind === "token" && run.nodeName === "composer-mention"),
103+
).toHaveLength(1);
104+
},
105+
);
106+
107+
it.each([
108+
"before $x + @README.md$ after",
109+
"before \\(x + @README.md\\) after",
110+
"\\[x + @README.md\\] after",
111+
])("keeps a mention that ends a formula inside the formula: %s", (source) => {
112+
const map = roundTrip(source);
113+
expect(map.value).toBe(source);
114+
expect(map.runs.some((run) => run.kind === "token")).toBe(false);
115+
});
116+
94117
it.each([
95118
"Area $x_i^2$ after",
96119
"before \\(\\frac{1}{2}\\) after",

‎apps/web/src/composer-rich-text-doc.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { splitPromptIntoComposerSegments } from "~/composer-editor-mentions";
2828
import { nextOrderedMarkerText } from "~/composer-list-continuation";
2929
import { parseInlineMarkdown, RICH_TEXT_DELIMITERS, type RichTextMark } from "~/composer-rich-text";
3030
import { collectInlineContextIds } from "~/lib/composerContextReferences";
31-
import { markdownMathRanges } from "@t3tools/shared/markdownMath";
31+
import { composerMathRanges } from "@t3tools/shared/composerInlineTokens";
3232

3333
export type SkillMeta = { label: string; description: string | null };
3434

@@ -555,7 +555,7 @@ export function buildTiptapContent(
555555
const atomSources: string[] = [];
556556
const literal = value.slice(0, options?.literalLength ?? 0);
557557
const remainder = value.slice(literal.length);
558-
const formulas = markdownMathRanges(remainder);
558+
const formulas = composerMathRanges(remainder);
559559
let offset = 0;
560560
const chunks: Array<{ text: string } | { source: string }> = [];
561561
for (const formula of formulas) {

‎packages/shared/src/composerInlineTokens.test.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { collectComposerInlineTokens } from "./composerInlineTokens.ts";
3+
import { collectComposerInlineTokens, composerMathRanges } from "./composerInlineTokens.ts";
4+
import { serializeComposerFileLink } from "./composerTrigger.ts";
45

56
describe("collectComposerInlineTokens", () => {
67
it("collects file links, mentions, and skills with source ranges", () => {
@@ -217,4 +218,46 @@ describe("collectComposerInlineTokens", () => {
217218
"$real-skill",
218219
]);
219220
});
221+
222+
it.each([
223+
"app/routes/posts.$postId.tsx",
224+
"src/routes/_chat.$environmentId.$threadId.tsx",
225+
"pages/posts/[id].tsx",
226+
"app/blog/[...slug].tsx",
227+
])("keeps a file link whose name looks like TeX as one mention: %s", (path) => {
228+
const link = serializeComposerFileLink(path);
229+
expect(collectComposerInlineTokens(`fix ${link} please`)).toMatchObject([
230+
{ type: "mention", value: path, source: link },
231+
]);
232+
});
233+
234+
it.each(["app/routes/posts.$postId.tsx", "pages/posts/[id].tsx"])(
235+
"preserves a confirmed TeX-like pill when its trailing delimiter is removed: %s",
236+
(path) => {
237+
const link = serializeComposerFileLink(path);
238+
const confirmed = collectComposerInlineTokens(`fix ${link} `);
239+
240+
expect(confirmed).toMatchObject([{ type: "mention", value: path, source: link }]);
241+
expect(
242+
collectComposerInlineTokens(`fix ${link}`, { preserveTrailingFrom: confirmed }),
243+
).toEqual(confirmed);
244+
expect(collectComposerInlineTokens(`fix ${link}`)).toEqual([]);
245+
},
246+
);
247+
248+
it.each([
249+
"before $x + @README.md$ after",
250+
"before \\(x + @README.md\\) after",
251+
"\\[x + @README.md\\] after",
252+
"\\[\\text{use @README.md}\\] then go ",
253+
])("keeps a mention that ends a formula inside the formula: %s", (source) => {
254+
expect(collectComposerInlineTokens(source)).toEqual([]);
255+
});
256+
257+
it("still finds a formula after a mention that starts the line", () => {
258+
const link = serializeComposerFileLink("app/routes/posts.$postId.tsx");
259+
expect(composerMathRanges(`${link} sets $x_i$`).map((formula) => formula.source)).toEqual([
260+
"$x_i$",
261+
]);
262+
});
220263
});

‎packages/shared/src/composerInlineTokens.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,32 @@ function collectMentionTokens(text: string): ComposerInlineToken[] {
9999
return matches;
100100
}
101101

102+
/**
103+
* Formulas in composer text, ignoring the `$` and `\[` that file names such as
104+
* `posts.$postId.tsx` or `[id].tsx` put into a file link. File links are masked with letters,
105+
* not spaces, so a link at the start of a line cannot become an indented code block.
106+
* Bare `@path` mentions are not masked: one such as `@README.md$` can run into a formula's
107+
* closing delimiter, and a mention written inside a real formula stays covered by it.
108+
*/
109+
export function composerMathRanges(
110+
text: string,
111+
mentions: ReadonlyArray<ComposerInlineToken> = collectMentionTokens(text),
112+
) {
113+
let masked = text;
114+
for (const mention of mentions) {
115+
if (mention.type !== "mention" || !mention.source.startsWith("[")) continue;
116+
masked =
117+
masked.slice(0, mention.start) +
118+
"x".repeat(mention.end - mention.start) +
119+
masked.slice(mention.end);
120+
}
121+
return markdownMathRanges(masked).map(({ start, end }) => ({
122+
start,
123+
end,
124+
source: text.slice(start, end),
125+
}));
126+
}
127+
102128
export function collectComposerInlineTokens(
103129
text: string,
104130
options: CollectComposerInlineTokensOptions = {},
@@ -138,7 +164,8 @@ export function collectComposerInlineTokens(
138164

139165
// TeX can contain literal skill and file tokens, especially inside \text{}.
140166
// They remain formula source rather than becoming composer context chips.
141-
const math = matches.length > 0 ? markdownMathRanges(text) : [];
167+
// Confirmed trailing file links are masked too, so a kept chip is not read as TeX.
168+
const math = matches.length > 0 ? composerMathRanges(text, matches) : [];
142169
return matches
143170
.filter(
144171
(match) => !math.some((formula) => match.start < formula.end && match.end > formula.start),

0 commit comments

Comments
 (0)