Skip to content

Commit 5ae806c

Browse files
committed
Split markdown support into separate component
1 parent 40b1450 commit 5ae806c

File tree

2 files changed

+54
-42
lines changed

2 files changed

+54
-42
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React from 'react';
2+
import { Box, Text, Link as ChakraLink, Heading, useColorModeValue, useColorMode } from '@chakra-ui/react';
3+
import MonacoEditor from '@monaco-editor/react';
4+
import ReactMarkdown from 'react-markdown';
5+
6+
function ChakraMarkdown({ content }) {
7+
// Access Chakra's color mode
8+
const { colorMode } = useColorMode();
9+
10+
// Dynamic theme for Monaco Editor based on Chakra UI color mode
11+
const monacoTheme = colorMode === 'dark' ? 'vs-dark' : 'vs-light';
12+
13+
// Custom renderers for Markdown components using Chakra UI and Monaco Editor for code blocks
14+
const renderers = {
15+
h1: ({ children }) => <Heading as="h1" size="xl" mb={2}>{children}</Heading>,
16+
h2: ({ children }) => <Heading as="h2" size="lg" mb={2}>{children}</Heading>,
17+
h3: ({ children }) => <Heading as="h3" size="md" mb={2}>{children}</Heading>,
18+
a: ({ href, children }) => (
19+
<ChakraLink href={href} color="blue.500" isExternal>
20+
{children}
21+
</ChakraLink>
22+
),
23+
code: ({ inline, children, className }) => {
24+
const language = className ? className.replace('language-', '') : 'plaintext';
25+
if (inline) {
26+
return <Text as="code" bg={useColorModeValue("gray.100", "gray.700")} p="1" borderRadius="md">{children}</Text>;
27+
}
28+
return (
29+
<Box border="1px solid" borderColor={useColorModeValue("gray.200", "gray.600")} borderRadius="md" overflow="hidden" my={2}>
30+
<MonacoEditor
31+
height="200px"
32+
language={language}
33+
theme={monacoTheme}
34+
value={String(children).trim()}
35+
options={{
36+
readOnly: true,
37+
minimap: { enabled: false },
38+
scrollBeyondLastLine: false,
39+
lineNumbers: "off",
40+
}}
41+
/>
42+
</Box>
43+
);
44+
},
45+
};
46+
47+
return <ReactMarkdown components={renderers} children={content} />;
48+
}
49+
50+
export default ChakraMarkdown;

example/components/chat-with-markdown.tsx

+4-42
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import React, { useState } from 'react';
2-
import { ChakraProvider, Box, Textarea, Button, VStack, HStack, Avatar, Text, Link as ChakraLink, Heading, useColorMode, useColorModeValue } from '@chakra-ui/react';
3-
import ReactMarkdown from 'react-markdown';
4-
import MonacoEditor from '@monaco-editor/react'; // Monaco Editor
2+
import { ChakraProvider, Box, Textarea, Button, VStack, HStack, Avatar, Text, useColorModeValue } from '@chakra-ui/react';
3+
import ChakraMarkdown from './chakra-markdown'; // Import the renamed component
54

65
function ChatApp() {
76
const [messages, setMessages] = useState([]);
87
const [input, setInput] = useState("");
9-
const { colorMode, toggleColorMode } = useColorMode(); // Chakra color mode
108

119
const sendMessage = () => {
1210
if (input.trim() !== "") {
@@ -21,43 +19,6 @@ function ChatApp() {
2119
}
2220
};
2321

24-
// Dynamic theme for Monaco Editor based on Chakra UI color mode
25-
const monacoTheme = colorMode === 'dark' ? 'vs-dark' : 'vs-light';
26-
27-
// Custom renderers for Markdown components using Chakra UI and Monaco Editor for code blocks
28-
const renderers = {
29-
h1: ({ children }) => <Heading as="h1" size="xl" mb={2}>{children}</Heading>,
30-
h2: ({ children }) => <Heading as="h2" size="lg" mb={2}>{children}</Heading>,
31-
h3: ({ children }) => <Heading as="h3" size="md" mb={2}>{children}</Heading>,
32-
a: ({ href, children }) => (
33-
<ChakraLink href={href} color="blue.500" isExternal>
34-
{children}
35-
</ChakraLink>
36-
),
37-
code: ({ inline, children, className }) => {
38-
const language = className ? className.replace('language-', '') : 'plaintext';
39-
if (inline) {
40-
return <Text as="code" bg={useColorModeValue("gray.100", "gray.700")} p="1" borderRadius="md">{children}</Text>;
41-
}
42-
return (
43-
<Box border="1px solid" borderColor={useColorModeValue("gray.200", "gray.600")} borderRadius="md" overflow="hidden" my={2}>
44-
<MonacoEditor
45-
height="200px"
46-
language={language}
47-
theme={monacoTheme}
48-
value={String(children).trim()}
49-
options={{
50-
readOnly: true,
51-
minimap: { enabled: false },
52-
scrollBeyondLastLine: false,
53-
lineNumbers: "off",
54-
}}
55-
/>
56-
</Box>
57-
);
58-
},
59-
};
60-
6122
return (
6223
<ChakraProvider>
6324
<Box bg={useColorModeValue("gray.100", "gray.900")} h="100vh" p={4} display="flex" flexDirection="column">
@@ -79,7 +40,8 @@ function ChatApp() {
7940
<Text fontSize="sm" fontWeight="bold">
8041
{message.sender}
8142
</Text>
82-
<ReactMarkdown components={renderers} children={message.text} />
43+
{/* Rendering the message text with markdown support */}
44+
<ChakraMarkdown content={message.text} />
8345
<Text fontSize="xs" color="gray.500">
8446
{message.timestamp}
8547
</Text>

0 commit comments

Comments
 (0)