Skip to content
lit-docx
Developer docs
Preview · unreleased

The LitAST node model

Common node fields, node and mark types, and how paragraphs, lists, sections and document metadata are represented.

LitAST is a tree of plain JSON objects. You can save it with JSON.stringify and later hand the JSON.parse result to generateDocx.

Common fields

Any node may carry these fields:

FieldMeaning
typeNode type, required
childrenChild nodes
attrsNode attributes, such as paragraph alignment, table column widths or image size
marksOnly on text: formatting such as bold or links
textOnly on text: the characters
rawXmlOOXML kept verbatim; see Round trips and preservation
metaExtra data; the root's meta holds package-level data

parseDocx returns mutable LitNodes; generateDocx accepts read-only LitAstNodes. Both have the same structure.

Attributes starting with an underscore (such as _pPrPreservation) are internal data kept for writing the original file back. Ignore them when reading; keep them when editing so the original formatting is written back intact.

An example

A paragraph with bold text:

json

Node types

CategoryTypes
Structuredocument, section, header, footer
Blocksparagraph, heading, blockquote, codeBlock, horizontalRule, list, listItem, table, tableRow, tableCell
BreakspageBreak, columnBreak, lastRenderedPageBreak
Inlinetext, image, textBox, field, sdt (content control), footnoteRef, endnoteRef, math, ruby, watermark, backgroundImage
Range markerscommentStart / commentEnd, bookmarkStart / bookmarkEnd, permStart / permEnd, revision
UnrecognizedunknownBlock, unknownInline

Mark types

bold, italic, underline, strike, doubleStrike, superscript, subscript, code, link, highlight, textStyle, plus insertion / deletion for tracked changes.

Font size, color, font family and similar properties live in the attrs of a textStyle mark, for example:

json

Paragraphs and headings

Paragraph formatting lives in attrs, for example textAlign, spacing and indent. A heading stores its level (1–6) in attrs.level.

Lists

Lists are list > listItem, with list.attrs.ordered telling ordered from unordered. Each listItem is one numbered paragraph and holds inline nodes (text, field and so on) directly, without a wrapping paragraph. An item's depth is listItem.attrs.level, starting at 0. Items at different levels or with different numbering definitions are split into adjacent list nodes rather than nesting a list inside a listItem:

text

Pass numberingMode: "flat-numbered-paragraphs" when parsing to keep Word's flat numbered paragraphs instead.

Use the same shape when generating. Do not put block nodes such as paragraph or list inside a listItem; that produces invalid nested paragraphs.

Phonetic guides

ruby is an inline node: children holds the annotated base text and attrs.rubyText holds the annotation. Both are arrays of inline nodes with their own text formatting. The other attrs are Word's guide properties: rubyAlign, hps (annotation size), hpsRaise (raise), hpsBaseText (reference base size), lid (language) and dirty; sizes and distances are in half-points:

text

lit-docx never infers pronunciation; it reads and writes the annotation stored in the file. See Generating DOCX to create one.

Sections

A section is not a container around content but a sibling in document.children: it ends the section formed by the content before it and carries that section's page setup. Every Word document ends with at least one section:

json

The section's headers and footers are its header / footer children. For units, see Generating DOCX.

Document metadata

The root's meta holds package-level data that is not body content. Common keys:

KeyContents
rawXmlFilesPart XML kept verbatim, with keys such as stylesXml, numberingXml, settingsXml and fontTableXml
corePropertiesCore properties such as creator, last modifier, created and modified times, and revision
settingsDocument settings
fieldDataCore properties, custom properties and document variables that fields can reference
commentsComment contents
footnotes / endnotesFootnote and endnote contents

A key appears only when the document actually contains that data.

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