-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chat): add support for code blocks
- Loading branch information
Showing
11 changed files
with
325 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
apps/chat/src/modules/chats/conversation/messages/content/chat-message-markdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import clsx from 'clsx'; | ||
import { Children, memo } from 'react'; | ||
import Markdown from 'react-markdown'; | ||
import rehypeRaw from 'rehype-raw'; | ||
import remarkGfm from 'remark-gfm'; | ||
|
||
import { ChatCodeBlock } from './widgets'; | ||
|
||
type Props = { | ||
content: string; | ||
inlinedReactComponents?: Record<string, React.ReactNode>; | ||
}; | ||
|
||
export const ChatMessageMarkdown = memo(({ content, inlinedReactComponents = {} }: Props) => { | ||
return ( | ||
<Markdown | ||
className={clsx( | ||
'prose-td:p-2 prose-th:p-2 prose-td:border prose-th:border prose-td:border-gray-300 prose-th:border-gray-300 prose-table:border-collapse', | ||
'prose-a:underline prose-code:overflow-auto', | ||
'prose-ol:list-decimal chat-markdown prose-sm prose-ul:list-disc', | ||
'prose-hr:my-3', | ||
'[&_.footnotes]:text-xs [&_.footnotes]:text-gray-600', | ||
'[&_pre]:!p-0 [&_pre]:!m-0 [&_pre]:!bg-transparent', | ||
)} | ||
remarkPlugins={[remarkGfm]} | ||
rehypePlugins={[rehypeRaw]} | ||
components={{ | ||
blockquote: ({ children }) => ( | ||
<blockquote className="my-2 pl-4 border-gray-300 border-l-4 italic">{children}</blockquote> | ||
), | ||
a: ({ children, ...props }) => { | ||
if (typeof children === 'string' && children === '$embed') { | ||
const key = props.href?.replace(/^react\$/, ''); | ||
|
||
if (key && inlinedReactComponents[key]) { | ||
return inlinedReactComponents[key]; | ||
} | ||
} | ||
|
||
return <a {...props}>{children}</a>; | ||
}, | ||
pre: ({ children }) => { | ||
const hasCodeTag = ( | ||
Children | ||
// eslint-disable-next-line react/no-children-to-array | ||
.toArray(children) | ||
.some((child: any) => child.props?.className?.startsWith('language-')) | ||
); | ||
|
||
if (hasCodeTag) { | ||
return <>{children}</>; | ||
} | ||
|
||
return <pre className="bg-gray-100 p-2 rounded-md">{children}</pre>; | ||
}, | ||
code({ node, inline, className, children, ...props }: any) { | ||
const match = /language-(\w+)/.exec(className || ''); | ||
|
||
return !inline && match | ||
? ( | ||
<ChatCodeBlock language={match[1]}> | ||
{String(children).replace(/\n$/, '')} | ||
</ChatCodeBlock> | ||
) | ||
: ( | ||
<code className={className} {...props}> | ||
{children} | ||
</code> | ||
); | ||
}, | ||
}} | ||
> | ||
{content} | ||
</Markdown> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 0 additions & 44 deletions
44
apps/chat/src/modules/chats/conversation/messages/content/parser/message-markdown.tsx
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
apps/chat/src/modules/chats/conversation/messages/content/widgets/chat-code-block.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Check, Copy } from 'lucide-react'; | ||
import { useState } from 'react'; | ||
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; | ||
import { oneLight } from 'react-syntax-highlighter/dist/cjs/styles/prism'; | ||
|
||
import { useI18n } from '~/i18n'; | ||
|
||
type ChatCodeBlockProps = { | ||
language?: string; | ||
children: string; | ||
}; | ||
|
||
export function ChatCodeBlock({ language, children }: ChatCodeBlockProps) { | ||
const t = useI18n().pack.chat.widgets.code; | ||
const [copied, setCopied] = useState(false); | ||
|
||
const handleCopy = async () => { | ||
await navigator.clipboard.writeText(children); | ||
|
||
setCopied(true); | ||
setTimeout(() => setCopied(false), 2000); | ||
}; | ||
|
||
return ( | ||
<div className="relative bg-[#f7f7f8] border border-gray-200 rounded-md"> | ||
<div className="flex justify-between items-center px-4 pt-2"> | ||
<span className="text-gray-600 text-xs">{language || 'plaintext'}</span> | ||
<button | ||
type="button" | ||
onClick={handleCopy} | ||
className="flex items-center gap-1 text-gray-600 hover:text-gray-900 text-xs transition-colors" | ||
> | ||
{copied ? <Check className="w-3 h-3" /> : <Copy className="w-3 h-3" />} | ||
{copied ? t.copied : t.copy} | ||
</button> | ||
</div> | ||
|
||
<SyntaxHighlighter | ||
language={language || 'plaintext'} | ||
style={oneLight} | ||
customStyle={{ | ||
margin: 0, | ||
padding: '1rem', | ||
background: 'transparent', | ||
}} | ||
PreTag="div" | ||
> | ||
{children} | ||
</SyntaxHighlighter> | ||
</div> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
apps/chat/src/modules/chats/conversation/messages/content/widgets/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './chat-code-block'; |
Oops, something went wrong.