Convert every office file of a directory to Markdown
Convert every office file of a folder to Markdown; a corrupt file is reported, not fatal.
AGPL-3.0-onlyNode
Package: @awacloud/tool-convert
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.mdThe 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
-
npm install @awacloud/tool-convert(it depends on@awacloud/fw,@awacloud/mdand@awacloud/oconv). -
Copy
main.jsand runbun main.js ./documents(ornode main.js ./documents); pass an ISO-8601 timestamp as the second argument for reproducible front matter. -
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-convertresolves{ markdown, losses, lossy }or aCoreError{ error, usage }— it never throws, so one failing file cannot abort the loop;isCoreError(result)tells the two apart;TO_MD_FORMATSis 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,skipfor the rest; then aconverted / skipped / failedtotal. 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
.docxand.odtthrough the package's ownfromMd, a.txtas the unsupported case and a truncated.docxas the corrupt one, all in a temp directory. - Not shown:
fromMd,convertandtoHtmlof the same core API, the CLI shell itself (src/index.ts, exit codes 1 and 2) and the MCP server.
Code
main.js
// 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);
}