oconv-ir/v2→ structured-markdown profile v1, the frozen wire contract.
Module oconvIrToMd | Source packages/front/office/oconv/src/write/ir-to-md.js | Deps oconvIr, md, mdNode | Worker-safe yes
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 oconvIrToMd = runtime.resolve('oconvIrToMd');
In practice this module is reached through the oconv facade
(oconv.toMd(...)/oconv.fromMd for the read direction, none for write —
toMd is the ONLY documented facade member for markdown output); resolving
it directly is for tooling that wants the profile-v1 writer in isolation.
API
| Member | Signature | Returns | Throws |
|---|---|---|---|
irToMd |
(ir: object, meta: OconvMeta, opts?: object) => OconvMdResult |
{markdown, anchors, lossy, losses, assets} |
oconv: bad front matter, oconv: bad output budget, oconv: output too large (no other input validation; a malformed ir produces malformed output rather than throwing — the caller-facing facade oconv.toMd validates upstream) |
OconvMeta (all caller-supplied provenance — this module reads no clock and
computes no hash): sourceFormat, sourceName, optional title (written right after sourceName when it is a non-empty string, trimmed), sourceBytes,
sourceSha256, convertedAt, converter, converterVersion, engine,
optional losses (reader losses, merged FIRST into the returned ledger).
opts.frontMatter (opt-in): 'full' (the default) writes the front matter above; 'compact' writes the same keys, its losses one entry per distinct (code, detail) in first-occurrence order with a count (- { code: inline/dropped, detail: text:bookmark, count: 3 }); 'none' writes no front matter — the body alone, which is not a profile-v1 document, the result's anchors / lossy / losses / assets being the caller's sidecar. The result's losses is the full ledger in every form. Any other value throws oconv: bad front matter; other opts keys are ignored.
opts.outputBudget (since 2.0.0; toMd forwards its own): { bytes?, overflow? }, a bound on the IR's text measured before the render. Past bytes (a positive safe integer, 256 MiB when absent; Number.MAX_SAFE_INTEGER renders everything), overflow 'throw' (also when absent) throws oconv: output too large before any markdown is built; 'truncate' keeps the top-level blocks that fit and a crossing table's leading rows, recorded doc/truncated, the first of the render's losses. With no outputBudget, a document past 256 MiB of text throws. Another shape throws oconv: bad output budget.
OconvMdResult: markdown (front matter + rendered body), anchors
({level, anchor}[], document order), lossy (losses.length > 0),
losses (reader losses then this module's own, document order), assets
({kind: 'image', name, bytes?}[], deduplicated by name).
Examples
Convert a two-block IR document
// Build the IR with the oconvIr helpers: `node` fills each node's frozen
// defaults, so the tree passes `oconvIr.validate` (a bare
// `{ kind: 'run', text }` literal does not).
const { node, doc } = runtime.resolve('oconvIr');
const ir = doc([
node('heading', { level: 1 }, [node('run', { text: 'Title' })]),
node('paragraph', {}, [
node('run', { text: 'Hello ' }),
node('run', { text: 'world', bold: true })
])
]);
const meta = {
sourceFormat: 'docx', sourceName: 'x.docx', sourceBytes: 10,
sourceSha256: 'abc123', convertedAt: '2026-09-15T00:00:00Z',
converter: 'oconv', converterVersion: '2.0.0', engine: 'bun'
};
const { markdown, anchors, lossy } = oconvIrToMd.irToMd(ir, meta);
// markdown starts with the YAML front matter, then "# Title\n\nHello **world**\n"
// anchors deep-equals [{ level: 1, anchor: 'title' }]
// lossy === false
Executed against the live package (2026-10-03): the front matter opens
---\nprofile: v1\nir: oconv-ir/v2\n…, the body renders # Title followed
by Hello **world**, anchors is exactly [{ level: 1, anchor: 'title' }]
and lossy is false.
Notes
- Front matter key order is exact and frozen — see
profile-v1 for the full key-by-key reference.
anchors:,losses:(only present whenlossy: true) andassets:are each omitted entirely when empty, never emitted as[]. - The body is never assembled as raw text. This module builds an
@awacloud/mdAST with themdNodefactory and serialises it with the resolvedmdmodule'srenderMarkdown(ast)— escaping, table padding and fence widths are@awacloud/md's job (src/write/ir-to-md.js:8-11). - The asset manifest's
bytesfield is read from the shape the reader really produces.imageNodepopulatesassets[].bytesfrom an IRimagenode'sescapes.docx.bytes, else itsescapes.odt.bytes, else itsescapes.pdf.bytes, else itsescapes.pptx.bytes— the nested fieldsdocx-to-ir.jsandodt-to-ir.jsstore for an embedded image (andpdf-to-ir.jsfor a placed picture, underinclude: ['images'], andpptx-to-ir.jsfor a slide picture). The array is passed through by reference (the sameUint8Array, never copied) and is never serialized into the YAML front matter. A flatescapes.bytesis not read: no shipped reader produces it, so the single nested shape is the contract. The real-corpus legpoi-with-gif.docxintests/fidelity.integration.test.jspins it end to end:toMdreturns one asset (Grafik 1) whosebytesis a 6554-byteUint8Array, withlossystillfalse. - A
run.link === ''(present but empty) recordslink/target-missingand emits the run unlinked; arun.link === undefinedis a plain run, no loss. See the loss matrix for the published Preserved/Degraded/Dropped classification ofblock/droppedandlink/target-missing. - A GFM task item (
listItem.escapes.md.task, set by the md reader) is written back as- [ ]/- [x], so md → IR → md keeps it. A task item whose first block is not a paragraph has no line for the marker: it recordslist/task-marker-dropped(detail[x]or[ ]). - A table cell's block content is flattened to inline text (paragraphs and
headings joined by a space); a heading flattened inside a cell produces no
#-heading and contributes no anchor. - A list or a table inside a table cell is written inline, not dropped: its
text is kept, the pieces (a list's items, a table's cells row by row,
nested ones too) joined by
;, and the structure is recorded asinline/flattened(detailtable cell listortable cell table). Any other block a GFM cell cannot hold is still dropped (block/dropped). - A
\nin a run's text is a hard line break (an odttext:line-breakreads as one). In a paragraph it is written as a CommonMark hard break — a backslash at the end of the line — and a break that ends the paragraph is dropped (it has no meaning there). A heading or a table cell cannot hold one: the break becomes a space and recordsinline/linebreak-degraded(detailhard break). A code run keeps its newline inside the code span. - Capture-free by contract (
fw/no-factory-capture): the anchor-slug algorithm is re-declared inside this module's factory rather than imported from./anchors.js, and a drift test pins the two copies identical. opts.frontMatterandopts.outputBudgetare the writer's options; other keys are read nowhere.
See also
ir-to-docx/ir-to-odt/ir-to-pdf— the other three IR writers.- Structured-markdown profile v1 — the full wire contract this module emits.
- Loss matrix — published fidelity classification.