Repository navigation
fix(chat): unclosed Codex angle-bracket links render as file chips #15590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { repairUnclosedAngleLinkDestinations } from "@t3tools/client-runtime/markdown-links"; | ||
| import type { MarkdownNode } from "react-native-nitro-markdown/headless"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { resolveMarkdownLinkPresentation } from "./markdownLinks"; | ||
| import { nativeMarkdownTextRuns } from "./nativeMarkdownText"; | ||
|
|
||
| /** | ||
| * md4c turns well-formed `[file](<local/path/file.md>)` into a link node | ||
| * whose href is the destination between the angle brackets. The native | ||
| * parser never builds a node for the unclosed shape, so this mirrors what | ||
| * it yields once the source is repaired. | ||
| */ | ||
| function parsedLinkNodeFor(markdown: string): MarkdownNode { | ||
| const repaired = repairUnclosedAngleLinkDestinations(markdown); | ||
| const href = repaired.slice(repaired.indexOf("(<") + 2, repaired.lastIndexOf(">)")); | ||
| const label = repaired.slice(repaired.indexOf("[") + 1, repaired.indexOf("]")); | ||
| const link = { type: "link", href, children: [{ type: "text", content: label }] }; | ||
| return { type: "document", children: [{ type: "paragraph", children: [link] }] } as MarkdownNode; | ||
| } | ||
|
|
||
| describe("unclosed angle-bracket file links", () => { | ||
| it.each([ | ||
| ["[file](<local/path/file.md)", "[file](<local/path/file.md>)"], | ||
| [ | ||
| "[Receipts](</Users/dara/Downloads/Lime Ride Artifacts/Bike Receipts)", | ||
| "[Receipts](</Users/dara/Downloads/Lime Ride Artifacts/Bike Receipts>)", | ||
| ], | ||
| ["[Open](<C:/Users/shawn/project/src/main.ts)", "[Open](<C:/Users/shawn/project/src/main.ts>)"], | ||
| ["- [file](<local/path/file.md)", "- [file](<local/path/file.md>)"], | ||
| ])("repairs %s before parsing", (source, expected) => { | ||
| expect(repairUnclosedAngleLinkDestinations(source)).toBe(expected); | ||
| }); | ||
|
|
||
| it.each([ | ||
| "[file](<local/path/file.md>)", | ||
| "[site](<https://example.com/docs)", | ||
| "```\n[file](<local/path/file.md)\n```", | ||
| ])("leaves %s alone", (source) => { | ||
| expect(repairUnclosedAngleLinkDestinations(source)).toBe(source); | ||
| }); | ||
|
|
||
| it("renders the repaired destination as a file chip run", () => { | ||
| expect(resolveMarkdownLinkPresentation("local/path/file.md")).toMatchObject({ | ||
| kind: "file", | ||
| label: "file.md", | ||
| }); | ||
| expect(nativeMarkdownTextRuns(parsedLinkNodeFor("[file](<local/path/file.md)"))).toMatchObject([ | ||
| { text: "file.md", href: "local/path/file.md", fileIcon: "markdown" }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("leaves a non-file destination without a chip", () => { | ||
| expect(repairUnclosedAngleLinkDestinations("[site](<https://example.com/docs)")).toBe( | ||
| "[site](<https://example.com/docs)", | ||
| ); | ||
| expect(resolveMarkdownLinkPresentation("<https://example.com/docs")).toMatchObject({ | ||
| kind: "link", | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -344,3 +344,33 @@ export function workspaceRelativeFilePath( | |
| if (!pathForCompare.startsWith(`${rootForCompare}/`)) return null; | ||
| return normalizedPath.slice(normalizedRoot.length + 1); | ||
| } | ||
|
|
||
| const FENCED_CODE_SEGMENT_PATTERN = /(```[\s\S]*?(?:```|$))/; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium
🤖 Copy this AI Prompt to have your agent fix this:
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| const PROSE_UNCLOSED_ANGLE_LINK_PATTERN = /(`[^`\n]+`)|\[([^\]\n]*)\]\(\s*<([^<>\n]+?)\s*\)/g; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Closes Codex's unclosed angle-bracket destinations (`[label](<path)`) so | ||
| * they parse as the same file link the well-formed shape produces. Only | ||
| * destinations that already read as file paths are repaired; fenced code, | ||
| * inline code, and every other malformed shape stay exactly as written. | ||
| */ | ||
| export function repairUnclosedAngleLinkDestinations(markdown: string): string { | ||
| // Fast path: an unclosed angle destination always contains "](<" in a row. | ||
| if (!markdown.includes("](<")) return markdown; | ||
| return markdown | ||
| .split(FENCED_CODE_SEGMENT_PATTERN) | ||
| .map((segment, index) => | ||
| index % 2 === 1 | ||
| ? segment | ||
| : segment.replace( | ||
| PROSE_UNCLOSED_ANGLE_LINK_PATTERN, | ||
| (match: string, codeSpan: string | undefined, label: string, destination: string) => { | ||
| if (codeSpan !== undefined) return match; | ||
| const path = destination.trim(); | ||
| if (parseMarkdownFileLink(path) === null) return match; | ||
| return `[${label}](<${path}>)`; | ||
| }, | ||
| ), | ||
| ) | ||
| .join(""); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.