Customization
Customizing Prezet Front Matter
Prezet makes it easy to customize the frontmatter of your markdown files. To learn more about how Prezet uses frontmatter data, see this article: Frontmatter Data.
This guide will walk you through the process of extending the package's default FrontmatterData
class and updating config/prezet.php
to use your custom class.
#The Default FrontmatterData Class
Out of the box, Prezet uses the bundled FrontmatterData
class to define and validate the structure of your frontmatter. You can find the contents of the default class here: FrontmatterData.php.
For type safety, this class makes use of the laravel-validated-dto package. For more advanced customization options, you may want to refer to the package's documentation.
#Customizing the FrontmatterData Class
Customizing front matter allows you to tailor your blog's metadata to your specific needs. You can add fields for author information, custom taxonomies, or any content-specific data beyond Prezet's defaults by following the steps below.
#1. Create a Custom FrontmatterData Class
Create a new class that extends the default FrontmatterData
class and add it to your application, for example: app/Data/CustomFrontmatterData.php
:
1<?php 2 3namespace App\Data; 4 5use BenBjurstrom\Prezet\Data\FrontmatterData; 6use WendellAdriel\ValidatedDTO\Attributes\Rules; 7 8class CustomFrontmatterData extends FrontmatterData 9{10 // Add new properties11 #[Rules(['required', 'string'])]12 public string $author;13}
In this example, we've added an author
field
#2. Update the Prezet Configuration File
Update the data.frontmatter
key within the config/prezet.php
file so Prezet knows to use your custom class:
1return [2 // ... other config options ...3 4 'data' => [5 'frontmatter' => App\Data\CustomFrontmatterData::class,6 ],7 8 // ... rest of the config ...9];
#3. Update the Prezet Index
After customizing your front matter, it's crucial to update the Prezet index:
1php artisan prezet:index
This command updates the SQLite index with your new front matter structure, ensuring Prezet recognizes and can use your custom fields.
For more information about the SQLite index and its purposes, refer to the SQLite Index documentation.
#Using Your Custom Frontmatter
Now that you've customized the frontmatter, you can use the new structure in your markdown files:
1--- 2title: My Custom Post 3slug: my-custom-post 4date: 2024-05-10 5author: Jane Doe 6category: Technology 7tags: [php, laravel, prezet] 8excerpt: This is an optional excerpt for my custom post. 9draft: false10---11 12Your markdown content goes here...
To display your custom front matter properties in your blog, you'll need to update the blade views to include them. See the Customizing Blade Views documentation for more information.