feat: add export to .md button

This commit is contained in:
Yifei Zhang
2023-03-15 17:24:03 +00:00
parent 64e331a3e3
commit bab470d000
11 changed files with 181 additions and 18 deletions

View File

@@ -23,9 +23,13 @@ import DeleteIcon from "../icons/delete.svg";
import LoadingIcon from "../icons/three-dots.svg";
import MenuIcon from "../icons/menu.svg";
import CloseIcon from "../icons/close.svg";
import CopyIcon from "../icons/copy.svg";
import DownloadIcon from "../icons/download.svg";
import { Message, SubmitKey, useChatStore, Theme } from "../store";
import { Settings } from "./settings";
import { showModal } from "./ui-lib";
import { copyToClipboard, downloadAs } from "../utils";
export function Markdown(props: { content: string }) {
return (
@@ -208,7 +212,10 @@ export function Chat(props: { showSideBar?: () => void }) {
<IconButton
icon={<ExportIcon />}
bordered
title="导出聊天记录为 Markdown开发中"
title="导出聊天记录"
onClick={() => {
exportMessages(session.messages, session.topic)
}}
/>
</div>
</div>
@@ -294,6 +301,22 @@ function useSwitchTheme() {
}, [config.theme]);
}
function exportMessages(messages: Message[], topic: string) {
const mdText = `# ${topic}\n\n` + messages.map(m => {
return m.role === 'user' ? `## ${m.content}` : m.content.trim()
}).join('\n\n')
const filename = `${topic}.md`
showModal({
title: "导出聊天记录为 Markdown", children: <div className="markdown-body">
<pre className={styles['export-content']}>{mdText}</pre>
</div>, actions: [
<IconButton icon={<CopyIcon />} bordered text="全部复制" onClick={() => copyToClipboard(mdText)} />,
<IconButton icon={<DownloadIcon />} bordered text="下载文件" onClick={() => downloadAs(mdText, filename)} />
]
})
}
export function Home() {
const [createNewSession] = useChatStore((state) => [state.newSession]);
const loading = !useChatStore?.persist?.hasHydrated();