Features

Typed Front Matter

Front matter is the YAML metadata at the top of a markdown file enclosed by triple dashes (---). Prezet converts front matter into a strongly typed data transfer object (DTO) to ensure consistency and reliability.

As an example, here is the front matter for this very post:

1---
2title: Typed Front Matter
3date: 2024-05-06
4category: Features
5excerpt: 'Prezet uses typed front matter for robust content management.'
6image: '/prezet/img/ogimages/example.png'
7---

#How Prezet Uses Front Matter

Prezet utilizes front matter in several ways:

  1. Content Display: Generates visible elements like titles, dates, and excerpts for your blog.
  2. SEO Optimization: Renders SEO tags in the page's <head>, including title, description, and Open Graph attributes.
  3. Content Organization: Uses categories, tags, and other metadata for structuring and organizing your content.

#Validating Front Matter

To ensure the integrity and consistency of your content, Prezet employs the FrontmatterData class to define and validate the structure of your front matter. This class uses the laravel-validated-dto package for type safety and validation.

You can find the default FrontmatterData class here: FrontmatterData.php

The current FrontmatterData class includes the following fields:

1class FrontmatterData extends ValidatedDTO
2{
3 #[Rules(['required', 'string'])]
4 public string $slug;
5 
6 #[Rules(['required', 'string'])]
7 public string $title;
8 
9 #[Rules(['required', 'string'])]
10 public string $excerpt;
11 
12 #[Rules(['nullable', 'string'])]
13 public ?string $category;
14 
15 #[Rules(['array'])]
16 public array $tags;
17 
18 #[Rules(['nullable', 'string'])]
19 public ?string $image;
20 
21 #[Rules(['bool'])]
22 public bool $draft;
23 
24 #[Rules(['required'])]
25 public Carbon $createdAt;
26 
27 #[Rules(['required'])]
28 public Carbon $updatedAt;
29 
30 // ... other methods ...
31}

If the front matter is missing or does not pass validation, Prezet will throw an error and prevent the post from being rendered.

#Bulk Front Matter Validation

Prezet provides a convenient way to validate the front matter for all of your posts at once. Anytime you update the Prezet index SQLite file, Prezet also scans all your markdown files and checks that the front matter can be rendered into a valid DTO.

1php artisan prezet:index

If any files contain invalid front matter, the command will output detailed error messages along with the file paths, allowing you to quickly identify and correct any issues.

#Understanding Front Matter Fields

  • slug: A unique identifier for your post, used in the URL.
  • title: The title of your post.
  • excerpt: A brief summary of your post, used in previews and SEO.
  • category: An optional category for your post.
  • tags: An array of tags associated with your post.
  • image: An optional image associated with your post.
  • draft: A boolean indicating whether the post is a draft (not publicly visible).
  • createdAt: The creation date of the post.
  • updatedAt: The last update date of the post.

#Customizing Front Matter

While Prezet provides a default structure for front matter, you can customize it to fit your specific needs. For detailed instructions on how to extend or modify the FrontmatterData class, please refer to the documentation on Customizing front matter.

By leveraging typed front matter, Prezet helps you maintain a robust and consistent content structure, enhancing both the reliability of your blog and the efficiency of your content management workflow.