An extension for league/commonmark that allows you to use Laravel routes inside markdown, just as you would in your PHP code.
Warning: This extension is intended for use in controlled environments where the markdown is trusted. Do not use this extension for processing user-input markdown due to potential security risks.
- Support Us
- Installation
- Usage
- Testing
- Changelog
- Contributing
- Security Vulnerabilities
- Credits
- License
Creating and maintaining open-source projects requires significant time and effort. Your support will help enhance the project and enable further contributions to the PHP community.
Sponsorship can be made through the GitHub Sponsors program. Just click the "Sponsor" button at the top of this repository. Any amount is greatly appreciated, even a contribution as small as $1 can make a big difference and will go directly towards developing and improving this package.
Thank you for considering sponsoring. Your support truly makes a difference!
Requires PHP 8.1+
You can install the package via composer:
composer require mozex/commonmark-routes
Register RoutesExtension as a CommonMark extension and use the route function instead of URLs in your markdown, just as you would in your PHP code.
use League\CommonMark\Environment\Environment;
use League\CommonMark\CommonMarkConverter;
use Mozex\CommonMarkRoutes\RoutesExtension;
$converter = new CommonMarkConverter($environment);
$converter->getEnvironment()->addExtension(new RoutesExtension());
echo $converter->convert("[Home](route('home'))");
// Output: <p><a href="https://domain.com">Home</a></p>
echo $converter->convert("[Home](<route('home')>)");
// Output: <p><a href="https://domain.com">Home</a></p>
echo $converter->convert("[Home](route('home', absolute: false))");
// Output: <p><a href="/">Home</a></p>
echo $converter->convert("[Product](route('product', 3))");
// Output: <p><a href="https://domain.com/product/3">Product</a></p>
echo $converter->convert("[Features](route('home', ['id' => 'features']))");
// Output: <p><a href="https://domain.com?id=features">Features</a></p>
echo $converter->convert("[Features](route('home', ['id' => 'features'], false))");
// Output: <p><a href="/?id=features">Features</a></p>
For more information on CommonMark extensions and environments, refer to the CommonMark documentation.
When using the Laravel Markdown package, you may register the extension in config/markdown.php
:
/*
* These extensions should be added to the markdown environment. A valid
* extension implements League\CommonMark\Extension\ExtensionInterface
*
* More info: https://commonmark.thephpleague.com/2.4/extensions/overview/
*/
'extensions' => [
Mozex\CommonMarkRoutes\RoutesExtension::class,
],
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.