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.
84 lines (83 loc) • 3.16 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.markdownToCommonMark = markdownToCommonMark;
exports.renderCommonMark = renderCommonMark;
/**
* This module provides the functionality to convert Markdown documents into
* [CommonMark](https://commonmark.org/) format, either in a single step or by
* formatting an existing abstract syntax tree ({@linkcode AST}).
*
* @see {@linkcode markdownToCommonMark} to convert Markdown to CommonMark.
* @see {@linkcode renderCommonMark} to render an existing {@linkcode AST}.
* @see {@linkcode parseMarkdown} to parse Markdown into an {@linkcode AST}.
*
* @module cm
*/
const _wasm_js_1 = __importDefault(require("./_wasm.js"));
const _internal_js_1 = require("./_internal.js");
/**
* Parses a Markdown document into an {@linkcode AST}, rendering it back into
* CommonMark format in a single step.
*
* This is equivalent to calling {@linkcode renderCommonMark} on the result of
* the {@linkcode parseMarkdown} function.
*
* @param md The Markdown string to be converted.
* @param [options] Options to customize the conversion.
* @returns The generated CommonMark string.
* @example
* ```ts
* import { markdownToCommonMark } from "@nick/comrak";
* import assert from "node:assert";
*
* const cm = markdownToCommonMark("Hello, **_Nick_**!");
* assert.strictEqual(cm, "Hello, ***Nick***\\!\n");
* ```
* @category Conversion
*/
function markdownToCommonMark(md, options) {
const [opts, , , ...fns] = (0, _internal_js_1.collectOptions)(options);
return _wasm_js_1.default.markdown_to_commonmark(md, opts, null, null, ...fns);
}
/**
* Formats an abstract syntax tree (AST), produced by parsing a Markdown
* document with the {@linkcode parseMarkdown} function, into CommonMark 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 CommonMark, you should
* consider the single-step {@linkcode markdownToCommonMark} functioninstead.
*
* @param ast The AST to be formatted.
* @param [options] Options to customize the formatting.
* @returns The generated CommonMark string.
* @see {@linkcode parseMarkdown} to parse Markdown into an {@linkcode AST}.
* @example
* ```ts
* import {
* renderCommonMark,
* 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 cm = renderCommonMark(ast, options);
* assert.strictEqual(cm, "# Hello, world\\!\n\nHow are you?\n");
* ```
* @category Rendering
*/
function renderCommonMark(ast, options) {
const [opts, , , ...fns] = (0, _internal_js_1.collectOptions)(options);
return _wasm_js_1.default.format_commonmark(ast, opts, null, null, ...fns);
}