@stesura/docx
DOCX import and export, framework-free. The toolbar buttons are in
@stesura/docx-react; behaviour and round-trip limits are in the
DOCX guide.
Import
importDocx
Imports a DOCX as a ProseMirror document, with its list configs, images and
warnings for anything the editor can’t represent. Throws DocxImportError
when the file isn’t a readable DOCX (wrong format, too large, corrupt).
| Name | Type | Default |
|---|---|---|
file | File | ArrayBuffer | |
schema | Schema<any, any> | |
options | DocxImportOptions |
DocxImportOptions
Options for importDocx.
DocxImportResult
Result of importDocx.
DocxImportStats
What the importer saw and what it consumed, for measuring loss across a corpus.
DocxImportError
Thrown by importDocx when the file isn’t a readable DOCX: wrong format, corrupt
archive, or over the size limits (50 MB, 200 MB decompressed, 5,000 entries). Its
message is written for end users.
ImportWarning
Something the importer could not represent faithfully.
| Name | Type | Default |
|---|---|---|
type | ImportWarningType | |
message | string | |
element | string | |
position | number |
ImportWarningType
export type ImportWarningType =
| "unsupported_element"
| "missing_image"
| "image_upload_failed"
/** Pictures dropped because the schema has no image node. */
| "images_excluded"
| "style_fallback"
| "numbering_mismatch"
| "parse_error";ImportedThread
A comment thread from the file. The doc’s comment anchors carry its id; create the
thread in your comments store and remap the anchors before mounting (see
Loading the result into an editor).
A comment thread: its root comment first, then the replies in file order.
ImportedComment
One comment of an imported thread.
ImportedImage
An image extracted from the DOCX.
NumberingConfig
NumberingLevel.pStyle carries Word’s level→style back-link for
style-linked numbering.
A list definition imported from numbering.xml.
NumberingLevel
One level of a numbering definition.
Export
downloadDocx
generateDocxBuffer, then saves the file in the browser
(outputFileName defaults to example.docx).
| Name | Type | Default |
|---|---|---|
state | EditorState | |
comments | CommentsDocx[] | |
outputFileName | string | |
options | DocxExportOptions |
generateDocxBuffer
Serializes an editor state to final .docx bytes, plus warnings for content that was skipped or degraded. Throws rather than produce a file Word would refuse to open.
| Name | Type | Default |
|---|---|---|
state | EditorState | |
comments | CommentsDocx[] | |
options | DocxExportOptions |
generateDocx
Returns the unpacked docx-js Document. It skips the post-pack stage: packing it
yourself loses comment threads, endnote options, section direction, style→level links
and validation. Prefer downloadDocx / generateDocxBuffer
(details).
Serializes an editor state to a docx-js Document, without packing or
post-processing it. comments come from threadsToDocx; options carries
the handlers and preloaded bytes of nodes outside the core schema.
| Name | Type | Default |
|---|---|---|
sourceState | EditorState | |
comments | CommentsDocx[] | |
options | DocxExportOptions |
GeneratedDocx
A serialized document, plus everything the serializer could not represent.
threadsToDocx
Flattens threads into the comment list the docx export takes. Soft-deleted
comments are dropped before isReply is computed, so a deleted root’s first
reply becomes the root.
| Name | Type | Default |
|---|---|---|
threads | Thread[] | |
schema | Schema<any, any> | |
resolveUserName | ResolveUserNameDisplay name for a mentioned user id. Optional: authors named in the same threads are the fallback, then the raw id. |
CommentsDocx
A comment as generateDocx writes it: one per comment, replies flagged with isReply.
DocxExportOptions
Export support for nodes outside the core schema. A package exports one of
these (e.g. mermaidDocx); a host combining several spreads them together.
NodeSerializer
Renders nodes outside the core schema, passed as DocxExportOptions.nodes. Each handler
gets the DocxSerializerState, which renders child content and collects the export’s
blocks; mermaidDocx from @stesura/mermaid is one.
export type NodeSerializer = Record<
string,
(state: DocxSerializerState, node: Node, parent: Node, index: number) => void
>;Per-node-type render functions, keyed by schema node name.
DocxExportError
Thrown by downloadDocx / generateDocxBuffer when a stage throws for a reason other than
validation. stage and at say where, cause keeps the original error.
An export that threw outside the validator: which stage, and (for the
serializer) the node it was on. cause keeps the original stack.
DocxValidationError passes through unwrapped.
| Name | Type | Default |
|---|---|---|
stage | ExportStage | |
at | ExportLocation | undefined | |
name | string | |
message | string | |
stack | string | |
cause | unknown |
ExportStage
export type ExportStage = "serialize" | "pack" | "post-pack";The export stage a DocxExportError failed in.
ExportLocation
Where in the document an issue or a failure happened.
ExportIssue
Content an export skipped or degraded, with the node it came from.
| Name | Type | Default |
|---|---|---|
code | ExportIssueCode | |
message | string | |
at | ExportLocation |
ExportIssueCode
export type ExportIssueCode =
| "format-change-partial"
| "comment-unmatched-close"
| "comment-unclosed"
| "comment-on-block"
| "header-footer-failed"
| "note-body-no-ref"
| "note-anchor-no-ref"
| "note-anchor-orphan"
| "note-body-orphan"
| "table-cell-failed"
| "numbering-incomplete"
| "numbering-unknown-list"
| "image-failed"
| "image-missing"
| "image-passthrough-dropped"
| "file-omitted"
| "code-block-failed"
| "node-unsupported"
/** A text box's minimum height: Word's boxes are fixed or fit their text. */
| "text-box-min-height"
/** A block a text box can't hold in Word's model (a callout) went out as its paragraphs. */
| "text-box-content-flattened"
| "mark-unsupported"
| "link-internal-dropped"
| "link-unsafe-dropped"
| "link-invalid-dropped";What an export skipped or degraded, one code per kind so a corpus run can count them.
createExportTrace
An empty DocxExportTrace, to pass as options.trace.
DocxExportTrace
Filled by generateDocxBuffer when passed as options.trace: where an
export’s time and bytes go. Durations in ms.
DocxValidationError
Thrown by downloadDocx / generateDocxBuffer when the packed file breaks a rule Word
enforces, instead of shipping a file Word would reject or repair. problems lists every
violation as a DocxProblem. This points to a serializer bug, not bad input.
DocxProblem
One rule violation found by assertValidDocx.