Skip to content
This repository was archived by the owner on Oct 10, 2026. It is now read-only.

Commit 55dd016

Browse files
Link inline code file paths in chat markdown (pingdotgg#4726)
Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 3c50a64 commit 55dd016

4 files changed

Lines changed: 491 additions & 56 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 149 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ import {
6767
import { remarkNormalizeListItemIndentation } from "../markdown-list-indentation";
6868
import {
6969
normalizeMarkdownLinkDestination,
70+
resolveInlineCodeFileLinkMeta,
7071
resolveMarkdownFileLinkMeta,
7172
rewriteMarkdownFileUriHref,
73+
type MarkdownFileLinkMeta,
7274
} from "../markdown-links";
7375
import { readLocalApi } from "../localApi";
7476
import { cn } from "../lib/utils";
@@ -162,7 +164,7 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
162164
attributes: {
163165
...defaultSchema.attributes,
164166
"*": (defaultSchema.attributes?.["*"] ?? []).filter((attribute) => attribute !== "title"),
165-
code: [...(defaultSchema.attributes?.code ?? []), "dataCodeMeta"],
167+
code: [...(defaultSchema.attributes?.code ?? []), "dataCodeMeta", "dataInlineCode"],
166168
},
167169
protocols: {
168170
...defaultSchema.protocols,
@@ -174,13 +176,15 @@ const CHAT_MARKDOWN_REMARK_PLUGINS = [
174176
remarkGfm,
175177
remarkNormalizeListItemIndentation,
176178
remarkPreserveCodeMeta,
179+
remarkTagInlineCode,
177180
] satisfies NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
178181

179182
const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
180183
remarkGfm,
181184
remarkNormalizeListItemIndentation,
182185
remarkBreaks,
183186
remarkPreserveCodeMeta,
187+
remarkTagInlineCode,
184188
] satisfies NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
185189

186190
const CHAT_MARKDOWN_REHYPE_PLUGINS = [
@@ -253,6 +257,33 @@ function remarkPreserveCodeMeta() {
253257
};
254258
}
255259

260+
/**
261+
* Fenced code also lands on the `code` component, and inline vs block is no
262+
* longer distinguishable there once both render `<code>` — so inline spans are
263+
* tagged on the mdast, where the distinction still exists. Code inside a link
264+
* label stays untagged: linkifying it would nest an anchor inside the link's
265+
* anchor and steal its clicks.
266+
*/
267+
function remarkTagInlineCode() {
268+
return (tree: MarkdownAstNode) => {
269+
const visit = (node: MarkdownAstNode, insideLink: boolean) => {
270+
if (node.type === "inlineCode" && !insideLink) {
271+
node.data = {
272+
...node.data,
273+
hProperties: {
274+
...node.data?.hProperties,
275+
dataInlineCode: "",
276+
},
277+
};
278+
}
279+
const childInsideLink = insideLink || node.type === "link" || node.type === "linkReference";
280+
node.children?.forEach((child) => visit(child, childInsideLink));
281+
};
282+
283+
visit(tree, false);
284+
};
285+
}
286+
256287
function nodeToPlainText(node: ReactNode): string {
257288
if (typeof node === "string" || typeof node === "number") {
258289
return String(node);
@@ -276,11 +307,17 @@ function extractCodeBlock(
276307

277308
const onlyChild = childNodes[0];
278309
if (
279-
!isValidElement<{ className?: string; children?: ReactNode }>(onlyChild) ||
280-
onlyChild.type !== "code"
310+
!isValidElement<{ className?: string; children?: ReactNode; node?: { tagName?: string } }>(
311+
onlyChild,
312+
)
281313
) {
282314
return null;
283315
}
316+
// With a custom `code` component the child's type is that component, not
317+
// the "code" tag — the hast node react-markdown attaches still names it.
318+
if (onlyChild.type !== "code" && onlyChild.props.node?.tagName !== "code") {
319+
return null;
320+
}
284321

285322
return {
286323
className: onlyChild.props.className,
@@ -816,6 +853,21 @@ function buildFileLinkParentSuffixByPath(filePaths: ReadonlyArray<string>): Map<
816853
return suffixByPath;
817854
}
818855

856+
const FENCED_CODE_SEGMENT_PATTERN = /(```[\s\S]*?(?:```|$))/;
857+
const INLINE_CODE_SPAN_PATTERN = /`([^`\n]+)`/g;
858+
859+
function extractInlineCodeSpans(text: string): string[] {
860+
const spans: string[] = [];
861+
const segments = text.split(FENCED_CODE_SEGMENT_PATTERN);
862+
for (let index = 0; index < segments.length; index += 2) {
863+
for (const match of (segments[index] ?? "").matchAll(INLINE_CODE_SPAN_PATTERN)) {
864+
const span = match[1]?.trim();
865+
if (span) spans.push(span);
866+
}
867+
}
868+
return spans;
869+
}
870+
819871
function extractMarkdownLinkHrefs(text: string): string[] {
820872
const hrefs: string[] = [];
821873
for (const match of text.matchAll(MARKDOWN_LINK_HREF_PATTERN)) {
@@ -1281,10 +1333,24 @@ function ChatMarkdown({
12811333
}
12821334
return metaByHref;
12831335
}, [cwd, text]);
1336+
const inlineCodeFileLinkMetaByText = useMemo(() => {
1337+
const metaByText = new Map<string, MarkdownFileLinkMeta>();
1338+
for (const span of extractInlineCodeSpans(text)) {
1339+
if (metaByText.has(span)) continue;
1340+
const meta = resolveInlineCodeFileLinkMeta(span, cwd);
1341+
if (meta) {
1342+
metaByText.set(span, meta);
1343+
}
1344+
}
1345+
return metaByText;
1346+
}, [cwd, text]);
12841347
const fileLinkParentSuffixByPath = useMemo(() => {
1285-
const filePaths = [...markdownFileLinkMetaByHref.values()].map((meta) => meta.filePath);
1348+
const filePaths = [
1349+
...[...markdownFileLinkMetaByHref.values()].map((meta) => meta.filePath),
1350+
...[...inlineCodeFileLinkMetaByText.values()].map((meta) => meta.filePath),
1351+
];
12861352
return buildFileLinkParentSuffixByPath(filePaths);
1287-
}, [markdownFileLinkMetaByHref]);
1353+
}, [inlineCodeFileLinkMetaByText, markdownFileLinkMetaByHref]);
12881354
const markdownUrlTransform = useCallback((href: string) => {
12891355
return rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href);
12901356
}, []);
@@ -1339,8 +1405,49 @@ function ChatMarkdown({
13391405
},
13401406
[createAssetUrl, openPreview, preparedConnection, threadRef],
13411407
);
1342-
const markdownComponents = useMemo<Components>(
1343-
() => ({
1408+
const markdownComponents = useMemo<Components>(() => {
1409+
const fileLinkChip = (
1410+
fileLinkMeta: MarkdownFileLinkMeta,
1411+
copyMarkdown: string,
1412+
className?: string,
1413+
) => {
1414+
const parentSuffix = fileLinkParentSuffixByPath.get(fileLinkMeta.filePath);
1415+
const labelParts = [fileLinkMeta.basename];
1416+
if (typeof parentSuffix === "string" && parentSuffix.length > 0) {
1417+
labelParts.push(parentSuffix);
1418+
}
1419+
if (fileLinkMeta.line) {
1420+
labelParts.push(
1421+
`L${fileLinkMeta.line}${fileLinkMeta.column ? `:C${fileLinkMeta.column}` : ""}`,
1422+
);
1423+
}
1424+
1425+
return (
1426+
<MarkdownFileLink
1427+
href={fileLinkMeta.targetPath}
1428+
targetPath={fileLinkMeta.targetPath}
1429+
iconPath={fileLinkMeta.filePath}
1430+
displayPath={fileLinkMeta.displayPath}
1431+
workspaceRelativePath={fileLinkMeta.workspaceRelativePath}
1432+
line={fileLinkMeta.line}
1433+
label={labelParts.join(" · ")}
1434+
copyMarkdown={copyMarkdown}
1435+
theme={resolvedTheme}
1436+
threadRef={threadRef}
1437+
onOpen={openInPreferredEditor}
1438+
onOpenInBrowser={
1439+
threadRef &&
1440+
isPreviewSupportedInRuntime() &&
1441+
isBrowserPreviewFile(fileLinkMeta.filePath)
1442+
? () => openMarkdownFileInPreview(fileLinkMeta.filePath)
1443+
: undefined
1444+
}
1445+
className={className}
1446+
/>
1447+
);
1448+
};
1449+
1450+
return {
13441451
p({ node: _node, children, ...props }) {
13451452
return <p {...props}>{renderSkillInlineMarkdownChildren(children, skills)}</p>;
13461453
},
@@ -1455,39 +1562,26 @@ function ChatMarkdown({
14551562
);
14561563
}
14571564

1458-
const parentSuffix = fileLinkParentSuffixByPath.get(fileLinkMeta.filePath);
1459-
const labelParts = [fileLinkMeta.basename];
1460-
if (typeof parentSuffix === "string" && parentSuffix.length > 0) {
1461-
labelParts.push(parentSuffix);
1462-
}
1463-
if (fileLinkMeta.line) {
1464-
labelParts.push(
1465-
`L${fileLinkMeta.line}${fileLinkMeta.column ? `:C${fileLinkMeta.column}` : ""}`,
1466-
);
1565+
return fileLinkChip(
1566+
fileLinkMeta,
1567+
`[${fileLinkMeta.basename}](${normalizedHref})`,
1568+
props.className,
1569+
);
1570+
},
1571+
code({ node, children, className, ...props }) {
1572+
if (node?.properties?.dataInlineCode != null) {
1573+
const codeText = nodeToPlainText(children);
1574+
const fileLinkMeta =
1575+
inlineCodeFileLinkMetaByText.get(codeText.trim()) ??
1576+
resolveInlineCodeFileLinkMeta(codeText, cwd);
1577+
if (fileLinkMeta) {
1578+
return fileLinkChip(fileLinkMeta, `\`${codeText}\``);
1579+
}
14671580
}
1468-
14691581
return (
1470-
<MarkdownFileLink
1471-
href={fileLinkMeta.targetPath}
1472-
targetPath={fileLinkMeta.targetPath}
1473-
iconPath={fileLinkMeta.filePath}
1474-
displayPath={fileLinkMeta.displayPath}
1475-
workspaceRelativePath={fileLinkMeta.workspaceRelativePath}
1476-
line={fileLinkMeta.line}
1477-
label={labelParts.join(" · ")}
1478-
copyMarkdown={`[${fileLinkMeta.basename}](${normalizedHref})`}
1479-
theme={resolvedTheme}
1480-
threadRef={threadRef}
1481-
onOpen={openInPreferredEditor}
1482-
onOpenInBrowser={
1483-
threadRef &&
1484-
isPreviewSupportedInRuntime() &&
1485-
isBrowserPreviewFile(fileLinkMeta.filePath)
1486-
? () => openMarkdownFileInPreview(fileLinkMeta.filePath)
1487-
: undefined
1488-
}
1489-
className={props.className}
1490-
/>
1582+
<code {...props} className={className}>
1583+
{children}
1584+
</code>
14911585
);
14921586
},
14931587
table({ node: _node, ...props }) {
@@ -1524,22 +1618,23 @@ function ChatMarkdown({
15241618
</MarkdownCodeBlock>
15251619
);
15261620
},
1527-
}),
1528-
[
1529-
diffThemeName,
1530-
fileLinkParentSuffixByPath,
1531-
isStreaming,
1532-
markdownFileLinkMetaByHref,
1533-
onTaskListChange,
1534-
openInPreferredEditor,
1535-
openExternalLinkInPreview,
1536-
openMarkdownFileInPreview,
1537-
resolvedTheme,
1538-
skills,
1539-
text,
1540-
threadRef,
1541-
],
1542-
);
1621+
};
1622+
}, [
1623+
cwd,
1624+
diffThemeName,
1625+
fileLinkParentSuffixByPath,
1626+
inlineCodeFileLinkMetaByText,
1627+
isStreaming,
1628+
markdownFileLinkMetaByHref,
1629+
onTaskListChange,
1630+
openInPreferredEditor,
1631+
openExternalLinkInPreview,
1632+
openMarkdownFileInPreview,
1633+
resolvedTheme,
1634+
skills,
1635+
text,
1636+
threadRef,
1637+
]);
15431638

15441639
return (
15451640
<div

‎apps/web/src/components/chat/SkillInlineText.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ export function renderSkillInlineMarkdownChildren(
5252
if (typeof child === "string") {
5353
return <SkillInlineText text={child} skills={skills} />;
5454
}
55-
if (!isValidElement<{ children?: ReactNode }>(child)) {
55+
if (!isValidElement<{ children?: ReactNode; node?: { tagName?: string } }>(child)) {
5656
return child;
5757
}
58-
if (child.type === "code" || child.type === "a") {
58+
// Custom react-markdown components replace the intrinsic type, so also
59+
// check the hast node they carry.
60+
const markdownTagName = typeof child.type === "string" ? child.type : child.props.node?.tagName;
61+
if (markdownTagName === "code" || markdownTagName === "a") {
5962
return child;
6063
}
6164
if (!("children" in child.props)) {

0 commit comments

Comments
 (0)