Markdown live preview
Render Markdown to safe HTML as the user types; scripts and event handlers do not survive.
AGPL-3.0-onlyRuns on this page
Package: @awacloud/md
A textarea rendered to HTML on every input through the md façade with
safe and sanitize on; a <script>, a javascript: link and an
onerror attribute in the input are shown not to survive.
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
Any place a person writes Markdown and sees the result at once — a comment box, a note editor, a form's description field — where the text is theirs and therefore untrusted. The preview must never become a way to run script in the page.
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). - 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. - Keep
render()as the single place that turns text into HTML, and keep{ safe: true, sanitize: true }in it.
What it shows
- Wiring:
bootstrapMd({ runtime })from@awacloud/md/bootstrap.jsregisters the package's manifest arrays on a freshModuleRuntimeand returns themdfaçade lazily. - Rendering:
md.renderHtml(text, { safe: true, sanitize: true }).safeis the renderer's own guard — raw HTML blocks and inline HTML are dropped,javascript:,vbscript:,file:anddata:URLs are neutralised.sanitizethen runs fw's HTML sanitiser over the output as an independent second boundary (its options go insanitizeOpts). - Why both are explicit: the façade already runs with
safeon (raw HTML dropped, dangerous URL schemes neutralised) and leavessanitizeoff; the example writes both so the boundary is visible at the one place text becomes HTML, and the test pins thatsafe: false— the explicit opt-out — is what lets ajavascript:link and anonerrorattribute through. - The sink: the sanitised HTML is the only HTML the page writes
(
preview.innerHTML = render(...)), the same pattern as fw'sdom-templateexample withsanitize.sanitizeHtml. - Not shown: the extras (
mdFullBundle: footnotes, admonitions, math, emoji…),md.parseand the AST (seemd/table-of-contents), the single-file document builder (see themd-to-html-documentrecipe).
Code
main.js
// SPDX-License-Identifier: AGPL-3.0-only
import { ModuleRuntime } from '@awacloud/fw/core/runtime.js';
import { bootstrapMd } from '@awacloud/md/bootstrap.js';
// registers md's manifest arrays; `md` resolves on first use
const { md } = bootstrapMd({ runtime: new ModuleRuntime() });
/**
* CommonMark + GFM to HTML with both guards on. `safe` is the renderer's own:
* raw HTML is dropped and `javascript:` / `data:` URLs are neutralised.
* `sanitize` then pipes the result through fw's HTML sanitiser, an independent
* second boundary. The façade already defaults `safe` to on; `sanitize` stays off by default,
* and this page sets both explicitly so the two guards stay visible where user text becomes HTML.
* `safe: false` is the explicit raw-HTML opt-out.
* @param {string} text
* @returns {string} HTML that is safe to write into the page
*/
export const render = (text) => md.renderHtml(text, { safe: true, sanitize: true });
const source = /** @type {HTMLTextAreaElement} */ (document.getElementById('source'));
const preview = /** @type {HTMLElement} */ (document.getElementById('preview'));
const count = /** @type {HTMLElement} */ (document.getElementById('count'));
function update() {
preview.innerHTML = render(source.value); // the only HTML this page writes, and it has been sanitised
count.textContent = `${source.value.length} characters → ${preview.children.length} block(s)`;
}
source.addEventListener('input', update);
update();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 live preview</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 live preview</h1>
<p><label for="source">Markdown</label></p>
<textarea id="source" rows="10" cols="60"># Welcome
Type here, see the **HTML** below.
- CommonMark and GFM
- raw HTML is dropped, `javascript:` links are neutralised
</textarea>
<p id="count" aria-live="polite"></p>
<section id="preview" aria-label="Preview"></section>
</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>