YAML frontmatter, also known as YFM, is a common convention used in static site generators like Jekyll and content management systems like GitHub Docs for adding metadata to files. It's a block of valid YAML placed at the beginning of a file, typically enclosed by triple-dashed lines (---).
Here's a breakdown of how it works:
- Metadata: YAML frontmatter is primarily used to store metadata about the file, such as the title, author, date, categories, tags, and more.
- Structure: It follows the YAML syntax, using key-value pairs to organize the metadata.
- Position: It must be the very first thing in the file, preceding any content.
- Delimiters: The frontmatter is marked by three dashes (---) at the start and end.
- Example:
--- layout: post title: My Blog Post date: 2024-07-28 author: Your Name tags: - markdown - yaml ---
Why use YAML frontmatter?
- Content Management: It helps organize and manage content by providing structured information about each file.
- Static Site Generation: Static site generators utilize YAML frontmatter to control aspects like page layouts, rendering, and dynamic routing.
- Metadata for Tools: Tools like Obsidian and Zettlr use YAML frontmatter to enhance document features and manage information.
- Flexibility: It allows you to define custom variables and use them within the file's content or in templates.
Important Considerations:
- Valid YAML: Ensure your frontmatter is valid YAML; otherwise, it may break your website or tools.
- Indentation: Use spaces for indentation, not tabs, as YAML is sensitive to whitespace.
- Case Sensitivity: YAML keys are case-sensitive.
- Meaningful Keys: Choose descriptive names for your keys to improve readability.
YAML frontmatter is a powerful tool for adding metadata and control to files, particularly within the context of static site generators and content management systems.