Add export chat as Markdown feature#370
Conversation
Adds a download button to the chat section toolbar and a command palette entry that exports the current chat conversation as a Markdown file. The export formats messages with sender names, timestamps, and proper Markdown structure. The file is downloaded directly to the user's machine. Changes: - Add `downloadChatAsMarkdown` and `formatChatAsMarkdown` utilities - Add export button to `ChatSection` toolbar (download icon) - Add `exportChat` command to the command palette - Export `downloadChatAsMarkdown` from `@jupyter/chat` public API Closes jupyterlab#328
afd50fe to
5b47122
Compare
|
Rebased on main, conflicts are resolved. Fixed the linked issue to #145 too, good catch. |
| document.body.appendChild(a); // Firefox requires DOM attachment | ||
| a.click(); | ||
| document.body.removeChild(a); | ||
| setTimeout(() => URL.revokeObjectURL(url), 100); // delayed to avoid premature revoke |
There was a problem hiding this comment.
| setTimeout(() => URL.revokeObjectURL(url), 100); // delayed to avoid premature revoke | |
| requestAnimationFrame(() => URL.revokeObjectURL(url)); |
It might be more robust to use the requestAnimationFrame to cleanup the URL.
There was a problem hiding this comment.
Good call — requestAnimationFrame is cleaner since it guarantees the download has started before we revoke. Will update.
| for (const msg of visibleMessages) { | ||
| const displayName = msg.sender.display_name || msg.sender.username || 'Unknown'; | ||
| const timestamp = new Date(msg.time * 1000).toLocaleString(); | ||
| const body = typeof msg.body === 'string' ? msg.body : '[Rich content]'; |
There was a problem hiding this comment.
Maybe we should wait for #386, which changes the API. The body will always be a string after that.
Instead, the message can contain mime_model, that replaces the body content with a rich content. This mime model should always be serializable, so maybe it could also be interesting to export it.
There was a problem hiding this comment.
| }); | ||
|
|
||
| // Command to export the current chat as a Markdown file. | ||
| commands.addCommand(CommandIDs.exportChat, { |
There was a problem hiding this comment.
This command could also be added in the toolbar of the main area widget https://github.com/jupyterlab/jupyter-chat/blob/main/packages/jupyterlab-chat-extension/schema/chat-panel.json
There was a problem hiding this comment.
Good idea — I'll add the export command to the chat panel toolbar schema so it's accessible from the main area widget too.
Summary
.mdfileexportChatcommand accessible from the command palettedownloadChatAsMarkdownandformatChatAsMarkdownutility functions in@jupyter/chatThe exported Markdown includes sender names, timestamps, and preserves message formatting. The file downloads directly to the user's machine via a temporary anchor element.
Changes
@jupyter/chatsrc/utils.tsformatChatAsMarkdownanddownloadChatAsMarkdownfunctions@jupyter/chatsrc/index.tsdownloadChatAsMarkdown@jupyter/chatsrc/widgets/multichat-panel.tsxChatSectiontoolbarjupyterlab-chatsrc/token.tsexportChatcommand IDjupyterlab-chat-extensionsrc/index.tsTest plan
.mdfile downloadsCloses #145