mozex/commonmark-routes

Laravel routes in Markdown: A CommonMark extension to seamlessly use Laravel route functions within your Markdown content.

Fund package maintenance!
mozex

1.1.0 2024-06-28 16:22 UTC

This package is auto-updated.

Last update: 2024-10-28 17:08:29 UTC


README

Latest Version on Packagist GitHub Tests Workflow Status License

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.

Table of Contents

Support us

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!

Installation

Requires PHP 8.1+

You can install the package via composer:

composer require mozex/commonmark-routes

Usage

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.

Spatie Laravel Markdown

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,
],

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.