Open in a new tab

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

  1. npm install @awacloud/md (it depends on @awacloud/fw).
  2. Serve node_modules/@awacloud/fw/src/ at /vendor/fw/ and node_modules/@awacloud/md/src/ at /vendor/md/ (node serve.js from the examples root does this; any static server works).
  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 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.js registers the package's manifest arrays on a fresh ModuleRuntime and returns the md façade lazily.
  • Rendering: md.renderHtml(text, { safe: true, sanitize: true }). safe is the renderer's own guard — raw HTML blocks and inline HTML are dropped, javascript:, vbscript:, file: and data: URLs are neutralised. sanitize then runs fw's HTML sanitiser over the output as an independent second boundary (its options go in sanitizeOpts).
  • Why both are explicit: the façade already runs with safe on (raw HTML dropped, dangerous URL schemes neutralised) and leaves sanitize off; the example writes both so the boundary is visible at the one place text becomes HTML, and the test pins that safe: false — the explicit opt-out — is what lets a javascript: link and an onerror attribute through.
  • The sink: the sanitised HTML is the only HTML the page writes (preview.innerHTML = render(...)), the same pattern as fw's dom-template example with sanitize.sanitizeHtml.
  • Not shown: the extras (mdFullBundle: footnotes, admonitions, math, emoji…), md.parse and the AST (see md/table-of-contents), the single-file document builder (see the md-to-html-document recipe).

Code

main.js

javascriptAGPL-3.0-only30 lines

// 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

htmlAGPL-3.0-only43 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>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>