Repository navigation
feat(chat): export an answer as Markdown, Word, PDF, CSV or Excel - #1813
Conversation
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
left a comment
There was a problem hiding this comment.
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'throughcsvLine, 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//hostlinks 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
noteis 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
left a comment
There was a problem hiding this comment.
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.
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:
write-excel-file/nodeandfflate, as in the traffic export.docx, 9.7.0 on main.@react-pdf/renderer, as in Reports.So
exceljsandjszipweren't added, and no lockfile changes.What it includes
lib/chat/export.ts(pure):question-slug-YYYY-MM-DD.lib/chat/export-file.ts(server):docxBlocks.lib/chat/export-pdf.tsx: Inter embedded so Turkish letters print, clickable links, page numbers.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 featureHow 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/fontsmust be included in the function (outputFileTracingIncludes), and@react-pdf/renderermay needserverExternalPackages.Checklist
🤖 Generated with Claude Code