Skip to content

Commit 6fb7ff1

Browse files
committed
Add MonacoEditor for code blocks
1 parent c0739a1 commit 6fb7ff1

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

example/components/chat-with-markdown.tsx

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useState } from 'react';
22
import { ChakraProvider, Box, Textarea, Button, VStack, HStack, Avatar, Text, Link as ChakraLink, Heading } from '@chakra-ui/react';
33
import ReactMarkdown from 'react-markdown';
4+
import MonacoEditor from '@monaco-editor/react'; // Monaco Editor
45

56
function ChatApp() {
67
const [messages, setMessages] = useState([]);
@@ -19,7 +20,7 @@ function ChatApp() {
1920
}
2021
};
2122

22-
// Custom renderers for Markdown components using Chakra UI
23+
// Custom renderers for Markdown components using Chakra UI and Monaco Editor for code blocks
2324
const renderers = {
2425
// Render headers using Chakra's Heading component
2526
h1: ({ children }) => <Heading as="h1" size="xl" mb={2}>{children}</Heading>,
@@ -32,6 +33,30 @@ function ChatApp() {
3233
{children}
3334
</ChakraLink>
3435
),
36+
37+
// Use Monaco Editor for code blocks
38+
code: ({ inline, children, className }) => {
39+
const language = className ? className.replace('language-', '') : 'plaintext';
40+
if (inline) {
41+
return <Text as="code" bg="gray.100" p="1" borderRadius="md">{children}</Text>;
42+
}
43+
return (
44+
<Box border="1px solid" borderColor="gray.200" borderRadius="md" overflow="hidden" my={2}>
45+
<MonacoEditor
46+
height="200px"
47+
language={language}
48+
theme="vs-dark"
49+
value={String(children).trim()}
50+
options={{
51+
readOnly: true,
52+
minimap: { enabled: false },
53+
scrollBeyondLastLine: false,
54+
lineNumbers: "off",
55+
}}
56+
/>
57+
</Box>
58+
);
59+
},
3560
};
3661

3762
return (
@@ -51,7 +76,7 @@ function ChatApp() {
5176
{messages.map((message) => (
5277
<HStack key={message.id} align="start" w="100%">
5378
<Avatar name={message.sender} size="sm" />
54-
<Box bg="blue.50" p={3} borderRadius="lg" maxW="80%">
79+
<Box bg="blue.50" p={3} borderRadius="lg" width="80%">
5580
<Text fontSize="sm" fontWeight="bold">
5681
{message.sender}
5782
</Text>
@@ -72,8 +97,8 @@ function ChatApp() {
7297
onChange={(e) => setInput(e.target.value)}
7398
placeholder="Type a message with Markdown..."
7499
bg="white"
75-
resize="none" // Disable manual resize
76-
overflow="hidden" // Hide overflow when max height is reached
100+
resize="none"
101+
overflow="hidden"
77102
flex="1"
78103
/>
79104
<Button colorScheme="blue" onClick={sendMessage}>

example/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@chakra-ui/react": "^2.10.3",
1414
"@emotion/react": "^11.13.3",
1515
"@emotion/styled": "^11.13.0",
16+
"@monaco-editor/react": "^4.6.0",
1617
"framer-motion": "^11.11.9",
1718
"next": "14.2.15",
1819
"react": "18.3.1",

example/yarn.lock

+19
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,20 @@
321321
"@jridgewell/resolve-uri" "^3.1.0"
322322
"@jridgewell/sourcemap-codec" "^1.4.14"
323323

324+
"@monaco-editor/loader@^1.4.0":
325+
version "1.4.0"
326+
resolved "https://registry.yarnpkg.com/@monaco-editor/loader/-/loader-1.4.0.tgz#f08227057331ec890fa1e903912a5b711a2ad558"
327+
integrity sha512-00ioBig0x642hytVspPl7DbQyaSWRaolYie/UFNjoTdvoKPzo6xrXLhTk9ixgIKcLH5b5vDOjVNiGyY+uDCUlg==
328+
dependencies:
329+
state-local "^1.0.6"
330+
331+
"@monaco-editor/react@^4.6.0":
332+
version "4.6.0"
333+
resolved "https://registry.yarnpkg.com/@monaco-editor/react/-/react-4.6.0.tgz#bcc68671e358a21c3814566b865a54b191e24119"
334+
integrity sha512-RFkU9/i7cN2bsq/iTkurMWOEErmYcY6JiQI3Jn+WeR/FGISH8JbHERjpS9oRuSOPvDMJI0Z8nJeKkbOs9sBYQw==
335+
dependencies:
336+
"@monaco-editor/loader" "^1.4.0"
337+
324338
325339
version "14.2.15"
326340
resolved "https://registry.yarnpkg.com/@next/env/-/env-14.2.15.tgz#06d984e37e670d93ddd6790af1844aeb935f332f"
@@ -1604,6 +1618,11 @@ sprintf-js@~1.0.2:
16041618
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
16051619
integrity sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==
16061620

1621+
state-local@^1.0.6:
1622+
version "1.0.7"
1623+
resolved "https://registry.yarnpkg.com/state-local/-/state-local-1.0.7.tgz#da50211d07f05748d53009bee46307a37db386d5"
1624+
integrity sha512-HTEHMNieakEnoe33shBYcZ7NX83ACUjCu8c40iOGEZsngj9zRnkqS9j1pqQPXwobB0ZcVTk27REb7COQ0UR59w==
1625+
16071626
streamsearch@^1.1.0:
16081627
version "1.1.0"
16091628
resolved "https://registry.yarnpkg.com/streamsearch/-/streamsearch-1.1.0.tgz#404dd1e2247ca94af554e841a8ef0eaa238da764"

0 commit comments

Comments
 (0)