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

spatie/commonmark-shiki-highlighter

League/CommonMark code block renderer that highlights fenced code using Shiki PHP. Includes extra Antlers and Blade grammars in addition to Shiki’s 100+ languages. Ideal for Laravel setups (see spatie/laravel-markdown). Requires JS shiki v1.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Leverage for Laravel/PHP: The package is a block renderer extension for league/commonmark, enabling syntax-highlighted code blocks via Shiki (a high-performance, language-agnostic syntax highlighter). It fits seamlessly into Laravel applications where Markdown processing is required (e.g., documentation, CMS, or dynamic content rendering).
  • Modularity: The package is lightweight and non-intrusive, designed to integrate with existing league/commonmark pipelines without requiring major architectural changes.
  • Use Cases:
    • Enhancing user-generated Markdown (e.g., blog comments, wikis).
    • Documentation systems (e.g., API docs, internal guides).
    • Dynamic content rendering (e.g., admin panels, CMS).
    • Code-heavy applications (e.g., IDE-like interfaces, tutorials).

Integration Feasibility

  • Dependencies:
    • Requires league/commonmark (v1.0+), which is widely used in Laravel via spatie/laravel-markdown.
    • Requires spatie/shiki-php (Shiki PHP port), which must be installed separately.
    • No Laravel-specific dependencies beyond league/commonmark integration.
  • Compatibility:
    • Works with Laravel 8+ (PHP 8.0+) due to league/commonmark and Shiki PHP requirements.
    • Supports Blade templates (via spatie/laravel-markdown) and Antlers (Laravel’s templating engine).
    • No database migrations or schema changes required—purely runtime logic.
  • Customization:
    • Configurable via CommonMark environment (e.g., theme, language overrides).
    • Extensible for custom languages or post-processing of highlighted code.

Technical Risk

Risk Area Assessment Mitigation Strategy
Performance Shiki PHP is fast, but heavy Markdown processing may impact latency. Cache rendered Markdown (e.g., spatie/laravel-caching).
Dependency Bloat Adds shiki-php (~5MB) and league/commonmark (~1MB). Justify ROI for code-heavy apps; avoid if syntax highlighting is non-critical.
Breaking Changes Shiki PHP updates may introduce syntax changes. Pin versions in composer.json; monitor spatie/shiki-php releases.
Browser Compatibility Shiki outputs CSS classes for styling; ensure theme supports them. Use a Shiki-compatible CSS theme (e.g., github-dark, vscode-dark).
Edge Cases Malformed Markdown or unsupported languages may fail gracefully. Implement fallback rendering (e.g., plain-text code blocks).

Key Questions

  1. Why Syntax Highlighting?

    • Is this for user-facing content (e.g., docs) or internal tools (e.g., admin panels)?
    • What’s the expected volume of Markdown processing (e.g., per-request vs. batch)?
  2. Styling & Theming

    • Do you have a CSS framework (e.g., Tailwind, Bootstrap) that conflicts with Shiki’s output?
    • Will you customize the highlighted code appearance (e.g., line numbers, copy buttons)?
  3. Performance Trade-offs

    • Can you cache rendered Markdown (e.g., for static docs)?
    • Is Shiki’s initial load time acceptable for your use case?
  4. Alternatives

    • Have you considered client-side highlighting (e.g., Prism.js, Highlight.js) to offload work?
    • Would a simpler package (e.g., league/commonmark-extensions) suffice?
  5. Long-Term Maintenance

    • Who will update Shiki PHP and handle breaking changes?
    • Is there a fallback plan if Shiki PHP deprecates a language?

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Primary Integration: Use spatie/laravel-markdown (if already in use) to wrap league/commonmark with Shiki.
    • Blade Integration: Render Markdown in Blade templates via @markdown directive.
    • APIs: Process Markdown in controllers/services and return highlighted HTML.
  • Non-Laravel PHP:
    • Directly extend league/commonmark with the CommonMarkShikiHighlighter renderer.
  • Frontend:
    • Shiki outputs HTML + CSS classes; ensure your frontend theme supports them.
    • Optionally, post-process output with JavaScript (e.g., add copy buttons).

Migration Path

  1. Assess Current Markdown Setup:
    • Are you using league/commonmark directly or another library (e.g., michelf/php-markdown)?
    • If using spatie/laravel-markdown, this package is a drop-in extension.
  2. Install Dependencies:
    composer require spatie/commonmark-shiki-highlighter spatie/shiki-php
    
  3. Configure CommonMark:
    use Spatie\CommonMarkShikiHighlighter\CommonMarkShikiHighlighter;
    use League\CommonMark\Environment;
    
    $environment = new Environment();
    $environment->addRenderer(new CommonMarkShikiHighlighter());
    
  4. Leverage Laravel Helpers (if applicable):
    // In a controller or service
    $markdown = Markdown::parse('# Code Example', [
        'extensions' => [new CommonMarkShikiHighlighter()],
    ]);
    
  5. Test Edge Cases:
    • Unsupported languages (fallback to plain text).
    • Malformed Markdown (ensure graceful degradation).

Compatibility

Component Compatibility Notes
Laravel Versions 8.0+ (PHP 8.0+ required by Shiki PHP).
PHP Versions 8.0+ (Shiki PHP drops PHP 7.x support).
Markdown Libraries Only league/commonmark (v1.0+).
Frontend Frameworks Works with any CSS-compatible frontend (e.g., Tailwind, Bootstrap, vanilla CSS).
Caching Layers Compatible with Laravel’s cache (e.g., spatie/laravel-caching).

Sequencing

  1. Phase 1: Proof of Concept
    • Test in a non-production environment with sample Markdown.
    • Verify performance and styling.
  2. Phase 2: Core Integration
    • Integrate into key Markdown-processing endpoints (e.g., docs, CMS).
    • Add caching for static content.
  3. Phase 3: Optimization
    • Profile performance (e.g., tideways/xhprof).
    • Optimize Shiki theme or disable unused languages.
  4. Phase 4: Rollout
    • Gradually enable in production.
    • Monitor for rendering failures or performance regressions.

Operational Impact

Maintenance

  • Dependency Updates:
    • Monitor spatie/shiki-php for breaking changes (e.g., language syntax updates).
    • Pin versions in composer.json to avoid surprises.
  • Configuration Drift:
    • Track Shiki theme/language settings in a config file (e.g., config/markdown.php).
  • Fallback Mechanisms:
    • Implement a plain-text fallback for unsupported languages or errors.

Support

  • Debugging:
    • Log failed Markdown parsing (e.g., unsupported languages).
    • Use Shiki’s debug mode to inspect rendering issues.
  • User Training:
    • Document supported languages and Markdown syntax for content creators.
  • Frontend Support:
    • Ensure CSS themes are compatible with Shiki’s output classes.

Scaling

  • Performance Bottlenecks:
    • Shiki Initialization: Lazy-load Shiki if processing is infrequent.
    • Batch Processing: Use queues (e.g., Laravel Queues) for large Markdown volumes.
    • Caching: Cache rendered HTML (e.g., Redis) for static content.
  • Horizontal Scaling:
    • Stateless processing means no database locks; scales with Laravel’s queue workers.
  • Language Support:
    • Shiki’s 100+ languages may increase memory usage; disable unused ones.

Failure Modes

Failure Scenario Impact Mitigation
Shiki PHP Crashes Broken rendering Fallback to plain-text code blocks.
Unsupported Language Unhighlighted code Log warnings; document supported languages.
**
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
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