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!