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 Laravel Package

league/commonmark

Extensible PHP Markdown parser supporting the full CommonMark spec and GitHub-Flavored Markdown. Works with PHP 7.4+ (mbstring) and provides simple converters to turn Markdown into HTML with configurable safety options.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Markdown Processing Core: league/commonmark is a highly extensible CommonMark/GFM parser, making it a strong fit for Laravel applications requiring structured, spec-compliant Markdown rendering (e.g., documentation, CMS content, or user-generated content).
  • Laravel Synergy: Aligns well with Laravel’s service container, Blade templating, and content management needs (e.g., markdown() helpers, API responses, or rich-text editors).
  • Extensibility: Supports custom extensions (e.g., emoji, YouTube embeds, LaTeX) via the extension system, enabling tailored functionality without monolithic bloat.
  • Security-First Design: Built-in XSS/SSRF protections (e.g., DisallowedRawHtml, DomainFilteringAdapter) mitigate risks from untrusted input, critical for Laravel’s multi-user environments.

Integration Feasibility

  • Composer Integration: Zero-config via composer require league/commonmark; no Laravel-specific bootstrapping required.
  • Service Provider Pattern: Can be registered as a Laravel service provider for dependency injection (e.g., CommonMarkConverter bound to markdown).
  • Blade Integration: Easily integrated into Blade directives (e.g., @markdown) or global helpers (e.g., markdown($content)).
  • API/CLI Use Cases: Ideal for API responses (e.g., rendering Markdown in JSON:API) or CLI tools (e.g., generating static sites).

Technical Risk

  • Breaking Changes: Follows SemVer strictly; minor/patch updates are safe, but major versions may require AST/HTML output adjustments (e.g., bug fixes aligning with CommonMark spec).
  • Performance: Optimized regex (v2.7.1+) and benchmarking tools included, but large-scale parsing (e.g., 10K+ docs) may need caching (e.g., Laravel’s cache() or Redis).
  • Security: Critical vulnerabilities (e.g., SSRF/XSS in v2.8.x) are actively patched; default config (allow_unsafe_links: false) mitigates risks but requires customization for trusted HTML.
  • PHP Version: PHP 7.4+ required; Laravel 10+ (PHP 8.1+) is fully compatible.

Key Questions

  1. Use Case Scope:
    • Is this for user-generated content (risk: XSS) or static docs (lower risk)?
    • Do you need GFM extensions (tables, task lists) or CommonMark-only?
  2. Performance Needs:
    • Will you parse high-volume Markdown? If yes, caching (e.g., cache()->remember()) is recommended.
  3. Customization:
    • Do you need custom extensions (e.g., syntax highlighting, emoji)?
    • Will you modify the AST for post-processing (e.g., analytics tracking)?
  4. Laravel-Specific:
    • Should it be globally available (service provider) or scoped (e.g., only in CMS controllers)?
    • Will it integrate with Laravel’s file storage (e.g., parsing .md files from storage/app)?

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Service Container: Bind CommonMarkConverter/GithubFlavoredMarkdownConverter as a singleton or context-bound service.
    • Blade: Create a @markdown directive or global markdown() helper.
    • APIs: Use for dynamic content rendering (e.g., return response()->json(['content' => markdown($request->input('body'))])).
    • File System: Parse .md files from storage/ or public/ (e.g., docs, blog posts).
  • Third-Party Integrations:
    • Tiptap/Quill: Render Markdown from rich-text editors.
    • Spatie Media Library: Store Markdown in metadata fields.
    • Laravel Nova: Display Markdown in custom fields.

Migration Path

  1. Installation:
    composer require league/commonmark
    
  2. Basic Setup:
    • Option A (Global Helper):
      // app/Helpers/markdown.php
      if (!function_exists('markdown')) {
          function markdown(string $input): string {
              return app(\League\CommonMark\CommonMarkConverter::class)->convert($input);
          }
      }
      
    • Option B (Service Provider):
      // app/Providers/AppServiceProvider.php
      public function register() {
          $this->app->singleton(\League\CommonMark\CommonMarkConverter::class, function () {
              return new \League\CommonMark\CommonMarkConverter([
                  'html_input' => 'strip',
                  'allow_unsafe_links' => false,
              ]);
          });
      }
      
  3. Blade Integration:
    // app/Providers/BladeServiceProvider.php
    Blade::directive('markdown', function ($expression) {
        return "<?php echo markdown({$expression}); ?>";
    });
    
    Usage:
    @markdown($post->content)
    
  4. Advanced Customization:
    • Add Extensions:
      $converter = new CommonMarkConverter();
      $converter->addExtension(new \League\CommonMark\Extension\Table\TableExtension());
      
    • Override Defaults:
      $converter = new GithubFlavoredMarkdownConverter([
          'autolink' => true,
          'strikethrough' => true,
      ]);
      

Compatibility

  • Laravel Versions: Works with Laravel 4+ (PHP 7.4+); no deprecations in Laravel 10/11.
  • PHP Extensions: Requires mbstring (enabled by default in Laravel).
  • Database Storage: Store Markdown in text/longtext columns; avoid mediumtext for large docs (>16KB).
  • Caching: Use Laravel’s cache (e.g., cache()->remember('markdown_'.$id, ...)) for frequently accessed content.

Sequencing

  1. Phase 1: Core Integration
    • Install package, set up service provider/helper, test basic rendering.
  2. Phase 2: Security Hardening
    • Configure html_input: 'strip', allow_unsafe_links: false; add DomainFilteringAdapter if needed.
  3. Phase 3: Extensions & Customization
    • Add GFM extensions (tables, task lists) or community extensions (emoji, YouTube).
  4. Phase 4: Performance Optimization
    • Implement caching for parsed Markdown; benchmark with ./vendor/bin/phpunit --testdox (if using tests).
  5. Phase 5: Monitoring
    • Log parsing errors (e.g., malformed Markdown); set up Laravel Horizon for queue-based processing if parsing is heavy.

Operational Impact

Maintenance

  • Dependency Updates:
    • Minor/Patch: Safe to update (follows SemVer); test with composer update league/commonmark --with-dependencies.
    • Major: Requires integration testing (AST/HTML output changes may affect custom logic).
  • Security Patches:
    • Automated: Use composer outdated + composer update for security releases (e.g., v2.8.x fixes).
    • Manual: Monitor GitHub Releases and Tidelift.
  • Backward Compatibility:
    • Internal APIs: Avoid @internal methods; rely on public CommonMarkConverter/GithubFlavoredMarkdownConverter.
    • Output Changes: Minor spec updates may alter HTML output (e.g., whitespace, tag nesting).

Support

  • Troubleshooting:
    • Common Issues:
      • XSS: Ensure html_input: 'strip' and allow_unsafe_links: false are set.
      • Performance: Profile with ./tests/benchmark/benchmark.php; cache parsed results.
      • Extensions: Debug with CommonMarkConverter::getEnvironment()->getExtensions().
    • Debugging Tools:
      • AST Dumping: Use CommonMarkConverter::getEnvironment()->getParser()->parse() to inspect the Abstract Syntax Tree.
      • Logging: Wrap conversions in try-catch to log malformed Markdown.
  • Community Resources:
    • Documentation: commonmark.thephpleague.com (comprehensive).
    • GitHub Discussions: Active community for custom extensions/bugs.
    • Tidelift: Professional
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.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope