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 when lossy: true) and assets: are each omitted entirely when empty, never emitted as [].
  • The body is never assembled as raw text. This module builds an @awacloud/md AST with the mdNode factory and serialises it with the resolved md module's renderMarkdown(ast) — escaping, table padding and fence widths are @awacloud/md's job (src/write/ir-to-md.js:8-11).
  • The asset manifest's bytes field is read from the shape the reader really produces. imageNode populates assets[].bytes from an IR image node's escapes.docx.bytes, else its escapes.odt.bytes, else its escapes.pdf.bytes, else its escapes.pptx.bytes — the nested fields docx-to-ir.js and odt-to-ir.js store for an embedded image (and pdf-to-ir.js for a placed picture, under include: ['images'], and pptx-to-ir.js for a slide picture). The array is passed through by reference (the same Uint8Array, never copied) and is never serialized into the YAML front matter. A flat escapes.bytes is not read: no shipped reader produces it, so the single nested shape is the contract. The real-corpus leg poi-with-gif.docx in tests/fidelity.integration.test.js pins it end to end: toMd returns one asset (Grafik 1) whose bytes is a 6554-byte Uint8Array, with lossy still false.
  • A run.link === '' (present but empty) records link/target-missing and emits the run unlinked; a run.link === undefined is a plain run, no loss. See the loss matrix for the published Preserved/Degraded/Dropped classification of block/dropped and link/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 records list/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 as inline/flattened (detail table cell list or table cell table). Any other block a GFM cell cannot hold is still dropped (block/dropped).
  • A \n in a run's text is a hard line break (an odt text:line-break reads 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 records inline/linebreak-degraded (detail hard 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.frontMatter and opts.outputBudget are the writer's options; other keys are read nowhere.

See also