.docx(@awacloud/ooxml'sdocx.read()result) →oconv-ir/v2, tier 2.
Module oconvDocxToIr (oconvDocxToIr) | Source packages/front/office/oconv/src/read/docx-to-ir.js | Deps oconvIr, docx | Worker-safe yes
Pure transformation over an already-parsed docx.read(bytes) structure — no bytes, no I/O of its own. Composes only docx's public API, never an @awacloud/ooxml internal.
Resolve
import { ModuleRuntime } from '@awacloud/fw/core/runtime.js';
import { fw_require, modules } from '@awacloud/oconv';
const runtime = new ModuleRuntime();
runtime.registerAll(fw_require);
runtime.registerAll(modules);
const docx = runtime.resolve('docx');
const { docxToIr } = runtime.resolve('oconvDocxToIr');
API
| Member | Signature | Returns | Throws |
|---|---|---|---|
docxToIr |
(readResult: object, opts?: {include?: string[]}) => {ir, losses} |
ir — an oconv-ir/v2 document; losses — {code, detail}[] in document order |
oconv: bad docx include — opts.include not an array of distinct known names |
opts.include — the opt-in parts brought back, as the odt reader's: 'toc' reads a table of contents control's entries as paragraphs (below); 'layout' reads tier 3 — the page, the paragraphs' alignment and breaks, the runs' size, font, colour, underline and script, the headers and footers, and the page fields (below). Absent or []: none.
Examples
Convert a .docx read result to IR
const { ir, losses } = docxToIr(docx.read(bytes));
ir.kind; // 'document'
losses; // [] for a plain document with no tracked features
Notes
-
Headings are resolved from
pPr.pStylein three steps:- a style ID matching
Heading1..Heading6(case-insensitive) gives that level; - otherwise the style's built-in
w:nameis looked up in the document'sstyles.xml(readResult.styles). Built-in names stay language-invariant when Word localizes the ID — a FrenchTitre1is still namedheading 1— soheading 1..heading 6give that level; - the built-in name
Titlebecomes a level-1 heading;Subtitlebecomes a plain paragraph and recordsheading/subtitle-degraded(detail = the style ID, one loss per subtitle paragraph, even an empty one).
A style ID that matches no rule and whose name resolves to nothing stays a paragraph with no loss — the reader cannot know it was meant as a heading. Limit: a document without a
styles.xmlpart has no names to resolve, so only step 1 applies and a localizedTitre1reads as a plain paragraph.basedOnchains, numbered heading styles, character styles andw:aliasesare not consulted. A resolved heading style takes precedence overpPr.numPr. - a style ID matching
-
Code blocks, quotes and rules are read from the paragraph style too (a paragraph without
pPr.numPr), by its built-in name — or, whenstyles.xmldoes not define the ID, by the ID split at its case changes (SourceCode→source code), so pandoc's andoconvIrToDocx's IDs resolve without a styles part:Source Code→ acodeBlock(info'') whose text is the paragraph's text, a line break giving\nand a tab\t. One paragraph is one code block: adjacent code paragraphs are not merged, and an empty one is skipped like any spacer;Block Text,Quote,Intense Quote→ a quoted paragraph; CONSECUTIVE quoted paragraphs form ONEblockquote(an empty one is skipped without closing the quote). Quote nesting cannot be recovered — two quotes with nothing between them read as one;Horizontal Rule→ anhrwhen the paragraph is empty; with text it stays a paragraph.
-
Monospace runs (
run.code) are detected by a frozen, name-based allowlist ofrPr.fontvalues (consolas,courier new, …,src/read/docx-to-ir.js'sMONO_FONTS); a font outside the list yields a plain run with no per-node loss. -
Lists: the consecutive list paragraphs of one
numIdform one list tree byilvl: the lowestilvlis the root level, a deeper item opens a nested list in the last item above (an item holding only the nested list when a level is skipped), a shallower one goes back to its level's list, and each list is ordered by its own level'snumFmt; a paragraph of anothernumIddeeper than the group's first item nests in it (oconv's docx writer gives each list kind its ownnumId, and Word lays it out indented under), while a change ofnumIdat the first level starts a new list; anumIdthat does not resolve to a concrete<w:num>gives unordered lists (losslist/numbering-unresolved, once pernumIdof a group). Nesting is kept, never flattened. -
Tables: no row is ever marked
header—docx.read()surfaces no first-class header-row signal. A table without a row holds nothing and is skipped without a loss, as an empty paragraph is. A cell inside a content control is an ordinary cell of its row (docx.read()reads it in place), so its text is read. -
Content controls: a block-level content control (
blockSdt, in the body or a cell) is read in place — its paragraphs, tables and nested controls walked as the blocks around it are, an open list or quote left open across its bounds; its properties are not content. A table of contents control (w:docPartObjgalleryTable of Contents) is dropped withblock/dropped, detailtoc— the odt reader's default fortext:table-of-content; underopts.include: ['toc']it is read in place as one plain paragraph per paragraph (nested controls too) of the text the document shows (docx.toText: field instructions and tracked deletions left out, a tab a space), whitespace collapsed and trimmed, an empty one skipped, no loss — its links (to heading bookmarks the IR does not carry) are not kept. -
Tracked changes and range markup: the accepted view is read. An insertion and a move destination (
ins,moveTo) are read as their content, a hyperlink inside one keeping its link; a deletion and a move source (del,moveFrom) are dropped without a loss. Between blocks, bookmarks, comment ranges and revision range markers (bookmarkStart,bookmarkEnd,commentRangeStart,commentRangeEnd,rangeMarker) are skipped without ablock/droppedloss and without closing an open list or quote. -
Task markers: a list item whose first run, a plain text run, leads with
☐or☒(a glyph followed by one space; the GFM task marker asoconvIrToDocxdraws it) reads aslistItem.escapes.md.task'unchecked'/'checked', the marker stripped, so md → docx → md keeps[ ]/[x]. The glyph with no space, after other text, outside a list, or☑stays text. -
Images: each
w:drawingis an IRimagenamed after itswp:docPrname (else itspic:cNvPrname, elseimage), its alt text the drawing's description, its bytes inescapes.docxwhen the picture part resolves (a chart, a shape or a SmartArt has none:image/bytes-unavailable). A document may repeat a drawing name, an asset may not: the first image of a name keeps it, a later one takes the first free<name> (k), k from 2 (the odt writer's rule for frame names), so two distinct pictures never share one asset intoMd. -
Loss codes this module emits:
heading/subtitle-degraded(a paragraph styledSubtitlekept as a plain paragraph, detail = its style ID),link/target-missing(an unresolved hyperlinkrId, text kept),list/numbering-unresolved,image/bytes-unavailable(a drawing's image bytes did not resolve on read),block/dropped(an unmapped body/cell node, detail = itstype). -
Headers and footers (under
include: ['layout'], T80):document.headeranddocument.footerfrom the body's lastsectPr—defaultitsdefaultreference's part;firstnullwithouttitlePg, else itsfirstpart ([]with none);evennullwithout the settings'evenAndOddHeaders, else itsevenpart ([]with none). Each part's body is read as the body is; a page break does not cross into it or out of it (layout/break-dropped, detailheaderorfooter). A region none of whose slots holds a block isnull; a reference whose part did not read recordslayout/region-unresolved(detail<region> <slot>). A header's hyperlink takes the target of its own part's relationship. -
Page fields (under
include: ['layout'], T80): a simple or complex field whose instruction starts withPAGEis afieldpage,NUMPAGESorSECTIONPAGESafieldpages, itstextthe result the document shows. Another field keeps its result as runs; in a header or footer it recordsfield/static(detail its name, once per name). -
Footnotes and endnotes (under
include: ['layout'], T81): afootnoteReferenceorendnoteReferenceis anoteat its place in the runs, its children the note's body read as the body is; withcustomMarkFollowsthe run's next text is itsmark. Withoutlayouta reference is dropped, and its custom mark with it. A reference whose note did not read recordsnote/unresolved(detail<class> <id>). -
Comments and revision history (who changed what, when) are out of scope and never reported per node; without
include: ['layout'], so are the page, sections, headers and footers, notes and font colours — see the loss matrix for the full docx→md row.
See also
- docs/api/README.md — full module index +
exportsboundary note oconvIr— the pivot this reader producesoconv— the facade composing this reader intotoMd/convert- loss matrix