Skip to content

feat(chat): add Markdown export for conversations (#46) - #491

Merged
Ayinkx merged 1 commit into
AyinkxLab:mainfrom
psalmuel3:fix/conversation-markdown-export-46
Oct 7, 2026
Merged

Ayinkx merged 1 commit into
AyinkxLab:mainfrom
psalmuel3:fix/conversation-markdown-export-46

Conversation

@psalmuel3

Copy link
Copy Markdown
Contributor

What & why

Adds a Markdown export for conversations, requested in #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, Project snapshot export to archive #107) is untouched.

Closes #46

## 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
@psalmuel3

Copy link
Copy Markdown
Contributor Author

@Ayinkx Implemented the Markdown conversation export requested in #46.

  • New endpoint GET /chat/conversations/<id>/export.md renders the user and assistant messages in conversation order, preserves code blocks as fenced blocks (with language hints), returns Content-Type: text/markdown; charset=utf-8 and downloads as conversation-<id>.md.
  • Rendering lives in a new app/services/chat_export.py; the route stays thin and reuses the existing _get_conversation ownership check plus @login_required, so another user's conversation still 404s rather than leaking.
  • 9 focused tests in tests/test_chat_export_markdown.py: auth/ownership, ordering (including the created_at tie fallback), code-fence preservation, no double-escaping, and the empty-conversation case.

Verification: the new tests pass (9 passed) and the full suite is 1899 passed. The only failures are 16 pre-existing ones in tests/test_chat_markdown_sanitize.py (a Node asset issue in chat_markdown.js) which fail identically on an untouched main.

Could you take a look when you have a moment? Thanks!

@Ayinkx
Ayinkx merged commit 7250227 into AyinkxLab:main Oct 7, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Markdown export for conversations

2 participants