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

petervandijck's avatar

Writing markdown inside blade

Hi, I'd like to do the following: write markDown in a blade template, so that I can write a long article easily and I don't have to add html tags myself. The idea is to then wrap that into a Tailwind prose class, and voila, I can easily write long articles as a pure blade template.

I haven't been able to make this work. Laravel seems to have markdown built it. The Spatie plugin isn't working for me, it renders a simple line of text as for some reason.

Any tips on how to achieve this? Seems like it would be really handy?

0 likes
5 replies
kokoshneta's avatar

Which Spatie plugin do you mean? Laravel-markdown?

If you want an actual Blade directive for it, you can always just make one yourself. Adding the following in the boot() method in your AppServiceProvider should do it (not tested, but should work in theory):

Blade::directive('markdown', function () {
	return "<?php echo (new League\CommonMark\GithubFlavoredMarkdownConverter())->convert(<<<HEREDOC";
});

Blade::directive('endmarkdown', function () {
	return "HEREDOC)); ?>";
});
2 likes
petervandijck's avatar
petervandijck
OP
Best Answer
Level 1

For people searching for this, this worked (with tweak). Here are the docs https://laravel.com/docs/10.x/blade#extending-blade

To AppServiceProvider, I added:

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Str;

and then in the boot() method of that page, I added

Blade::directive('markdown', function () {
            return "Hello <?php echo(Str::markdown(<<<HEREDOC";
        });

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

(Note the double bracket at the end).

Now in the blade template, I can do:

@markdown

# A first-level heading
## A second-level heading
### A third-level heading
- bullet
- bullet

@endmarkdown

Finally, and this can be tricky, you have to clear the views because you just edited Blade. After you do that, it should work.

php artisan view:clear 
1 like
Snapey's avatar

I was looking what jetstream does.

It ships with a couple of markdown files which are processed in the controller

public function show()
{
		$policyFile = Jetstream::localizedMarkdownPath('policy.md');

        return view('policy', [
            'policy' => Str::markdown(file_get_contents($policyFile)),
        ]);
}

I know you wanted to write in the blade file, but isn't it easier to write in a markdown file, then you can use a markdown editor (or built into your IDE) and get the preview etc.

Your view file (in this case 'policy' ) accepts the $policy variable containing the markdown and you could echo this out (using raw {!! !!} ) inside a div with your prose class applied.

1 like
riettotek's avatar

@Snapey $policyFile = Jetstream::localizedMarkdownPath('policy.md') gots the vendor markdown file instead of resources markdown one. I didnt find anything about these things in the jetstream docs. Do u know where to look?

Please or to participate in this conversation.