Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mniblett's avatar

best way to render markdown in views

OK, so I am want to have a text field that the user can use markdown with their input, and then when I show that input I want it rendered into HTML. In other words, just like this forum. I'm wondering what the best/easiest way of doing this is? I've looked at three different packages:

Option 1; GrahamCampbell/Laravel-Markdown: useage: Markdown::convertToHtml('foo') Here it looks like I will need to write a function inside my class (or maybe a trait to use across different classes, which I've never done but it would be a good exorcise for me to learn how to do that) to parse the output so I can write my blade files something like:

<p><strong>Premise: </strong> {{ $story->premise->toHTML() }}</p>

or, could I just do this inside the blade file?:

<p><strong>Premise: </strong> {{ Markdown::convertToHtml($story->premise) }}</p> 

Option 2: andreasindal/laravel-markdown I like this one because it seems really simple, there are blade directives

@markdown($post->body)

and blockstyle:

@markdown
# Hello world
This *text* will be **parsed** to [HTML](http://laravel.com).
@endmarkdown

Seems really easy and straightforward, this is all I really need, so I am leaning towards this option.

Option 3; RobinRadic/blade-extensions I like this because there are a lot of other extensions that look super handy. But it seems to only be current through laravel 5.1 (maybe that doesn't matter?) The markdown directive seems to be the similar to the blade directive noted above in andreasindal's package (I'd need to add a parsedown package and it supports the same one used there).

So, how do you guys do this kind of thing?

0 likes
16 replies
Amaury's avatar

Maybe you should try the 'erusev/parsedown-extra' package used by the laravel.com site.

It seems to work perfectly well, isn't it?

KevinKirchner's avatar

It looks like in /vendor/laravel/framework/src/Illuminate/Mail/resources/views/html/layout.blade.php Laravel is using {{ Illuminate\Mail\Markdown::parse($slot) }} for parsing markdown. That's what I'm going to try now.

2 likes
julienb's avatar

{{ Illuminate\Mail\Markdown::parse($slot) }} works great!

4 likes
jagov's avatar

If you add

 'aliases' => [
        # [...] ,
         'Markdown' => Illuminate\Mail\Markdown::class
]

to your config/app.php file. You can use it like

{!! Markdown::parse($slot) !!}
7 likes
joaozitopolo's avatar

It's a call to Parsedown... You can call directly:

{!! (new Parsedown)->text($slot) !!}
chrispage1's avatar

Just to add to this, if you are using Markdown in lots of places, I'd recommend creating a custom directive...

In the boot method of your AppServiceProvider (or any other provider if you have custom ones) -

Blade::directive('markdown', function ($expression) {
  return "<?php echo (new Parsedown)->text($expression); ?>";
});

You can then render markdown using @markdown($content) within your views. Thanks to @joaozitopolo for the suggestion on how to render Markdown within Laravel.

Make sure to clear your View cache with php artisan view:clear before testing otherwise chances are it won't work.

Just to add another level to this, if you are using it a lot rather than instantiating a new instance of Parsedown every time, I'd recommend creating a singleton and referencing that instead -

class BladeServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        app()->singleton('parsedown', function () {
            return new Parsedown();
        });
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        Blade::directive('markdown', function ($expression) {
            return "<?php echo app('parsedown')->text($expression); ?>";
        });
    }
}

As you can see above, I've abstracted this out into my own BladeServiceProvider service provider. I hope this helps someone!

5 likes
D0n0van's avatar

I would recommend @chrispage1 's answer with a little tweak

Blade::directive('markdown', function () {
    return "<?php echo (new Parsedown)->text(<<<HEREDOC";
});

Blade::directive('endmarkdown', function () {
    return "HEREDOC); ?>";
});

Which should allow you to do:

@markdown 
# Title
Text
@endmarkdown
4 likes
danagram's avatar

@D0n0van Great solution. In my particular case, this failed when mixing markdown and blade directives. I was able to over come this limitation with the following adjustments:

Blade::directive('markdown', function () {
    return '<?php ob_start(); ?>';
});

Blade::directive('endmarkdown', function () {
    return '<?php echo str(ob_get_clean())->markdown(); ?>';
});
nenad's avatar

It seems that in laravel 6

use League\CommonMark\CommonMarkConverter is used insted of Parsedown

1 like
BlackBarba's avatar

This is a old question, but it's still one of the first results on Google when search for Laravel and Markdown, so I think it needs a update.

Laravel 8 now has a built-in markdown parser in its helpers, witch allows you to do things like these:

use Illuminate\Support\Str;

$html = Str::markdown($text);

In the documentation: https://laravel.com/docs/8.x/helpers#method-fluent-str-markdown

24 likes
aljaste's avatar

For Laravel 10: https://laravel.com/docs/10.x/strings#method-fluent-str-markdown (you can still use the Str::markdown() method directly, simply pass the markdown string as the first parameter and options as the second).

Basic configuration options for CommonMark: https://commonmark.thephpleague.com/2.4/configuration/

Additionally, options from the CommonMark GFM extension are included: https://commonmark.thephpleague.com/2.4/extensions/github-flavored-markdown/

1 like

Please or to participate in this conversation.