WASM API Reference v2.3.0¶
Package: @kreuzberg/html-to-markdown-wasm | Version: 2.28.1
The WASM binding runs in browsers, Node.js, Deno, and edge runtimes (Cloudflare Workers, Vercel Edge). No native compilation required.
Installation¶
Functions¶
convert¶
Convert HTML to Markdown.
Parameters:
| Parameter | Type | Description |
|---|---|---|
html | string | The HTML string to convert |
options | ConversionOptions? | Optional conversion configuration |
Returns: string -- the converted Markdown.
Throws: Error if conversion fails.
Example:
import { convert } from "@kreuzberg/html-to-markdown-wasm";
const markdown = convert("<h1>Hello</h1><p>World</p>");
// With options
const markdown = convert(html, {
headingStyle: "atx",
codeBlockStyle: "backticks",
});
convertWithMetadata¶
Convert HTML with metadata extraction.
function convertWithMetadata(
html: string,
options?: ConversionOptions,
metadataConfig?: MetadataConfig
): MetadataExtraction
Returns: MetadataExtraction with markdown and metadata properties.
Example:
import { convertWithMetadata } from "@kreuzberg/html-to-markdown-wasm";
const { markdown, metadata } = convertWithMetadata(html);
console.log(metadata.document.title);
console.log(metadata.headers.length);
convertBytes¶
Convert UTF-8 HTML bytes to Markdown (useful for binary data from fetch responses).
convertWithOptionsHandle¶
Convert using a pre-compiled options handle.
createConversionOptionsHandle¶
Create a reusable options handle.
Browser Usage¶
ES Module Import¶
<script type="module">
import init, { convert } from "@kreuzberg/html-to-markdown-wasm";
// Initialize the WASM module (required once)
await init();
const html = "<h1>Hello</h1><p>World</p>";
const markdown = convert(html);
document.getElementById("output").textContent = markdown;
</script>
With a Bundler (Vite, Webpack, etc.)¶
import init, { convert } from "@kreuzberg/html-to-markdown-wasm";
async function setup() {
await init();
return convert;
}
const convertHtml = await setup();
const markdown = convertHtml("<p>Hello World</p>");
Dynamic Import (Lazy Loading)¶
async function convertHtml(html: string): Promise<string> {
const { default: init, convert } = await import(
"@kreuzberg/html-to-markdown-wasm"
);
await init();
return convert(html);
}
Node.js Usage¶
import init, { convert } from "@kreuzberg/html-to-markdown-wasm";
await init();
const markdown = convert("<h1>Title</h1>");
console.log(markdown);
Node.js alternative
For Node.js, consider using @kreuzberg/html-to-markdown-node instead. The native NAPI-RS binding is significantly faster than WASM.
Deno Usage¶
import init, { convert } from "npm:@kreuzberg/html-to-markdown-wasm";
await init();
const markdown = convert("<h1>Title</h1>");
console.log(markdown);
Cloudflare Workers / Edge Runtimes¶
import init, { convert } from "@kreuzberg/html-to-markdown-wasm";
let initialized = false;
export default {
async fetch(request: Request): Promise<Response> {
if (!initialized) {
await init();
initialized = true;
}
const html = await request.text();
const markdown = convert(html);
return new Response(markdown, {
headers: { "Content-Type": "text/plain" },
});
},
};
Interfaces¶
ConversionOptions¶
Same interface as the TypeScript API. All fields are optional with camelCase naming.
interface ConversionOptions {
headingStyle?: "underlined" | "atx" | "atxClosed";
codeBlockStyle?: "indented" | "backticks" | "tildes";
wrap?: boolean;
wrapWidth?: number;
preserveTags?: string[];
stripTags?: string[];
skipImages?: boolean;
outputFormat?: "markdown" | "djot" | "plain";
// ... see Configuration Reference for full list
}
MetadataConfig¶
interface MetadataConfig {
extractDocument?: boolean;
extractHeaders?: boolean;
extractLinks?: boolean;
extractImages?: boolean;
extractStructuredData?: boolean;
maxStructuredDataSize?: number;
}
Bundle Size¶
The WASM binary is approximately 2.6 MB uncompressed. With gzip compression (standard for HTTP), this reduces to approximately 800 KB.
Tips for minimizing bundle impact:
- Use dynamic imports to lazy-load the WASM module
- The WASM binary is cached by the browser after first load
- Consider the native Node.js binding for server-side use
Browser Compatibility¶
| Browser | Minimum Version |
|---|---|
| Chrome | 57+ |
| Firefox | 52+ |
| Safari | 11+ |
| Edge | 16+ |
Requires WebAssembly support. No IE11 support.
Live Demo¶
Try the converter in your browser: Live Demo
See Also¶
- TypeScript API Reference -- native Node.js binding (faster for server-side)
- Configuration Reference -- full options documentation
- Types Reference -- cross-language type definitions