-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement instant Markdown rendering for chats
Now we have add a additional instant Markdown renderer along side of the Live(auto-typing) Markdown Style. Also now added support of Markdown Style for user inserted text.
- Loading branch information
1 parent
8d9a4ee
commit 9fcf187
Showing
6 changed files
with
124 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import Markdown from "react-markdown"; | ||
import remarkGfm from "remark-gfm"; | ||
import { PrismLight as SyntaxHighlighter } from "react-syntax-highlighter"; | ||
import { nightOwl } from "react-syntax-highlighter/dist/esm/styles/prism"; | ||
import { CopyToClipboard } from "react-copy-to-clipboard"; | ||
import { useStopTextGeneration } from "../context/StopTextGeneration"; | ||
import styles from "./styles/MarkdownStyle.module.css"; | ||
|
||
interface LiveMarkdownStyleProps { | ||
text: string; | ||
setButtonsDisabled?: React.Dispatch<React.SetStateAction<boolean>> | null; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const SyntaxHighlight = ({ children, code, ...props }: any) => { | ||
const [isCopied, setIsCopied] = useState(false); | ||
|
||
const handleCopy = () => { | ||
setIsCopied(true); | ||
setTimeout(() => setIsCopied(false), 2000); | ||
}; | ||
|
||
return ( | ||
<> | ||
<SyntaxHighlighter | ||
style={nightOwl} | ||
language="javascript" | ||
showLineNumbers | ||
customStyle={{ | ||
padding: "1rem", | ||
borderRadius: "5px 5px 0 0", | ||
fontSize: "1rem", | ||
margin: "0", | ||
}} | ||
{...props} | ||
> | ||
{children} | ||
</SyntaxHighlighter> | ||
<div className={styles.copyBtnContainer}> | ||
<CopyToClipboard text={code} onCopy={handleCopy}> | ||
<button className={styles.copyButton}> | ||
{isCopied ? "Copied!" : "Copy"} | ||
</button> | ||
</CopyToClipboard> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
// =============================================================== // | ||
|
||
export const LiveMarkdownStyle: React.FC<LiveMarkdownStyleProps> = ({ | ||
text, | ||
setButtonsDisabled, | ||
}) => { | ||
const { stop, setStop } = useStopTextGeneration(); | ||
const [currentText, setCurrentText] = useState(""); | ||
const [currentIndex, setCurrentIndex] = useState(0); | ||
const delay = 10; | ||
|
||
// | ||
// Handle typing effect for the entire Markdown content | ||
useEffect(() => { | ||
if (currentIndex >= text.length - 5) { | ||
setButtonsDisabled ? setButtonsDisabled(false) : null; | ||
} | ||
if (stop) { | ||
setButtonsDisabled ? setButtonsDisabled(false) : null; | ||
setStop(false); | ||
} else if (currentIndex < text.length) { | ||
const timeout = setTimeout(() => { | ||
setCurrentText((prevText) => prevText + text[currentIndex]); | ||
setCurrentIndex((prevIndex) => prevIndex + 1); | ||
}, delay); | ||
return () => clearTimeout(timeout); | ||
} | ||
}, [currentIndex, delay]); | ||
|
||
return ( | ||
<div className={styles.markdownText}> | ||
<Markdown | ||
remarkPlugins={[remarkGfm]} | ||
components={{ | ||
code({ className, children, ...props }) { | ||
if (className?.includes("language-")) { | ||
return ( | ||
<SyntaxHighlight code={children?.toString()}> | ||
{children} | ||
</SyntaxHighlight> | ||
); | ||
} | ||
return ( | ||
<code {...props} className={styles.inlineCode}> | ||
{children} | ||
</code> | ||
); | ||
}, | ||
}} | ||
> | ||
{currentText} | ||
</Markdown> | ||
</div> | ||
); | ||
}; |
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