Download src/lib/markdownDownload.ts from IOE-IFAD/test: direct link, hf CLI and curl.
- Browser
- Download file 1.41 kB
-
https://huggingface.co/spaces/IOE-IFAD/test/resolve/main/src/lib/markdownDownload.ts
- Command line
-
hf download hf://spaces/IOE-IFAD/test/src/lib/markdownDownload.ts
-
curl -L -o markdownDownload.ts https://huggingface.co/spaces/IOE-IFAD/test/resolve/main/src/lib/markdownDownload.ts
1.41 kB
| import { convertMarkdownToDocx, downloadDocx } from "@mohtasham/md-to-docx"; | |
| import type { Chat } from "./types"; | |
| export async function downloadChatAsMarkdown(chat: Chat): Promise<void> { | |
| try { | |
| const markdown = generateMarkdownFromChat(chat); | |
| const blob = await convertMarkdownToDocx(markdown); | |
| const filename = `${chat.title || "Chat"}_${ | |
| new Date().toISOString().split("T")[0] | |
| }.docx`; | |
| downloadDocx(blob, filename); | |
| } catch (error) { | |
| console.error("Failed to download chat as markdown:", error); | |
| throw error; | |
| } | |
| } | |
| function generateMarkdownFromChat(chat: Chat): string { | |
| let markdown = `# ${chat.title || "Chat Export"}\n\n`; | |
| markdown += `**Date:** ${new Date(chat.date).toLocaleDateString()}\n\n`; | |
| markdown += `[TOC]\n\n`; | |
| // Filter out system messages and process user/assistant pairs | |
| const nonSystemMessages = chat.messages.filter( | |
| (msg) => msg.role !== "system" | |
| ); | |
| for (let i = 0; i < nonSystemMessages.length; i++) { | |
| const message = nonSystemMessages[i]; | |
| if (message.role === "user") { | |
| markdown += ` ## ${message.content}\n\n`; | |
| } else if (message.role === "assistant") { | |
| markdown += `${message.content}\n\n`; | |
| // Add page break after each question-answer pair, except for the last one | |
| if (i < nonSystemMessages.length - 1) { | |
| markdown += `\\pagebreak\n\n`; | |
| } | |
| } | |
| } | |
| return markdown; | |
| } | |