forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkdown.tsx
More file actions
166 lines (157 loc) · 4.16 KB
/
Copy pathMarkdown.tsx
File metadata and controls
166 lines (157 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { FC, memo, useMemo } from "react";
import ReactMarkdown, { Options, Components } from "react-markdown";
import { CodeBlock } from "./CodeBlock";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import rehypeRaw from "rehype-raw";
const defaultComponents: Components = {
a({ children, ...props }) {
return (
<a
className="copilotKitMarkdownElement"
{...props}
target="_blank"
rel="noopener noreferrer"
>
{children}
</a>
);
},
// @ts-expect-error -- inline
code({ children, className, inline, ...props }) {
if (Array.isArray(children) && children.length) {
if (children[0] == "▍") {
return (
<span
style={{
animation: "pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite",
marginTop: "0.25rem",
}}
>
▍
</span>
);
}
children[0] = (children?.[0] as string).replace("`▍`", "▍");
}
const match = /language-(\w+)/.exec(className || "");
// Detect inline code: if it has a language class or contains newlines, it's likely a code block
// Otherwise, treat it as inline code
const hasLanguage = match && match[1];
const content = String(children);
const hasNewlines = content.includes("\n");
const isInline = !hasLanguage && !hasNewlines;
if (isInline) {
return (
<code
className={`copilotKitMarkdownElement copilotKitInlineCode ${className || ""}`}
{...props}
>
{children}
</code>
);
}
return (
<CodeBlock
language={(match && match[1]) || ""}
value={String(children).replace(/\n$/, "")}
{...props}
/>
);
},
h1: ({ children, ...props }) => (
<h1 className="copilotKitMarkdownElement" {...props}>
{children}
</h1>
),
h2: ({ children, ...props }) => (
<h2 className="copilotKitMarkdownElement" {...props}>
{children}
</h2>
),
h3: ({ children, ...props }) => (
<h3 className="copilotKitMarkdownElement" {...props}>
{children}
</h3>
),
h4: ({ children, ...props }) => (
<h4 className="copilotKitMarkdownElement" {...props}>
{children}
</h4>
),
h5: ({ children, ...props }) => (
<h5 className="copilotKitMarkdownElement" {...props}>
{children}
</h5>
),
h6: ({ children, ...props }) => (
<h6 className="copilotKitMarkdownElement" {...props}>
{children}
</h6>
),
p: ({ children, ...props }) => (
<div className="copilotKitMarkdownElement copilotKitParagraph" {...props}>
{children}
</div>
),
pre: ({ children, ...props }) => (
<pre className="copilotKitMarkdownElement" {...props}>
{children}
</pre>
),
blockquote: ({ children, ...props }) => (
<blockquote className="copilotKitMarkdownElement" {...props}>
{children}
</blockquote>
),
ul: ({ children, ...props }) => (
<ul className="copilotKitMarkdownElement" {...props}>
{children}
</ul>
),
li: ({ children, ...props }) => (
<li className="copilotKitMarkdownElement" {...props}>
{children}
</li>
),
};
const MemoizedReactMarkdown: FC<Options> = memo(ReactMarkdown);
type MarkdownProps = Omit<Options, "children"> & {
content: string;
};
export const Markdown = ({
content,
components,
remarkPlugins,
rehypePlugins,
...rest
}: MarkdownProps) => {
const mergedComponents = useMemo(
() => ({ ...defaultComponents, ...components }),
[components],
);
const mergedRemarkPlugins = useMemo<Options["remarkPlugins"]>(
() => [
remarkGfm,
[remarkMath, { singleDollarTextMath: false }],
...(remarkPlugins ?? []),
],
[remarkPlugins],
);
const mergedRehypePlugins = useMemo<Options["rehypePlugins"]>(
() => [rehypeRaw, ...(rehypePlugins ?? [])],
[rehypePlugins],
);
return (
<div className="copilotKitMarkdown">
<MemoizedReactMarkdown
{...rest}
components={mergedComponents}
remarkPlugins={mergedRemarkPlugins}
rehypePlugins={mergedRehypePlugins}
>
{content}
</MemoizedReactMarkdown>
</div>
);
};