Run it

   bun node_modules/@awacloud/tool-convert/src/index.ts to-md report.docx --at 2026-01-01T00:00:00.000Z --out report.md
   bun node_modules/@awacloud/tool-convert/src/index.ts to-md slides.pptx --out slides.md

The tool-convert core API in process: every supported file of a directory becomes <name>.md, the rest is skipped, a corrupt file prints an error line through isCoreError and the run continues, then a total. node kind.

Uses @awacloud/tool-convert (AGPL-3.0-only) over @awacloud/oconv (AGPL-3.0-only) and @awacloud/fw (Apache-2.0). This example's code is AGPL-3.0-only.

When to use it

Feeding a documentation set, a wiki import or a retrieval pipeline from a folder of .docx, .odt, .xlsx, .ods, .pptx, .odp and .pdf files, with a per-file verdict you can log and a run that never stops on one bad file.

Copy-paste steps

  1. npm install @awacloud/tool-convert (it depends on @awacloud/fw, @awacloud/md and @awacloud/oconv).

  2. Copy main.js and run bun main.js ./documents (or node main.js ./documents); pass an ISO-8601 timestamp as the second argument for reproducible front matter.

  3. The equivalent CLI, one file at a time (the package declares no bin):

    bun node_modules/@awacloud/tool-convert/src/index.ts to-md report.docx --at 2026-01-01T00:00:00.000Z --out report.md
    bun node_modules/@awacloud/tool-convert/src/index.ts to-md slides.pptx --out slides.md
    

What it shows

  • The core API: toMd({ name, bytes, convertedAt }) from @awacloud/tool-convert resolves { markdown, losses, lossy } or a CoreError { error, usage } — it never throws, so one failing file cannot abort the loop; isCoreError(result) tells the two apart; TO_MD_FORMATS is the list of extensions the batch accepts.
  • One line per file: ok, lossy (n) with the ledger length, error: <message> for a file that does not parse, skip for the rest; then a converted / skipped / failed total. The output is sorted by file name and carries no path or timestamp, so a test pins it.
  • The inputs are generated: the test writes its .docx and .odt through the package's own fromMd, a .txt as the unsupported case and a truncated .docx as the corrupt one, all in a temp directory.
  • Not shown: fromMd, convert and toHtml of the same core API, the CLI shell itself (src/index.ts, exit codes 1 and 2) and the MCP server.

Code

main.js

javascriptAGPL-3.0-only52 lines

// SPDX-License-Identifier: AGPL-3.0-only
// Run: bun main.js <dir> [<iso-timestamp>]   (or node main.js <dir>)
// Equivalent CLI, one file at a time (the package declares no `bin`):
//   bun node_modules/@awacloud/tool-convert/src/index.ts to-md <file> --at <iso-timestamp> --out <file>.md
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { extname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { TO_MD_FORMATS, isCoreError, toMd } from '@awacloud/tool-convert';

/**
 * Convert every supported file of `dir` to `<name>.md` beside it, skip the
 * rest, and keep going when one file fails: the core API never throws, it
 * returns `{ error, usage }` that `isCoreError` recognises.
 * @param {string} dir
 * @param {string} convertedAt ISO-8601, recorded in every front matter (pass a fixed value for reproducible output)
 * @returns {Promise<{ lines: string[], converted: number, skipped: number, failed: number }>}
 *   one line per file, then a total
 */
export async function batchToMarkdown(dir, convertedAt) {
    const out = { lines: /** @type {string[]} */ ([]), converted: 0, skipped: 0, failed: 0 };
    for (const name of readdirSync(dir).sort()) {
        const extension = extname(name);
        const format = extension.slice(1).toLowerCase();
        if (!TO_MD_FORMATS.includes(/** @type {any} */ (format))) {
            out.skipped += 1;
            out.lines.push(`${name}: skip (supported: ${TO_MD_FORMATS.join(', ')})`);
            continue;
        }
        const result = await toMd({ name, bytes: new Uint8Array(readFileSync(join(dir, name))), convertedAt });
        if (isCoreError(result)) {
            out.failed += 1;
            out.lines.push(`${name}: error: ${result.error}`);
            continue;
        }
        writeFileSync(join(dir, `${name.slice(0, -extension.length)}.md`), result.markdown);
        out.converted += 1;
        out.lines.push(`${name}: ${result.lossy ? `lossy (${result.losses.length})` : 'ok'}`);
    }
    out.lines.push(`${out.converted} converted, ${out.skipped} skipped, ${out.failed} failed`);
    return out;
}

/** @param {string[]} [argv] */
export async function main(argv = process.argv.slice(2)) {
    const [dir, convertedAt = new Date().toISOString()] = argv;
    if (!dir) return ['usage: bun main.js <dir> [<iso-timestamp>]'];
    return (await batchToMarkdown(dir, convertedAt)).lines;
}

if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
    for (const line of await main()) console.log(line);
}