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.
101 lines (100 loc) • 3.88 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.markdownToXML = markdownToXML;
exports.renderXML = renderXML;
/**
* This module provides the functionality to convert Markdown documents into
* [CommonMark XML](https://commonmark.org/xml) format, either in a single step
* (from Markdown text to CommonMark XML text) or by rendering an existing
* abstract syntax tree ({@linkcode AST}) into XML.
*
* @see {@linkcode markdownToXML} to convert Markdown to CommonMark XML.
* @see {@linkcode renderXML} to render an existing {@linkcode AST} into XML.
* @see {@linkcode parseMarkdown} to parse Markdown into an {@linkcode AST}.
*
* @module xml
*/
const _wasm_js_1 = __importDefault(require("./_wasm.js"));
const _internal_js_1 = require("./_internal.js");
/**
* Parses a Markdown document into an {@linkcode AST} and renders it back into
* CommonMark XML format in a single step. This is equivalent to calling the
* {@linkcode parseMarkdown} function followed by {@linkcode renderXML}.
*
* @param markdown The Markdown string to be converted.
* @param [options] Options to customize the conversion.
* @returns The generated XML string.
* @example
* ```ts
* import assert from "node:assert";
* import { markdownToXML } from "@nick/comrak";
*
* const xml = markdownToXML("Hello, **Nick**!");
* assert.strictEqual(xml, '<?xml version="1.0" encoding="UTF-8"?>\n' +
* '<!DOCTYPE document SYSTEM "CommonMark.dtd">\n' +
* '<document xmlns="http://commonmark.org/xml/1.0">\n' +
* ' <paragraph>\n' +
* ' <text xml:space="preserve">Hello, </text>\n' +
* ' <strong>\n' +
* ' <text xml:space="preserve">Nick</text>\n' +
* ' </strong>\n' +
* ' <text xml:space="preserve">!</text>\n' +
* ' </paragraph>\n' +
* '</document>\n');
* ```
* @category Conversion
* @tags xml
*/
function markdownToXML(markdown, options) {
const args = (0, _internal_js_1.collectOptions)(options);
return _wasm_js_1.default.markdown_to_xml(markdown, ...args);
}
/**
* Formats an abstract syntax tree (AST), produced by parsing a Markdown
* document with the {@linkcode parseMarkdown} function, into CommonMark
* XML 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 XML, you should
* consider using the single-step {@linkcode markdownToXML} function instead.
*
* @param ast The AST to be formatted.
* @param [options] Options to customize the formatting.
* @returns The generated XML string.
* @see {@linkcode parseMarkdown} to parse Markdown into an {@linkcode AST}.
* @example
* ```ts
* import { renderXML, 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 xml = renderXML(ast, options);
* assert.strictEqual(xml, '<?xml version="1.0" encoding="UTF-8"?>\n' +
* '<!DOCTYPE document SYSTEM "CommonMark.dtd">\n' +
* '<document xmlns="http://commonmark.org/xml/1.0">\n' +
* ' <heading level="1">\n' +
* ' <text xml:space="preserve">Hello, world!</text>\n' +
* ' </heading>\n' +
* ' <paragraph>\n' +
* ' <text xml:space="preserve">How are you?</text>\n' +
* ' </paragraph>\n' +
* '</document>\n');
* ```
* @category Rendering
* @tags xml
*/
function renderXML(ast, options) {
const args = (0, _internal_js_1.collectOptions)(options);
return _wasm_js_1.default.format_xml(ast, ...args);
}