Customization
Customizing Actions in Prezet
Prezet uses action classes to handle all of it's internal tasks—such as parsing markdown, fetching images, or updating the search index. These classes live in the \BenBjurstrom\Prezet\Actions
namespace and are resolved from the Laravel service container. If you'd like to modify or replace one of these actions, you can do so by overriding the underlying class. This guide will walk you through the process of customizing Prezet's actions to fit your needs.
#Default Action Classes
By default, Prezet ships with a number of action classes, including:
ParseMarkdown
: Handles markdown parsing with CommonMark.GetMarkdown
: Fetches the raw markdown from storage.UpdateIndex
: Updates the SQLite index for searching posts.GetImage
: Returns images from Prezet’s storage disk with optional resizing.GenerateOgImage
: Creates a social media-friendly "Open Graph" image.- … and many others.
Each one is responsible for a single, discrete operation. You can find them in the vendor/benbjurstrom/prezet/src/Actions
directory.
#Using a Helper Method or Facade
Prezet provides a facade with static methods for calling these actions. If you override an action in your application service provider, the facade automatically respects your binding. This means that:
1Prezet::updateIndex();
now calls your custom logic when it resolves UpdateIndex::class
—no code changes required anywhere else.
#Example: Skipping Certain Documents From Indexing
This example demonstrates:
- Extending the front matter with a custom property named
legacy
. - Overriding the
UpdateIndex
action to skip indexing headings for documents marked as legacy.
#1. Create A Custom FrontmatterData
Class
In 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 // We'll use "legacy: true" in the front matter to mark older docs11 #[Rules(['nullable', 'bool'])]12 public ?bool $legacy;13}
You might now have front matter like:
1---2title: "My Old Docs"3date: 2023-01-014legacy: true5draft: false6---
#2. Bind Your Custom Front Matter Class
In your AppServiceProvider
, swap the default FrontmatterData
binding with your own CustomFrontmatterData
:
1use BenBjurstrom\Prezet\Data\FrontmatterData;2use App\Data\CustomFrontmatterData;3 4public function register(): void5{6 $this->app->bind(FrontmatterData::class, CustomFrontmatterData::class);7}
This ensures that any time Prezet resolves the front matter DTO, it uses your version instead.
#3. Override The UpdateIndex
Action
Now, let’s say you only want to skip building headings for “legacy” documents so they aren’t searchable. You can override UpdateIndex
and modify how headings are processed:
1<?php 2 3namespace App\Actions; 4 5use BenBjurstrom\Prezet\Actions\UpdateIndex; 6use BenBjurstrom\Prezet\Models\Document; 7 8class CustomUpdateIndex extends UpdateIndex 9{10 protected function updateHeadings(Document $document, ?string $content): void11 {12 // If the document has "legacy" front matter, skip heading creation13 if ($document->frontmatter->legacy) {14 return;15 }16 17 // Otherwise, run the normal logic18 parent::updateHeadings($document, $content);19 }20}
#4. Register Your Custom Action
Finally, in your AppServiceProvider
:
1use BenBjurstrom\Prezet\Actions\UpdateIndex;2use App\Actions\CustomUpdateIndex;3 4public function register(): void5{6 // ...7 $this->app->bind(UpdateIndex::class, CustomUpdateIndex::class);8}
Now, whenever Prezet::updateIndex()
is called, your CustomUpdateIndex
logic runs. Documents flagged with legacy: true
in front matter will not have their headings processed or indexed, effectively removing them from search queries that rely on headings.
For additional customization ideas, check out: