Skip to content

Commit ff67e41

Browse files
authored
fix: File mentions rendering as block elements in submitted messages (#769)
1 parent 8aeeb52 commit ff67e41

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

apps/twig/src/renderer/features/editor/components/MarkdownRenderer.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const HeadingText = ({ children }: { children: React.ReactNode }) => (
3131
</Text>
3232
);
3333

34-
const components: Components = {
34+
export const baseComponents: Components = {
3535
h1: ({ children }) => <HeadingText>{children}</HeadingText>,
3636
h2: ({ children }) => <HeadingText>{children}</HeadingText>,
3737
h3: ({ children }) => <HeadingText>{children}</HeadingText>,
@@ -151,7 +151,7 @@ const components: Components = {
151151
),
152152
};
153153

154-
const remarkPlugins = [remarkGfm];
154+
export const remarkPlugins = [remarkGfm];
155155

156156
export const MarkdownRenderer = memo(function MarkdownRenderer({
157157
content,
@@ -161,7 +161,7 @@ export const MarkdownRenderer = memo(function MarkdownRenderer({
161161
[content],
162162
);
163163
return (
164-
<ReactMarkdown remarkPlugins={remarkPlugins} components={components}>
164+
<ReactMarkdown remarkPlugins={remarkPlugins} components={baseComponents}>
165165
{processedContent}
166166
</ReactMarkdown>
167167
);

apps/twig/src/renderer/features/sessions/components/session-update/UserMessage.tsx

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
1-
import { MarkdownRenderer } from "@features/editor/components/MarkdownRenderer";
1+
import {
2+
baseComponents,
3+
MarkdownRenderer,
4+
remarkPlugins,
5+
} from "@features/editor/components/MarkdownRenderer";
26
import { File } from "@phosphor-icons/react";
3-
import { Box, Code } from "@radix-ui/themes";
7+
import { Box, Code, Text } from "@radix-ui/themes";
48
import type { ReactNode } from "react";
9+
import { memo } from "react";
10+
import type { Components } from "react-markdown";
11+
import ReactMarkdown from "react-markdown";
512

613
interface UserMessageProps {
714
content: string;
815
}
916

17+
/**
18+
* Markdown components that render paragraphs as inline spans so that
19+
* text chunks between file mentions stay on the same line.
20+
*/
21+
const inlineComponents: Components = {
22+
...baseComponents,
23+
p: ({ children }) => (
24+
<Text as="span" size="1" color="gray" highContrast>
25+
{children}
26+
</Text>
27+
),
28+
};
29+
30+
const InlineMarkdown = memo(function InlineMarkdown({
31+
content,
32+
}: {
33+
content: string;
34+
}) {
35+
return (
36+
<ReactMarkdown remarkPlugins={remarkPlugins} components={inlineComponents}>
37+
{content}
38+
</ReactMarkdown>
39+
);
40+
});
41+
1042
function parseFileMentions(content: string): ReactNode[] {
1143
const fileTagRegex = /<file\s+path="([^"]+)"\s*\/>/g;
1244
const parts: ReactNode[] = [];
@@ -16,7 +48,7 @@ function parseFileMentions(content: string): ReactNode[] {
1648
if (match.index !== undefined && match.index > lastIndex) {
1749
const textBefore = content.slice(lastIndex, match.index);
1850
parts.push(
19-
<MarkdownRenderer key={`text-${lastIndex}`} content={textBefore} />,
51+
<InlineMarkdown key={`text-${lastIndex}`} content={textBefore} />,
2052
);
2153
}
2254

@@ -32,6 +64,7 @@ function parseFileMentions(content: string): ReactNode[] {
3264
alignItems: "center",
3365
gap: "4px",
3466
verticalAlign: "middle",
67+
margin: "0 6px",
3568
}}
3669
>
3770
<File size={12} />
@@ -44,7 +77,7 @@ function parseFileMentions(content: string): ReactNode[] {
4477

4578
if (lastIndex < content.length) {
4679
parts.push(
47-
<MarkdownRenderer
80+
<InlineMarkdown
4881
key={`text-${lastIndex}`}
4982
content={content.slice(lastIndex)}
5083
/>,

0 commit comments

Comments
 (0)