add a new function: export messages as markdown'

This commit is contained in:
littlemoonstones 2023-03-08 11:12:06 +08:00
parent 6c71422bdc
commit 897d2a90be
2 changed files with 41 additions and 0 deletions

28
src/lib/Export.svelte Normal file
View File

@ -0,0 +1,28 @@
<script context="module" lang="ts">
import { get } from "svelte/store";
import { chatsStorage } from "./Storage.svelte";
export const exportAsMarkdown = (chatId: number) => {
const chats = get(chatsStorage);
const chat = chats.find((chat) => chat.id === chatId);
const messages = chat.messages;
console.log(chat);
let markdownContent = `# ${chat.name}\n`;
messages.forEach((message) => {
const author = message.role;
const content = message.content;
const messageMarkdown = `## ${author}\n${content}\n\n`;
markdownContent += messageMarkdown;
});
const blob = new Blob([markdownContent], { type: "text/markdown" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.download = `${chat.name}.md`;
a.href = url;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
</script>

View File

@ -1,5 +1,6 @@
<script lang="ts">
import { addChat, clearChats } from "./Storage.svelte";
import { exportAsMarkdown } from "./Export.svelte";
import type { Chat } from "./Types.svelte";
export let activeChatId: number;
@ -64,5 +65,17 @@
}}><span class="greyscale mr-2">🗑️</span> Clear chats</a
>
</li>
{#if activeChatId }
<li>
<a
href={"#"}
class="panel-block"
class:is-disabled={!apiKey}
on:click|preventDefault={() => {
exportAsMarkdown(activeChatId);
}}><span class="greyscale mr-2">📥</span> Export chat</a
>
</li>
{/if}
</ul>
</aside>