mozex/commonmark-routes

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

Maintainers

Package info

github.com/mozex/commonmark-routes

pkg:composer/mozex/commonmark-routes

Fund package maintenance!

mozex

Statistics

Installs: 6 719

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

1.4.1 2026-03-05 10:13 UTC

This package is auto-updated.

Last update: 2026-03-12 00:30:19 UTC


README

Latest Version on Packagist GitHub Tests Workflow Status License Total Downloads

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 This Project

I maintain this package along with several other open-source PHP packages used by thousands of developers every day.

If my packages save you time or help your business, consider sponsoring my work on GitHub Sponsors. Your support lets me keep these packages updated, respond to issues quickly, and ship new features.

Business sponsors get logo placement in package READMEs. See sponsorship tiers →

Installation

Requires PHP 8.2+

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("[route('home')](route('home'))");
// Output: <p><a href="https://domain.com">https://domain.com</a></p>

echo $converter->convert("[<route('home')>](<route('home')>)");
// Output: <p><a href="https://domain.com">https://domain.com</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>

echo $converter->convert("[route('home', ['id' => 'features'], false)](route('home', ['id' => 'features'], false))");
// Output: <p><a href="/?id=features">/?id=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.