Skip to content

Commit 211f754

Browse files
feat: add code highlight with markdown
1 parent 4984d9a commit 211f754

File tree

4 files changed

+1845
-9
lines changed

4 files changed

+1845
-9
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# misc
1919
.DS_Store
2020
*.pem
21+
.idea
2122

2223
# debug
2324
npm-debug.log*
@@ -27,6 +28,7 @@ yarn-error.log*
2728

2829
# local env files
2930
.env*.local
31+
.env
3032

3133
# vercel
3234
.vercel

app/components/Message.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import ReactMarkdown from 'react-markdown';
2+
import { dark } from "react-syntax-highlighter/src/styles/hljs";
3+
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
4+
15
const Message = ({ message, isUser }) => {
26
let containerClass = "bg-gray-50";
37
if (isUser) {
@@ -25,14 +29,30 @@ const Message = ({ message, isUser }) => {
2529
)}
2630

2731
<div className="flex flex-col text-sm sm:text-base flex-1 gap-y-4 mt-1">
28-
{message.split("\n").map(
29-
(text, index) =>
30-
text.length > 0 && (
31-
<span key={index} className="min-w-0">
32-
{text}
33-
</span>
34-
)
35-
)}
32+
<ReactMarkdown
33+
components={{
34+
code(props) {
35+
const { children, className, node, ...rest } = props
36+
const match = /language-(\w+)/.exec(className || '')
37+
return match ? (
38+
<SyntaxHighlighter
39+
{...rest}
40+
PreTag="div"
41+
language={match[1]}
42+
style={dark}
43+
>
44+
{String(children).replace(/\n$/, '')}
45+
</SyntaxHighlighter>
46+
) : (
47+
<code {...rest} className={className}>
48+
{children}
49+
</code>
50+
)
51+
}
52+
}}
53+
>
54+
{message}
55+
</ReactMarkdown>
3656
</div>
3757
</div>
3858
);

0 commit comments

Comments
 (0)