comrak
Version:
Comrak is an efficient, extensible, and highly configurable Markdown parser and renderer, written in Rust and compiled to WebAssembly. Portable and agnostic, it works seamlessly in any WebAssembly-friendly JS runtime.
76 lines (75 loc) • 2.81 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.markdownToHTML = markdownToHTML;
exports.renderHTML = renderHTML;
/**
* This module provides the functionality to convert Markdown documents into
* HTML, either in a single step (from Markdown text to HTML text) or by
* rendering an existing abstract syntax tree ({@linkcode AST}) into HTML.
*
* @see {@linkcode markdownToHTML} to convert Markdown to HTML.
* @see {@linkcode renderHTML} to render an existing {@linkcode AST} into HTML.
* @see {@linkcode parseMarkdown} to parse Markdown into an {@linkcode AST}.
*
* @module html
*/
const _wasm_js_1 = __importDefault(require("./_wasm.js"));
const _internal_js_1 = require("./_internal.js");
/**
* Render Markdown to HTML.
*
* @param markdown The Markdown string to be converted.
* @param [options] Options to customize the conversion.
* @returns The generated HTML string.
* @example
* ```ts
* import assert from "node:assert";
* import { markdownToHTML } from "@nick/comrak";
*
* const html = markdownToHTML("Hello, **Nick**!");
* assert.strictEqual(html, "<p>Hello, <strong>Nick</strong>!</p>\n");
* ```
* @category Conversion
*/
function markdownToHTML(markdown, options) {
const args = (0, _internal_js_1.collectOptions)(options);
return _wasm_js_1.default.markdown_to_html(markdown, ...args);
}
/**
* Formats an abstract syntax tree (AST), produced by parsing a Markdown
* document with the {@linkcode parseMarkdown} function, into HTML text.
*
* **Note**: This is a low-level function that is primarily intended for
* advanced use cases where direct manipulation of the AST is required. If you
* simply want to convert a Markdown document into HTML, you should consider
* using the single-step {@linkcode markdownToHTML} function instead.
*
* @param ast The AST to be formatted.
* @param [options] Options to customize the formatting.
* @returns The generated HTML string.
* @see {@linkcode parseMarkdown} to parse Markdown into an {@linkcode AST}.
* @example
* ```ts
* import { renderHTML, parseMarkdown, type Options } from "@nick/comrak";
* import assert from "node:assert";
*
* const options = {
* extension: {
* alerts: true,
* footnotes: true,
* },
* } satisfies Options;
*
* const ast = parseMarkdown("# Hello, world!\n\nHow are you?", options);
* const html = renderHTML(ast, options);
* assert.strictEqual(html, "<h1>Hello, world!</h1>\n<p>How are you?</p>\n");
* ```
* @category Rendering
*/
function renderHTML(ast, options) {
const args = (0, _internal_js_1.collectOptions)(options);
return _wasm_js_1.default.format_html(ast, ...args);
}