Skip to content
lit-docx
Developer docs
Preview · unreleased
Getting startedView Markdown ↗

Quick start

Read DOCX into a traversable LitAST, then generate DOCX from LitAST, in browsers and Node alike.

@quirekit/lit-docx does two things: parseDocx reads a DOCX file into LitAST, a document tree made of plain JSON objects, and generateDocx writes LitAST back to DOCX. It does not depend on any editor and never touches the file system; it only works with bytes, so the same code runs in browsers and Node. It is a QuireKit commercial component distributed as a compiled build: development and evaluation are free, the free edition runs in production with an evaluation notice, and a license key removes the notice. Individuals, education, open-source projects and small teams can request a free community key.

Installation

lit-docx is a preview and has not been published to npm yet. Once published, install it with:

bash

It requires Node 18 or later, or a modern browser. The package ships both ESM and CommonJS builds.

Try it in the browser

The example below runs entirely in this page. It loads a sample report generated by lit-docx; switch to “Your file” to open your own .docx. Inspect the result as a rendered preview or as LitAST, then generate a new .docx from the LitAST.

Preparing the example…

Read a DOCX

parseDocx takes the bytes of a complete file (Uint8Array) and returns the document tree ast together with non-fatal warnings. A file that is not DOCX throws DocxPackageError.

Start with two steps: obtain bytes, then parse them. Here file is a browser File selected by the user:

Read a browser filets

ast is the tree to read or modify; warnings describes parsing issues to explain to the user. In Node, obtain bytes with readFile() and use the same parser.

For text extraction, inspect recipes/read-text.ts for reading and parsing, and shared/text.ts for tree traversal.

recipes/read-text.tsts

The collectParagraphText helper used above just walks the tree: the words of paragraphs and headings live in their text children.

Generate a DOCX

LitAST is plain objects. You can write it by hand or with the package's docx authoring helpers; both produce exactly the same structure. The report in the example above is built this way: a title, formatted text, a bullet list, a bordered table with a shaded header row, a link, and an A4 page with a header and footer. shared/report.ts builds that report; recipes/create-report.ts generates bytes and writes them. First, generate just one paragraph:

Generate one paragraphts

Write bytes to a Node file or download them in a browser. Inspect the complete report by file below:

recipes/create-report.tsts

Next steps

lit-docx · QuireKitA DOCX parser and generator, licensed under AGPL-3.0 with a commercial option.