user968270
user968270

Reputation: 4971

Laravel Markdown parser returns tags instead of formatted text

I am using Graham Campbell's markdown parser for Laravel in an application I'm building. While it does indeed parse Markdown into HTML, it is returning the tags literally, rather than the formatted text I'm looking for. Here's the view in question:

@extends('layouts.app')

@section('stylesheet')
  <link rel="stylesheet" href="{{ asset('css/writing.css') }}">
@endsection

@php
use GrahamCampbell\Markdown\Facades\Markdown;
@endphp

@section('content')
  <h2 class="writing_title">writing</h2>
  <h4 class="writing_subtitle">musings & guides</h4>
  <hr class="divider">
  <section class="writing_listing">
    @foreach( $posts as $post )
    <article class="writing_article">
      <div class="writing_article-title_block">
        <h3 class="writing_article-title">{{ $post->title }}</h3>
        <span class="writing_article-datetime">{{ date('M j, Y H:i', strtotime($post->created_at)) }}</span>
      </div>
      <div class="writing_article-content_block">
        {{ str_limit(Markdown::convertToHtml($post->body), 250) }}
      </div>
      <a href="{{ url('writing/'.$post->slug) }}">Read More</a>
    </article>
    @endforeach
  </section>
@endsection

My initial suspicion was that the str_limit method had something to do with it, but altering the order of operations and even removing str_limit entirely had no effect. So, for example, currently, if I have a post with body # Header of post, what I will get back is literally <h1>Header of post</h1> as a string rather than Header of post. And all my paragraphs are bounded by <p></p> as strings as well.

Upvotes: 1

Views: 1117

Answers (1)

sushibrain
sushibrain

Reputation: 2790

The problem most likely lays here:

{{ str_limit(Markdown::convertToHtml($post->body), 250) }}

Double brackets ({{ }}) in laravel are sanitized, which would make your code come out as actual tags instead of formatted output. What you could do is replace this:

{{ str_limit(Markdown::convertToHtml($post->body), 250) }}

with this:

{!! str_limit(Markdown::convertToHtml($post->body), 250) !!}

This syntax will echo unsanitized html into your template.

Hope this helped! Cheers.

Upvotes: 1

Related Questions