Repository navigation
feat(chat): add Markdown export for conversations (#46) - #491
Merged
Ayinkx merged 1 commit intoOct 7, 2026
Merged
Conversation
## What & why Adds a Markdown export for conversations, requested in AyinkxLab#46: > `GET /chat/conversations/<id>/export.md` returns the conversation as a Markdown document with the user's messages and assistant replies in order. This is the `.md` sibling of the existing JSON export at `GET /chat/conversations/<id>/export`. It follows the same auth/ownership pattern (`@login_required` + `_get_conversation`, which 404s for another user's conversation rather than leaking a 403), and keeps the route thin by delegating rendering to a focused service. ### Changes - **`app/services/chat_export.py`** (new) — `render_conversation_markdown(conversation)` and `ordered_messages(conversation)`. Emits a `# <title>` document with metadata and one `## <User|Assistant|System>` heading per message, in strict conversation order (`created_at`, with `id` breaking timestamp ties so the order is deterministic even for messages written in the same instant). Message bodies are forwarded **verbatim** — the stored text is already the Markdown the client renders, so re-escaping it here would double-escape entities and corrupt code fences. Fenced code blocks therefore survive unchanged, including their language info strings (e.g. ```` ```python ````). An empty conversation still produces valid Markdown (`*This conversation has no messages.*`). - **`app/chat/routes.py`** — new `export_conversation_markdown` route `GET /chat/conversations/<int:conversation_id>/export.md`, returning `Content-Type: text/markdown; charset=utf-8` and `Content-Disposition: attachment; filename="conversation-<id>.md"`. - **`tests/test_chat_export_markdown.py`** (new) — focused tests. ### Acceptance criteria coverage | Criterion | Test | | --- | --- | | `.md` route returns user + assistant messages in order | `test_messages_are_ordered_with_role_headings`, `test_order_falls_back_to_id_on_tied_timestamps` | | Code blocks preserved as fenced blocks | `test_code_blocks_are_preserved_as_fences` | | Sensible filename + correct `Content-Type` | `test_content_type_is_markdown_utf8`, `test_download_filename_is_sensible` | | Tests verify structure and ordering | the `TestMarkdownExportStructure` class | | Auth: unauthenticated rejected / other user's conversation 404 | `TestMarkdownExportAuth` | | Empty conversation | `test_empty_conversation_is_still_valid_markdown` | ### Testing ``` ../venv/bin/python -m pytest ``` Result: **1899 passed, 16 failed** (0 skipped) in 724.75s. The 16 failures are all in `tests/test_chat_markdown_sanitize.py`, which drives the pre-existing `app/static/js/chat_markdown.js` renderer under Node; they fail because that module loads no exports under this environment's Node (`require(...)` returns `{}`, so `renderMarkdown` is not a function). They are unrelated to this change (no JS was touched) and reproduce independently of it. The new file alone: `../venv/bin/python -m pytest tests/test_chat_export_markdown.py` → 9 passed. Baseline trio (`tests/test_providers.py tests/test_llm_retry.py tests/test_chat.py`) → 80 passed. ### Notes - `ruff` and `black` are not installed in the project venv (`No module named ruff` / `No module named black`), so those checks were not run — nothing new was installed. - Only the three files above are touched; `app/services/exporting.py` (project ZIP export, AyinkxLab#107) is untouched. Closes AyinkxLab#46
Contributor
Author
|
@Ayinkx Implemented the Markdown conversation export requested in #46.
Verification: the new tests pass (9 passed) and the full suite is 1899 passed. The only failures are 16 pre-existing ones in Could you take a look when you have a moment? Thanks! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
Adds a Markdown export for conversations, requested in #46:
This is the
.mdsibling of the existing JSON export atGET /chat/conversations/<id>/export. It follows the same auth/ownershippattern (
@login_required+_get_conversation, which 404s for another user'sconversation rather than leaking a 403), and keeps the route thin by delegating
rendering to a focused service.
Changes
app/services/chat_export.py(new) —render_conversation_markdown(conversation)and
ordered_messages(conversation). Emits a# <title>document withmetadata and one
## <User|Assistant|System>heading per message, in strictconversation order (
created_at, withidbreaking timestamp ties so theorder is deterministic even for messages written in the same instant).
Message bodies are forwarded verbatim — the stored text is already the
Markdown the client renders, so re-escaping it here would double-escape
entities and corrupt code fences. Fenced code blocks therefore survive
unchanged, including their language info strings (e.g.
```python).An empty conversation still produces valid Markdown (
*This conversation has no messages.*).app/chat/routes.py— newexport_conversation_markdownrouteGET /chat/conversations/<int:conversation_id>/export.md, returningContent-Type: text/markdown; charset=utf-8andContent-Disposition: attachment; filename="conversation-<id>.md".tests/test_chat_export_markdown.py(new) — focused tests.Acceptance criteria coverage
.mdroute returns user + assistant messages in ordertest_messages_are_ordered_with_role_headings,test_order_falls_back_to_id_on_tied_timestampstest_code_blocks_are_preserved_as_fencesContent-Typetest_content_type_is_markdown_utf8,test_download_filename_is_sensibleTestMarkdownExportStructureclassTestMarkdownExportAuthtest_empty_conversation_is_still_valid_markdownTesting
Result: 1899 passed, 16 failed (0 skipped) in 724.75s. The 16 failures are
all in
tests/test_chat_markdown_sanitize.py, which drives the pre-existingapp/static/js/chat_markdown.jsrenderer under Node; they fail because thatmodule loads no exports under this environment's Node
(
require(...)returns{}, sorenderMarkdownis not a function). They areunrelated to this change (no JS was touched) and reproduce independently of it.
The new file alone:
../venv/bin/python -m pytest tests/test_chat_export_markdown.py→ 9 passed.Baseline trio (
tests/test_providers.py tests/test_llm_retry.py tests/test_chat.py) → 80 passed.Notes
ruffandblackare not installed in the project venv(
No module named ruff/No module named black), so those checks were notrun — nothing new was installed.
app/services/exporting.py(projectZIP export, Project snapshot export to archive #107) is untouched.
Closes #46