open-agreements
Version:
Open-source legal template filling CLI and library
36 lines • 1.81 kB
JavaScript
/**
* Resolve selector-contract occurrence locators against a cleaned DOCX.
*
* Flow (per the engine spec): load the cleaned DOCX → `insertParagraphBookmarks`
* (required: `buildDocumentView` only includes paragraphs carrying a `_bk_*` id
* and `load()` does not insert them) → `buildDocumentView` → `resolveLocator`
* for every occurrence of every field. Resolution is fully deterministic — the
* determinism guarantee lives in the docx-core primitive.
*/
import { readFileSync } from 'node:fs';
import { DocxDocument, resolveLocator, } from '@usejunior/docx-core';
export function resolveFieldAgainstView(view, manifest) {
const occurrences = manifest.occurrences.map((locator, index) => ({
index,
resolution: resolveLocator(view, locator),
}));
const unresolved = occurrences.some((o) => o.resolution.unresolved);
const assertionFailures = occurrences.flatMap((o) => o.resolution.assertionResults
.filter((a) => !a.ok)
.map((a) => ({ occurrenceIndex: o.index, kind: a.kind, detail: a.detail })));
return { field_id: manifest.field_id, occurrences, unresolved, assertionFailures };
}
/**
* Load `cleanedDocxPath`, bookmark its paragraphs, build the view, and resolve
* every occurrence locator. Returns the loaded document so the caller can patch
* the resolved spans and serialize once.
*/
export async function resolveSelectorContracts(cleanedDocxPath, manifests) {
const buffer = readFileSync(cleanedDocxPath);
const doc = await DocxDocument.load(buffer);
doc.insertParagraphBookmarks('selector_contract');
const { nodes } = doc.buildDocumentView();
const fields = manifests.map((manifest) => resolveFieldAgainstView(nodes, manifest));
return { doc, view: nodes, fields };
}
//# sourceMappingURL=resolve.js.map