Skip to content

feat(chat): export an answer as Markdown, Word, PDF, CSV or Excel - #1813

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-w1-24-export
Oct 30, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-w1-24-export

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Chat Wave 1, W1-24: export a Chat answer as Markdown, Word, PDF, CSV or Excel (CHAT-06). It is a library, with no UI yet; W1-25 adds Copy / Download / Save to Files.

No new dependencies. The repo already has a library for each format:

  • Excel and zip: write-excel-file/node and fflate, as in the traffic export.
  • Word: docx, 9.7.0 on main.
  • PDF: @react-pdf/renderer, as in Reports.

So exceljs and jszip weren't added, and no lockfile changes.

What it includes

  • lib/chat/export.ts (pure):
    • Turns Markdown into the Content Editor's block model.
    • Names each table after the heading above it.
    • Decides which formats to offer (CSV and Excel only when there is a table).
    • Names files question-slug-YYYY-MM-DD.
    • Makes in-app links absolute and drops unsafe ones.
  • lib/chat/export-file.ts (server):
    • MD.
    • DOCX: through the Content Editor's own docxBlocks.
    • CSV: BOM, and formula-like cells guarded. Several tables become one CSV each, zipped.
    • XLSX: a sheet per table; plain numbers become number cells.
  • lib/chat/export-pdf.tsx: Inter embedded so Turkish letters print, clickable links, page numbers.
  • The "Generated … · Data as of …" stamp sits where the spec puts it:
    • MD: first line.
    • Word and PDF: header and footer.
    • CSV and each Excel sheet: first row.
    • The caller passes its text from en.json; the library has no user-facing strings.
  • draft-export.ts: two functions are exported and Word gets an optional header/footer line. The Content Editor's own export is unchanged.

Related issue

Part of Chat (CHAT-06, Wave 1).

Type of change

  • feat — New feature

How to test

cd web && yarn vitest run src/lib/chat/export (4 files, 48 tests). They cover each format's content, tables, headings and Turkish characters.

For the route (W1-25): on Vercel, public/fonts must be included in the function (outputFileTracingIncludes), and @react-pdf/renderer may need serverExternalPackages.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Web: lint, typecheck and the related tests pass
  • Tests added
  • No new dependencies

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits October 30, 2026 01:19
CHAT-06 items 30–42, the library W1-25's Download menu will call: an
answer rendered from its stored content_md on the server, with no model
call, nothing stored and no new dependency.

- lib/chat/export.ts reads the answer: Markdown to the Content Editor's
  export blocks (headings, paragraphs with bold, italics and links, lists,
  tables, quotes, code), its tables named by the heading above them, the
  formats Download offers (CSV and Excel only with a table), and the file
  name: the question's slug, or the chat title's past 60 characters, plus
  the day it was generated in its time zone. App links are made whole with
  the app's origin; javascript: and other links are dropped.
- lib/chat/export-file.ts writes the files: Markdown as UTF-8; Word with
  draft-export's docxBlocks; CSV (byte order mark, formula-safe cells),
  and a .zip of one CSV per table when there are several (fflate); Excel
  with one sheet per table and plain numbers as numbers
  (write-excel-file/node, as the traffic export).
- lib/chat/export-pdf.tsx writes the PDF with @react-pdf/renderer on the
  server, Inter embedded from public/fonts so Turkish letters print, links
  clickable, tables as tables, page numbers.
- Every file carries the answer's "Generated … · Data as of …" line
  (item 40): first line of Markdown, Word's and PDF's header and footer,
  the first row of a CSV and of each sheet. The caller words it from
  en.json, as the app shows it under the answer.
- draft-export: docxBlocks and docxDocument are exported, and
  docxDocument takes an optional header and footer line; the Content
  Editor's exports don't change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With the line height set on the page, react-pdf's page number threw
"unsupported number" once the PDF passed ten pages, so any long answer
failed to export. The line height now sits on the body text.

Claude-Session: https://claude.ai/code/session_0121amRjX7UFrgmGELAtkc7A

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good with 2f30e44 (one commit on this branch). Any PDF past ten pages (about 120 paragraphs, or a table of about 190 rows) failed in react-pdf with unsupported number: -8.55e+21, and no file was written. The cause is lineHeight: 1.45 on the page together with the footer's render page number. The commit moves the line height onto the body text: headings, paragraphs, quote lines, list items and table cells. A 62-page answer and a 2,000-row table now render. The new test in export-file.test.ts (a 150-paragraph PDF has more than 10 pages) fails without it.

Checked, fine

  • CSV: cells starting with = + - @, tab or CR get a leading ' through csvLine, including the stamp row and the zipped CSVs.
  • Excel: every non-number cell is written as a plain string, never a formula.
  • Word and PDF: javascript:, data: and //host links are dropped, inline HTML is reduced to text, and both libraries escape the rest.
  • Filenames and zip entries are slugified. No new dependencies, and the heavy libraries load lazily.
  • The Content Editor's Word export is unchanged when no note is passed.

For the route PR (W1-25): load the answer through the caller's own chat and organization, and add a maxDuration or a row cap for PDF. A 2,000-row PDF took 45–70 s here.

Nits: a raw control character in an answer ends up in word/document.xml, and Word then refuses the file. The shared CSV helper turns -12 into '-12, the same as the app's other CSV exports.

Tests: src/lib/chat, src/lib/content and src/lib/guards, including plan-row.test.ts: 89 files, 1,192 tests pass. tsc, eslint and prettier are clean. It merges cleanly on main and with the open Chat PRs. No migrations.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging in order with the other approved PRs. This head merges current main; its tree is what main will be after this squash, checked with the full server and web suites, tsc, lint, format and next build.

@gkhngyk
gkhngyk merged commit f6fd5d6 into ansvisor:main Oct 30, 2026
4 of 5 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.

2 participants