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.

Mail in Laravel

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

Jan 08, 2020 Laravel Version: v6.10.0 PHP Version: 7.2.21 (Valet / Mac OS) Database Driver & Version: Description: Markdown parser @component('mail::panel') shows RAW HTML tags. This issue is not present in Laravel version v6.9.0 and bellow. Bootstraping a Laravel blog (model, controller and routes) First thing we need is a bootstrapped Laravel app. You can check this previous article in which I create a Laravel + Tailwind CSS project. Once we have our bootstrapped project, lets focus in the blog. First we'd need to create a migration to create the articles table. Laravel Markdown was created by, and is maintained by Graham Campbell, and is a CommonMark wrapper for Laravel. It ships with integration with Laravel's view system too. Feel free to check out the change log, releases, security policy, license, code of conduct, and contribution guidelines. A list of default Laravel markdown components with screenshot for reference. Note to Laravel 7+ users: Laravel 7 has improved the markdown components, and removed the undocumented promotion component.

Markdown Mailables

Markdown laravel 6Laravel

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.

Markdown Laravel Email

What happens when you Mail::to()โ†’send() ?

Markdown laravel

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?

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.

Markdown Laravel Template

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.

Markdown

The Markdown

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

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.