secondman's avatar

Blade Minify Middleware

Hey All.

I'm integrating Graham Campbell's Laravel-Markdown package so that all of my pages and posts can be entered into my db in markdown format and then rendered correctly in views.

But it seems that the parser is overly sensitive to indentation, see this issue here:

The basic gist is, any html blocks that aren't aligned left will be enclosed in <pre> tags and just print to the screen.

This will work:

<div>
    @yield('content')
</div>

While this will not:

    <div>
        @yield('content')
    </div>

So my thought was to create a template minifier of sorts that would strip any whitespace to the left of each line, as a before middleware on all blade templates.

I've created a ViewMinify middleware:

namespace Acme\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Contracts\View\Factory;

class ViewMinify
{
    
    /**
     * The view factory implementation.
     *
     * @var \Illuminate\Contracts\View\Factory
     */
    protected $view;

    /**
     * Require the view factory
     *
     * @param  \Illuminate\Contracts\View\Factory  $view
     * @return void
     */
    public function __construct(Factory $view)
    {
        $this->view = $view;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        // minify the template here
        
        return $next($request);
        
    }
}

But now I'm stuck :(

0 likes
4 replies
pmall's avatar

You want to send blade template content to your server and compile them with markdown ?!

secondman's avatar

@pmall

No not exactly.

Apparently markdown, or at lest the markdown rendering with the league/commonmark package will only render markdown properly if it's all aligned to the left. Not sure which. Or maybe this is a bug in Graham's package.

Either way something like this won't work:

@extends('front.pages.page-layout')

@section('content')
    # {{ $page->title }}

    {{ $page->content }}
@stop 

So what I ended up having to do was include the markdown file as a partial like so:

@extends('front.pages.page-layout')

@section('content')
    @include('front.pages.page')
@stop 

Where front.pages.page is an .md.blade.php file:

# {{ $page->title }}

{{ $page->content }}

And now it works fine.

So initially what I wanted to do what fetch the actual template and align everything to the left before it was rendered out so that I didn't have to remove all my indenting and line breaks in my templates.

But this is fine in the end, it only effects pages and posts so it's no biggie.

Thanks for posting though, I appreciate it.

pmall's avatar

Whoa i really have a hard time to understand what you mean.

What contains $page->content ? Markdown ? When do you convert it to html ?

MikeHopley's avatar

I'm integrating Graham Campbell's Laravel-Markdown package so that all of my pages and posts can be entered into my db in markdown format and then rendered correctly in views.

You can do that without using Markdown views. It would be a lot simpler.

Do you actually care about writing your view templates in Markdown, or is it just the content you care about?

If it's just the content, then save both the markdown version (for editing later) and the generated HTML to the database. Send the HTML to your bog-standard .blade.php view. Done. You don't need a .md.blade.php view to use markdown content.

Please or to participate in this conversation.