Markdown to one self-contained HTML page
Turn Markdown files into one self-contained, sanitised HTML page, previewed safely.
AGPL-3.0-onlyRuns on this page
Package: @awacloud/md
Turn one or several Markdown sources into ONE self-contained, sanitised HTML
page with mdHtmlDocument.build, preview it in a sandboxed frame, download
it.
Uses @awacloud/md (AGPL-3.0-only) over @awacloud/fw (Apache-2.0). This example's code is AGPL-3.0-only.
When to use it
Shipping documentation as a single file people can open from a mail
attachment, a USB stick or an air-gapped machine: a guide and its reference
in one .html with its theme, its navigation and its cross-links inside,
and no request to any server when it is opened.
Copy-paste steps
npm install @awacloud/md(it depends on@awacloud/fw).- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/andnode_modules/@awacloud/md/src/at/vendor/md/(node serve.jsfrom the examples root does this; any static server works); the import map inindex.htmlpoints there. - Copy
index.htmlandmain.jsnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link. Keep the legal notice under</main>: copy../../LICENSES/AGPL-3.0-only.txtalong, or point its link at your copy. - Replace the two textareas with your own sources:
buildPage([{ path, source }, …], title)takes any number of documents;pathis what cross-links such as[x](reference.md#commands)resolve against.
What it shows
- Wiring:
bootstrapMd({ runtime })from@awacloud/md/bootstrap.jsregisters the package's four manifest arrays on the page'sModuleRuntimeand returns lazy accessors;htmlDocumentismdHtmlDocument, resolved on first use. fw'sdownloadis registered on the same runtime. - The build:
htmlDocument.build({ documents, title })returns{ html, documents, warnings }: one complete<!DOCTYPE html>page with the theme embedded in a<style>element and no<link>,src="http…"or other external reference; per document, its headings with the ids the page uses (guide--first-runwhen several documents share the page); warnings for unresolved links, fallback titles and unanchored headings. - Cross-links by path:
[first run](guide.md#first-run)in the second document becomes#guide--first-run, an in-page link. - Sanitised last: every fragment passes the fw sanitiser after the
renderer and the extras. The test feeds a
<script>, ajavascript:link and an<img onerror>and checks none survives in the body while the text does. - Preview and download: the page goes into
<iframe sandbox="allow-scripts" srcdoc>: the built page's own navigation script runs, in an opaque origin with no access to this page or its storage; anddownload.text(html, '<title>.html', 'text/html;charset=utf-8')hands out the very same bytes — the test reads the blob back and compares. - Not shown: the md extras (footnotes, admonitions, math, emoji…) that
build({ extras })can install, theclassificationbanner, customcss, and thenoticelines; themdexamples cover the renderer itself.
Code
main.js
// 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 { bootstrapMd } from '@awacloud/md/bootstrap.js';
const rt = new ModuleRuntime();
rt.register(download);
const { htmlDocument } = bootstrapMd({ runtime: rt }); // registers md's four manifest arrays on `rt`
const DOWNLOAD = rt.resolve('download');
/**
* One or several Markdown sources → ONE self-contained HTML page: embedded
* theme, a sidebar when there are several documents, per-document table of
* contents, cross-links by path, and every fragment passed through the fw
* sanitiser last. No external reference is emitted.
* @param {{ path: string, source: string }[]} documents
* @param {string} [title] the page title (defaults to the first document's title, or "Documents")
* @returns {{ html: string,
* documents: { id: string, title: string, headings: { level: number, text: string, id: string }[] }[],
* warnings: { code: string, document: string, detail: string }[] }}
*/
export function buildPage(documents, title) {
return htmlDocument.build(title ? { documents, title } : { documents });
}
const sources = /** @type {HTMLTextAreaElement[]} */ ([...document.querySelectorAll('textarea')]);
const titleInput = /** @type {HTMLInputElement} */ (document.getElementById('title'));
const preview = /** @type {HTMLIFrameElement} */ (document.getElementById('preview'));
const status = /** @type {HTMLElement} */ (document.getElementById('status'));
const saveButton = /** @type {HTMLButtonElement} */ (document.getElementById('download'));
let built = /** @type {string | null} */ (null);
document.getElementById('build')?.addEventListener('click', () => {
const documents = sources.filter((area) => area.value.trim())
.map((area) => ({ path: `${area.id}.md`, source: area.value }));
const result = buildPage(documents, titleInput.value.trim() || undefined);
built = result.html;
preview.srcdoc = built;
status.textContent = `${result.documents.length} document(s), `
+ `${new TextEncoder().encode(built).length} bytes, ${result.warnings.length} warning(s)`;
saveButton.disabled = false;
});
saveButton.addEventListener('click', () => {
if (built) DOWNLOAD.text(built, `${titleInput.value.trim() || 'documents'}.html`, 'text/html;charset=utf-8');
});index.html
<!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>md · Markdown to one self-contained HTML page</title>
<link rel="stylesheet" href="../../_style/examples.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/",
"@awacloud/md": "/vendor/md/main.js", "@awacloud/md/": "/vendor/md/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Markdown to one self-contained HTML page</h1>
<p>
<label for="title">Page title</label>
<input id="title" type="text" value="Field guide">
</p>
<p>
<label for="guide">First document (guide.md)</label><br>
<textarea id="guide" rows="8" cols="60"># Getting started
Install the tool, then read [the reference](reference.md).
## First run
Type `hello` and press Enter.
</textarea>
</p>
<p>
<label for="reference">Second document (reference.md)</label><br>
<textarea id="reference" rows="8" cols="60"># Reference
## Commands
- `hello`: greets you
- `quit`: leaves
## Options
See [first run](guide.md#first-run) for an example.
</textarea>
</p>
<p>
<button id="build" type="button">Build</button>
<button id="download" type="button" disabled>Download .html</button>
<span id="status" aria-live="polite"></span>
</p>
<iframe id="preview" title="Preview of the built page" sandbox="allow-scripts" width="100%" height="480"></iframe>
</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/md · AGPL-3.0-only</li>
</ul>
</footer>
<!-- aln:end -->
</body>
</html>