Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Commonmark.js Laravel Package

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.

Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Pre-rendering in build pipelines: Pre-parse Markdown files (e.g., in a static site generator) to AST, apply transformations, then render—ensuring deterministic output.
  • Editor integrations: Use the AST to power rich editing experiences (e.g., detect link targets, headings for TOC generation) before final HTML rendering.
  • Custom renderers: Extend HtmlRenderer (or implement your own renderer) to output JSX, JSON, or LaTeX—leverage the enter/leave hooks.
  • Node transformation: Traverse the AST to sanitize or enrich content (e.g., inject 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}`;
      }
    });
    

Gotchas and Tips

  • Whitespace sensitivity: CommonMark treats trailing whitespace and blank lines strictly—ensure consistent formatting in source MD to avoid unexpected HTML output.
  • Node immutability: AST nodes are mutable during traversal, but avoid replacing nodes mid-walk—use info.stop() and re-parse if structural changes are needed.
  • Renderer options: HtmlRenderer supports safe (escape HTML), unsafe (allow raw HTML—not recommended), and fragment (no <html>, <body> wrappers). Use fragment: true for inline rendering.
  • No built-in extensions: This is strict CommonMark—no GitHub Flavored Markdown (GFM). For footnotes, task lists, or tables, layer extensions after parsing (e.g., via custom AST post-processing or a wrapper like remark).
  • Debug AST: Use console.log(JSON.stringify(ast, null, 2)) during development to inspect node structure—critical for building custom transforms.
  • Browser tip: Bundle with esm imports (e.g., import { Parser } from '@commonmark/js') and tree-shake to avoid full parser bloat in production.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
redaxo/debug
redaxo/test
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder