Skip to main content
The primitives are the low-level OOXML building blocks used internally by the MCP tools and the comparison engine. They are exported from @usejunior/docx-core for direct use when you need fine-grained control over document structure.
Most users work with the MCP tools or compareDocuments rather than primitives directly. Use primitives when building custom tooling on top of the library.

DocxDocument and DocxZip

The two primary container classes.

DocxZip

Low-level DOCX zip container. Reads and writes named XML parts within the .docx package.
Key methods: Additional standalone helpers:

DocxDocument

High-level document object providing typed access to paragraphs, text, comments, footnotes, and layout. Used internally by the MCP session layer.

Text operations

Exported from primitives/text.ts.
replaceParagraphTextRange accepts either a plain string or a ReplacementPart[] array for multi-segment replacements with per-segment formatting overrides.

Run normalization

Exported from primitives/merge_runs.ts.
Merges adjacent w:r elements with identical w:rPr formatting. Takes a W3C DOM Document. Run fragmentation is common in documents edited in Word and can cause text-search mismatches.

Comment operations

Exported from primitives/comments.ts.
The Comment type returned by getComments:

Footnote operations

Exported from primitives/footnotes.ts.

Layout operations

Exported from primitives/layout.ts.
All layout mutations accept twip (twentieth of a point) values consistent with OOXML.

Revision operations

Accept changes

Accepts all tracked changes in the document body. Takes a W3C DOM Document (parsed with parseXml) and mutates it in place.

Reject changes

Rejects all tracked changes, restoring the document to its pre-change state. Takes a W3C DOM Document.

Extract revisions

Returns structured per-paragraph revision records with before_text, after_text, revision entries (type, text, author), and associated comments. RevisionType values: 'INSERTION' | 'DELETION' | 'MOVE_FROM' | 'MOVE_TO' | 'FORMAT_CHANGE'

Utilities

XML helpers (xml.ts)

Parse an XML string into a W3C Document and serialize it back. These are thin wrappers around @xmldom/xmldom.

DOM helpers (dom-helpers.ts)

Typed helpers for navigating OOXML DOMs without getElementsByTagName traversal overhead.

Namespaces (namespaces.ts)

Centralized namespace and local-name constants used throughout the library.