Markdown Laravel



From the About section of league/commonmark package:

  • Highly-extensible PHP Markdown parser which fully supports the CommonMark and GFM specs.

This package helps us parse markdown in PHP. If you are not familiar with markdown, GitHub has super helpful guide about it here.

Laravel Markdown Components

Markdown Laravel

Mail in Laravel

Customizing the Markdown email template in Laravel Posted on 10th September 2019 Updated on 11th September 2020 Laravel by Gergő D. Nagy It’s possible that we need a bit more control over our markdown email templates. :arrowdown: A parsedown (markdown) wrapper for Laravel - maxhoffmann/parsedown-laravel. A step-by-step guide to create a Laravel 7 blog that uses Markdown. I use SimpleMDE as the markdown editor and parse the markdown to HTML thanks to GitDown and the GitHub Markdown API, obtaining clean HTML with code highlighted blocks.

Laravel provides simple and easy ways to send emails. There are two options in Laravel to send emails:

  • Mailables
  • Notifications

I created a poll on Twitter to see how Developers usually send emails in their Laravel apps. Based on the results, seems like Mailable is the go-to choice. We'll also use Mailable to explore further.

(Interesting replies on this tweet, give them a read)

How do you usually send emails in a Laravel app? 👀#Laravel#PHP

— Zubair Mohsin (@Zubairmohsin33) March 15, 2021

Markdown Mailables

Let's generate a markdown mailable using Artisan.

We get two files as a result of this command.

  • AppMailNewsletterSubscribed class
  • resources/views/emails/newsletter/subscribed.blade.php file

Let's take a look at view file:

We can see that there's a combination of Blade Components and Markdown in this file. These components and others are made available by Laravel, read more about components here. When we send out this mailable, league/commonmark package comes into play and parse this markdown to HTML.

What happens when you Mail::to()→send() ?

We are specifying the recipient in to() method and the mailable class in send() method.

Code Dive

Let's dive into send() method. It leads us to IlluminateMailPendingMail class.

Mailable class is being filled with addresses and then its calling send method on the given Mailer instance. As we can see, $mailer is an instance of class which implements the Mailer contract. Where do we find its concrete class implementation?

Markdown

Markdown Laravel Email

Finding the Mailer

When request comes in and Laravel registers the ServiceProviders, part of these providers is IllumiateMailMailServiceProvider. Let's take a look at its register method.

It is binding a singleton of MailManager class and then bindMailer by calling mailer() method on mail.manager singleton above.

  • We can already see a Markdown class being registered. We will eventually reach to this class.
  • Read more about singleton() and bind() binding methods in the documentation

Let's dig into mailer() method of MailManager class.

Above code can be roughly translated to:

  • Get the default mail driver / default mailer (which is smtp) then call get() method on it. get() method checks if given mailer has already been resolved ( local cache ), otherwise resolve the concrete class for the given mailer.

We found the Mailer concrete class. Yay!

It's located at IlluminateMailMailer. It was kind of obvious, but finding it through code-dive was fun. Alright, moving on...

The Mailer

We are interested in send() method on the Mailer class. Let's take a look:

In the send() method it checks if $view is an instance of Mailable , in our case this is true. We are indeed working with Mailable and we passed a mailable from PendingMail class.

Then it sets Mailer on Mailable class itself and then calls send method on the Mailable.

The Mailable

If we were to find concrete class that implements Mailable class, this post will become huge.

Therefore, we are just going to assume that IlluminateMailMailable class is what we need as our NewsletterSubscribed class extends it and it also implements IlluminateContractsMailMailable interface.

In context of our topic, buildView() method is of our interest. Below is its implementation and related methods.

buildMarkdownView() method is where it is initialising Markdown class from container and rendering the markdown view of our NewlsetterSubscribed mail.

The Markdown

In the Markdown class, there are two important methods. render and parse.

And we can finally see inside the parse() method that it utilizes classes from league/commonmark package like CommonMarkConverter and TableExtension etc.

But we never called parse() method from anywhere? And we can see nothing inside the render() method related to markdown either?

Well, parse() method is called from within the email views. Our subscribed.blade.php view uses @component('mail::message') which in-turn uses @component('mail::layout') , and if we take a look at layout component at Mailresourcesviewshtmllayout.blade.php , we see an HTML template with table layout and some styling.

There we see the following code in which parse() method is being called.

When the subscribed view is rendered, parse gets called and our markdown content is parsed using league/commonmark package.

Interesting facts about Markdown in Laravel

  • Laravel started supporting Markdown syntax in emails **in version 5.4**
  • The first package used to parse markdown was erusev/parsedown
  • In Laravel v6.0, they switched from erusev/parsedown to league/commonmark for safety features.
  • Taylor Otwell removed erusev/parsedown from composer.json and added league/commonmark on 30 Dec, 2019.
  • Commit on GitHub can be found on this link
Markdown

I hope you enjoyed this post. Next, we will see how Laravel uses league/flysystem package. You can follow me on Twitter or join my newsletter to keep yourself updated.