- How do I install league/commonmark in a Laravel project?
- Run `composer require league/commonmark` in your project root. The package requires PHP 7.4+ and the `mbstring` extension. No additional Laravel-specific setup is needed for basic usage.
- Can I use this package to render Markdown in Laravel Blade templates?
- Yes, but you’ll need a wrapper (e.g., a Blade directive or a service provider). Pair it with a Twig/Blade extension like `aptoma/twig-markdown` for seamless integration in views.
- Does league/commonmark support GitHub Flavored Markdown (GFM)?
- Yes, use the `GithubFlavoredMarkdownConverter` class instead of `CommonMarkConverter` to enable GFM features like tables, task lists, and strikethrough.
- How do I secure user-generated Markdown to prevent XSS attacks?
- Configure the converter with `html_input => 'strip'` and `allow_unsafe_links => false`. This strips unsafe HTML and blocks external links by default.
- What Laravel versions does league/commonmark officially support?
- The package itself has no Laravel-specific dependencies, but it works with Laravel 8+ (PHP 7.4+). Test thoroughly if using older Laravel versions with PHP 7.4+.
- Can I extend league/commonmark to add custom syntax (e.g., emoji or LaTeX)?
- Yes, use the `Environment` and `Extension` interfaces to add custom extensions. The package includes examples for GFM and supports third-party extensions like `commonmark-ext-pygments-highlighter`.
- How do I cache parsed Markdown in Laravel to improve performance?
- Cache the HTML output using Laravel’s `cache()` facade. For example, store the result of `$converter->convert()` in a cache key tied to the Markdown content hash.
- What are the alternatives to league/commonmark for Laravel Markdown parsing?
- Alternatives include `parsedown/parsedown` (simpler but less extensible) or `graham-campbell/laravel-markdown` (a Laravel wrapper for league/commonmark). Choose based on your need for GFM or custom extensions.
- Will upgrading league/commonmark break my custom extensions?
- Minor updates may fix spec compliance issues, potentially altering HTML output. Major versions require testing, especially if you rely on undocumented behavior or custom AST transformations.
- How do I integrate league/commonmark with Laravel’s service container?
- Bind the converter as a singleton in a service provider: `$this->app->singleton(CommonMarkConverter::class, fn() => new CommonMarkConverter(['html_input' => 'strip']));`. Inject it into controllers or services via constructor.