Customization
Customizing Prezet Front Matter
Prezet makes it easy to customize the front matter of your markdown files. To learn more about how front matter works in general, check out the Typed Front Matter documentation.
This guide walks you through extending Prezet's default FrontmatterData
class so that Prezet recognizes your custom front matter fields.
#The Default FrontmatterData Class
Prezet includes a default FrontmatterData
class that validates essential fields like title
, excerpt
, category
, draft
, and more. You can review the source code here: FrontmatterData.php.
Key properties include:
- title (string)
- excerpt (string)
- category (nullable string)
- image (nullable string)
- draft (boolean)
- date (Carbon instance)
- author (nullable string)
- slug (nullable string)
- key (nullable string)
- tags (array)
Prezet uses the laravel-validated-dto package to ensure the fields in your YAML front matter are strictly typed.
#Adding Custom Fields
#1. Create a Custom Class
In app/Data/CustomFrontmatterData.php
, you might add a legacy
field to identify older docs you'd like to handle differently:
1<?php 2 3namespace App\Data; 4 5use BenBjurstrom\Prezet\Data\FrontmatterData; 6use WendellAdriel\ValidatedDTO\Attributes\Rules; 7 8class CustomFrontmatterData extends FrontmatterData 9{10 // Mark docs as "legacy: true" in the front matter if they should be excluded from certain features11 #[Rules(['nullable', 'bool'])]12 public ?bool $legacy;13}
Then your markdown front matter might look like:
1---2title: "My Old Docs"3date: 2023-01-014legacy: true5draft: false6---
#2. Override the Default in the Service Container
In a service provider (e.g., AppServiceProvider
), bind Prezet's default FrontmatterData
to your custom class:
1<?php 2 3namespace App\Providers; 4 5use Illuminate\Support\ServiceProvider; 6use BenBjurstrom\Prezet\Data\FrontmatterData; 7use App\Data\CustomFrontmatterData; 8 9class AppServiceProvider extends ServiceProvider10{11 public function register(): void12 {13 // Swap out the default front matter data for your custom class14 $this->app->bind(FrontmatterData::class, CustomFrontmatterData::class);15 }16 17 public function boot(): void18 {19 //20 }21}
This ensures that any time Prezet processes front matter, it uses your CustomFrontmatterData
.