commonmark/commonmark.js
JavaScript Markdown parser and renderer built on CommonMark. Parse Markdown to HTML or an AST, with consistent spec-compliant output and extensibility via plugins and custom rules. Ideal for editors, docs sites, and content pipelines.
First, install @commonmark/js via npm/yarn (npm install @commonmark/js). Despite the package name not matching the repo URL (the official package is @commonmark/js, not commonmark/commonmark.js), this is the canonical CommonMark implementation for JS. Start by parsing and rendering Markdown to HTML in Node.js or the browser:
const { read } = require('fs');
const { Parser, HtmlRenderer } = require('@commonmark/js');
const parser = new Parser();
const renderer = new HtmlRenderer();
const md = '# Hello\n\nThis is **bold** text.';
const ast = parser.parse(md);
const html = renderer.render(ast);
console.log(html); // '<h1>Hello</h1>\n<p>This is <strong>bold</strong> text.</p>\n'
Use the official docs for API reference—focus on Parser, HtmlRenderer, and Node traversal.
HtmlRenderer (or implement your own renderer) to output JSX, JSON, or LaTeX—leverage the enter/leave hooks.rel="noopener" on links, rewrite image paths):
ast.walk((node, info) => {
if (node.type === 'link' && node.destination?.startsWith('/')) {
node.destination = `https://example.com${node.destination}`;
}
});
info.stop() and re-parse if structural changes are needed.HtmlRenderer supports safe (escape HTML), unsafe (allow raw HTML—not recommended), and fragment (no <html>, <body> wrappers). Use fragment: true for inline rendering.remark).console.log(JSON.stringify(ast, null, 2)) during development to inspect node structure—critical for building custom transforms.esm imports (e.g., import { Parser } from '@commonmark/js') and tree-shake to avoid full parser bloat in production.How can I help you explore Laravel packages today?