Skip to content

Commit 920bc63

Browse files
committed
Fix file mentions rendering as block elements in submitted messages
1 parent 8aeeb52 commit 920bc63

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

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: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
1-
import { MarkdownRenderer } from "@features/editor/components/MarkdownRenderer";
1+
import {
2+
baseComponents,
3+
remarkPlugins,
4+
} from "@features/editor/components/MarkdownRenderer";
25
import { File } from "@phosphor-icons/react";
3-
import { Box, Code } from "@radix-ui/themes";
6+
import { Box, Code, Text } from "@radix-ui/themes";
47
import type { ReactNode } from "react";
8+
import { memo } from "react";
9+
import type { Components } from "react-markdown";
10+
import ReactMarkdown from "react-markdown";
511

612
interface UserMessageProps {
713
content: string;
814
}
915

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

@@ -32,6 +63,7 @@ function parseFileMentions(content: string): ReactNode[] {
3263
alignItems: "center",
3364
gap: "4px",
3465
verticalAlign: "middle",
66+
margin: "0 6px",
3567
}}
3668
>
3769
<File size={12} />
@@ -44,7 +76,7 @@ function parseFileMentions(content: string): ReactNode[] {
4476

4577
if (lastIndex < content.length) {
4678
parts.push(
47-
<MarkdownRenderer
79+
<InlineMarkdown
4880
key={`text-${lastIndex}`}
4981
content={content.slice(lastIndex)}
5082
/>,
@@ -66,7 +98,12 @@ export function UserMessage({ content }: UserMessageProps) {
6698
{hasFileMentions ? (
6799
parseFileMentions(content)
68100
) : (
69-
<MarkdownRenderer content={content} />
101+
<ReactMarkdown
102+
remarkPlugins={remarkPlugins}
103+
components={baseComponents}
104+
>
105+
{content}
106+
</ReactMarkdown>
70107
)}
71108
</Box>
72109
</Box>

0 commit comments

Comments
 (0)