Open in a new tab

Pick a .docx (or another office file), convert it in the page with oconv.toMd, show the Markdown, the heading index and the loss ledger, download the .md. Nothing is uploaded.

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

When to use it

Turning documents people already have into Markdown for a wiki, a knowledge base or a retrieval pipeline, on the user's machine: the file is read into memory and converted there, so confidential documents never reach a server. The ledger tells the user what the conversion could not carry over instead of hiding it.

Copy-paste steps

  1. npm install @awacloud/oconv (it depends on @awacloud/fw, md, ooxml, odf, pdf and fonts).
  2. Serve each package's src/ directory at /vendor/<name>/ — fw, pdf, fonts, oconv, md, ooxml, odf — with any static server; the import map in index.html points there.
  3. Copy index.html and main.js next to each other and open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link. Keep the legal notice under </main>: copy ../../LICENSES/AGPL-3.0-only.txt along, or point its link at your copy.
  4. Keep convertedAt explicit: the page passes the current time, a test passes a fixed value, and the front matter records it.

What it shows

  • Wiring: one ModuleRuntime registers @awacloud/oconv's fw_require and modules arrays (the latter already carries the four office packages' graphs) plus fw's download; rt.resolve('oconv') is the façade.
  • The conversion: oconv.toMd({ name, bytes, convertedAt, engine }) detects the format from the file extension (.docx, .odt, .xlsx, .ods, .pptx, .odp, .pdf), and returns markdown (YAML front matter with provenance, then CommonMark/GFM), anchors (the heading index with the ids a chunker can rely on), lossy and losses.
  • The ledger: every { code, detail } the readers and the writer record is listed; an empty ledger shows an explicit "no loss recorded" line. The test converts a .docx written by oconv.fromMd in the test itself (no binary fixture): headings, bold and a list survive, and that document records no loss.
  • The download: download.text(markdown, 'report.md', 'text/markdown;charset=utf-8') — the test reads the blob back and checks it equals the Markdown shown.
  • Errors: an unsupported file (.txt) shows oconv: unsupported format in the status line and clears the output; nothing is thrown at the page.
  • Not shown: the oconv Web Worker entry (the same toMd off the main thread, for large files) and fromMd / convert (see the oconv examples).

Code

main.js

javascriptAGPL-3.0-only67 lines

// SPDX-License-Identifier: AGPL-3.0-only
import { ModuleRuntime } from '@awacloud/fw/core/runtime.js';
import { download } from '@awacloud/fw/dom/fs/download.js';
import { fw_require, modules } from '@awacloud/oconv';

const rt = new ModuleRuntime();
rt.registerAll([...fw_require, ...modules, download]);
const OCONV = rt.resolve('oconv');
const DOWNLOAD = rt.resolve('download');

/**
 * Convert one office document to Markdown, in memory. `convertedAt` lands in
 * the front matter: pass a fixed value when you need reproducible output.
 * @param {File} file a .docx, .odt, .xlsx, .ods, .pptx, .odp or .pdf (the format comes from the extension)
 * @param {string} convertedAt ISO-8601
 * @returns {Promise<{ markdown: string, anchors: { level: number, anchor: string }[], lossy: boolean,
 *   losses: { code: string, detail: string }[] }>}
 */
export async function convertFile(file, convertedAt) {
    const bytes = new Uint8Array(await file.arrayBuffer());
    return OCONV.toMd({ name: file.name, bytes, convertedAt, engine: 'browser' });
}

/** `report.docx` → `report.md` @param {string} name */
export const markdownName = (name) => `${name.replace(/\.[^.]+$/, '')}.md`;

const input = /** @type {HTMLInputElement} */ (document.getElementById('file'));
const output = /** @type {HTMLElement} */ (document.getElementById('markdown'));
const anchors = /** @type {HTMLElement} */ (document.getElementById('anchors'));
const losses = /** @type {HTMLElement} */ (document.getElementById('losses'));
const status = /** @type {HTMLElement} */ (document.getElementById('status'));
const saveButton = /** @type {HTMLButtonElement} */ (document.getElementById('download'));
let current = /** @type {{ name: string, markdown: string } | null} */ (null);

/** @param {HTMLElement} list @param {string[]} items */
function fill(list, items) {
    list.replaceChildren(...items.map((text) => {
        const li = document.createElement('li');
        li.textContent = text;
        return li;
    }));
}

input.addEventListener('change', async () => {
    const [file] = input.files ?? [];
    if (!file) return;
    try {
        const result = await convertFile(file, new Date().toISOString());
        current = { name: markdownName(file.name), markdown: result.markdown };
        output.textContent = result.markdown;
        fill(anchors, result.anchors.map((a) => `${'#'.repeat(a.level)} ${a.anchor}`));
        fill(losses, result.lossy ? result.losses.map((l) => `${l.code}: ${l.detail}`) : ['no loss recorded']);
        status.textContent = `${file.name}: ${result.markdown.length} characters of Markdown, `
            + `${result.anchors.length} heading(s)`;
    } catch (err) {
        current = null;
        output.textContent = '';
        fill(anchors, []);
        fill(losses, []);
        status.textContent = `${file.name}: ${/** @type {Error} */ (err).message}`;
    }
    saveButton.disabled = current === null;
});

saveButton.addEventListener('click', () => {
    if (current) DOWNLOAD.text(current.markdown, current.name, 'text/markdown;charset=utf-8');
});

index.html

htmlAGPL-3.0-only56 lines

<!doctype html>
<!-- SPDX-License-Identifier: AGPL-3.0-only -->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>oconv · Convert a .docx to Markdown in the browser</title>
  <link rel="stylesheet" href="../../_style/examples.css">
  <script type="importmap">
  { "imports": {
    "@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/",
    "@awacloud/pdf": "/vendor/pdf/main.js", "@awacloud/pdf/": "/vendor/pdf/",
    "@awacloud/fonts": "/vendor/fonts/main.js", "@awacloud/fonts/": "/vendor/fonts/",
    "@awacloud/oconv": "/vendor/oconv/main.js", "@awacloud/oconv/": "/vendor/oconv/",
    "@awacloud/md": "/vendor/md/main.js", "@awacloud/md/": "/vendor/md/",
    "@awacloud/ooxml": "/vendor/ooxml/main.js", "@awacloud/ooxml/": "/vendor/ooxml/",
    "@awacloud/odf": "/vendor/odf/main.js", "@awacloud/odf/": "/vendor/odf/" } }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <main>
    <h1>Convert a .docx to Markdown in the browser</h1>
    <p>
      <label for="file">Office document</label>
      <input id="file" type="file" accept=".docx,.odt,.xlsx,.ods,.pptx,.odp,.pdf">
      <span id="status" aria-live="polite"></span>
    </p>
    <p>The file never leaves the page: it is read and converted in memory.</p>
    <h2>Markdown</h2>
    <pre id="markdown"></pre>
    <h2>Headings</h2>
    <ul id="anchors"></ul>
    <h2>Loss ledger</h2>
    <ul id="losses"></ul>
    <p><button id="download" type="button" disabled>Download .md</button></p>
  </main>
  <!-- aln:begin (AGPL-3.0 sections 0, 5d and 13; derived by _harness/contract.js legalNotice, checked by C7) -->
  <footer id="legal" aria-label="Legal notices">
    <p>Copyright (c) 2026 AwaCloud SAS. This page is free software under the GNU Affero General Public License,
      version 3 only (AGPL-3.0-only), with ABSOLUTELY NO WARRANTY:
      <a href="../../LICENSES/AGPL-3.0-only.txt">licence text</a>.</p>
    <p>It runs these packages, each with its own LICENSE and NOTICE file (third-party notices included):</p>
    <ul>
      <li>@awacloud/fw · Apache-2.0</li>
      <li>@awacloud/pdf · AGPL-3.0-only</li>
      <li>@awacloud/fonts · AGPL-3.0-only</li>
      <li>@awacloud/oconv · AGPL-3.0-only</li>
      <li>@awacloud/md · AGPL-3.0-only</li>
      <li>@awacloud/ooxml · AGPL-3.0-only</li>
      <li>@awacloud/odf · AGPL-3.0-only</li>
    </ul>
  </footer>
  <!-- aln:end -->
</body>
</html>