Home

1 minute read

Converting Markdown to HTML in Laravel

Tony Lea

Converting Markdown to HTML in Laravel

In the latest version of Laravel, converting your markdown into HTML couldn't be easier. All you need to do is reach for the handy Str helper method and you can accomplish this in one line.

Converting Markdown to HTML

You can simply run the new Str::markdown() helper to convert markdown text into clean HTML, like so:

{!! Str::markdown($body) !!}

Remember, you will need to echo out the HTML inside your blade files using the {!! !!} instead of {{ }}, this way the HTML output does not get parsed.

It couldn't be easier, right?

The Str namespace

Make sure that you are including the Str namespace in your file:

use Illuminate\Support\Str;

Str::markdown($body);

This way you can utilize the Str helper.

Convert markdown in one line

Additionally, you can include the full path to the helper macro and run it in one line:

{!! Illuminate\Support\Str::markdown($body) !!}

Easy Peezy as Mac and Cheesy 🙌 Be sure to check out all the Str helpers that are available for use at the Laravel Helpers Documentation page.