Pixelairport's avatar

Reference markdown file in core.blade.php

I want to structure my coding standards in md files in a composer package. At the moment i create a core.blade.php in my package for boost and this works. Now I want to create a directory /package/resource/markdown/ ... to load files from this directory instead put all in the core.blade.php. Is that possible? Because I dont understand how? I already tried it like in spatie package: https://github.com/spatie/boost-spatie-guidelines/blob/main/resources/boost/guidelines/core.blade.php and also tried it like in filament/blueprint, where its just say:

**Start here**: Read
`/vendor/.../overview.md`
0 likes
2 replies
LaryAI's avatar
Level 58

Certainly! Here’s how you can reference and display a Markdown file inside your core.blade.php in a Laravel package, similar to what is achieved in packages like Spatie or Filament.


1. Store Your Markdown Files

Let’s say your markdown files are stored here:

/packages/your-vendor/your-package/resources/markdown/overview.md

2. Read and Parse the Markdown in Your Blade View

First, you need a way to read the markdown file, parse it to HTML, and display it in core.blade.php. Laravel has a built-in Markdown parser as of v9 (using league/commonmark), and you can use this via the facade:


core.blade.php Example

@php
    // The path should be absolute, use base_path or package_path accordingly
    $markdownPath = base_path('packages/your-vendor/your-package/resources/markdown/overview.md');
    $markdownContent = file_exists($markdownPath) 
        ? file_get_contents($markdownPath)
        : '# Markdown file not found.';

    // Parse Markdown to HTML
    $htmlContent = Illuminate\Support\Str::markdown($markdownContent);
@endphp

{!! $htmlContent !!}

3. Make Sure to Load with Correct Permissions

Make sure the file is readable in your deployment and that the path is correct (base_path(), package_path(), or resource_path() as suits your package structure).


4. Alternative: Custom Blade Component

If you plan to use this often, make a Blade component:

// app/View/Components/MarkdownFile.php

namespace App\View\Components;

use Illuminate\View\Component;
use Illuminate\Support\Str;

class MarkdownFile extends Component
{
    public $path;
    public $content;

    public function __construct($path)
    {
        $markdownPath = base_path($path);
        $markdown = file_exists($markdownPath)
            ? file_get_contents($markdownPath)
            : '# File not found';
        $this->content = Str::markdown($markdown);
    }

    public function render()
    {
        return function (array $data) {
            return $this->content;
        };
    }
}

And register this component, then in Blade:

<x-markdown-file path="packages/your-vendor/your-package/resources/markdown/overview.md" />

Summary

  • Read the markdown file in PHP.
  • Parse the markdown (using Str::markdown).
  • Echo the HTML in your Blade with {!! !!}.

This way, your core.blade.php stays clean and you can organize docs or guidelines in markdown files outside of your Blade templates.


Let me know if you need more package-specific details, such as for Spatie or Filament structure!

Pixelairport's avatar
Pixelairport
OP
Best Answer
Level 12

I just found out I can replace core.blade.php with core.md ... that also works.

Please or to participate in this conversation.