#!/usr/bin/env node // Regenerate the export fidelity corpus (issue #69, ADR 0009) from the committed // `*.md` sources: for each source and each office format, write the Markdown you // get back after a full export → re-read round trip through the pinned pandoc. // That round-trip snapshot is what the fidelity gate pins — a change in pandoc's // docx/odt writer (e.g. a version bump) shifts the output and fails the suite. // // docker run --rm -p 3030:3030 pandoc/core:3.6 server // PANDOC_URL=http://localhost:3030 node scripts/gen-export-fixtures.mjs // // Mirrors the export path (ExportService: gfm → , standalone) and then // reads the document back ( → gfm) exactly as export.fidelity.test.ts does. import { readFileSync, writeFileSync, readdirSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import { dirname, join } from 'node:path'; const FIXTURES = join(dirname(fileURLToPath(import.meta.url)), '..', 'fixtures', 'export'); const PANDOC_URL = process.env.PANDOC_URL ?? 'http://localhost:3030'; // Match the import corpus reader so both directions normalise the same way. const MARKDOWN_FORMAT = 'gfm-implicit_figures-raw_html'; const BINARY = new Set(['docx', 'odt']); async function pandoc(params) { const res = await fetch(`${PANDOC_URL}/`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(params), }); if (!res.ok) throw new Error(`pandoc ${res.status}: ${await res.text()}`); return BINARY.has(params.to) ? Buffer.from(await res.arrayBuffer()) : res.text(); } /** Export markdown to an office document, then read it back to markdown. */ async function roundTrip(markdown, ext) { const document = await pandoc({ text: markdown, from: 'gfm', to: ext, standalone: true }); return pandoc({ text: document.toString('base64'), from: ext, to: MARKDOWN_FORMAT, standalone: false, wrap: 'none', }); } const sources = readdirSync(FIXTURES).filter((f) => f.endsWith('.md') && !f.includes('.expected.')); for (const source of sources) { const name = source.replace(/\.md$/, ''); const markdown = readFileSync(join(FIXTURES, source), 'utf8'); for (const ext of ['docx', 'odt']) { writeFileSync(join(FIXTURES, `${name}.${ext}.expected.md`), await roundTrip(markdown, ext)); console.log(`wrote ${name}.${ext}.expected.md`); } }