Skip to main content

Pelican YAML frontmatter with UltiSnips

Learn how to automate YAML frontmatter generation for Pelican blogs using UltiSnips in Vim.

Introduction

When working with Pelican for static site generation, the choice between standard Markdown metadata and YAML frontmatter comes down to readability and maintainability. YAML frontmatter provides a cleaner, more structured approach to defining content metadata. This article explains how to set up and automate YAML frontmatter in Pelican using Vim's UltiSnips plugin.

Prerequisites

  • Basic knowledge of Vim
  • Pelican installed and configured
  • UltiSnips plugin installed in Vim

Setting up Pelican for YAML frontmatter

Pelican doesn't support YAML frontmatter out of the box. To enable this functionality, we need to install and configure the FrontMark plugin.

Installing FrontMark

The FrontMark plugin extends Pelican to parse YAML frontmatter in Markdown files. Install it using pip:

pip install pelican-frontmark

Configuring Pelican

Add FrontMark to your Pelican configuration file (pelicanconf.py):

MARKUP = ('frontmark',)

This tells Pelican to use the FrontMark parser for your content files.

Automating frontmatter with UltiSnips

Manually typing YAML frontmatter for each article becomes tedious. UltiSnips can significantly streamline this process by generating the frontmatter structure with pre-populated values.

Creating the snippet

Add the following snippet to your UltiSnips Markdown snippet file (typically ~/.vim/UltiSnips/markdown.snippets):

snippet meta
---
title: $1`!p from re import sub
if not snip.c:
	snip.rv = re.sub('-',' ',snip.basename.capitalize())
`
date: `!p from datetime import datetime
if not snip.c:
    snip.rv=datetime.now().strftime("%Y-%m-%d %H:%M%z")`
categories: [$2]
tags: [$3]
slug: $4`!p
if not snip.c:
	snip.rv = snip.basename
`
author:
summary: $5
lang:
status: draft
---

$6
endsnippet

Understanding the snippet

This snippet leverages Python interpolation to:

  1. Generate a title from the filename if none is provided
  2. Insert the current date and time
  3. Create placeholders for categories and tags
  4. Generate a slug from the filename
  5. Add placeholders for summary and content

Using the snippet

When creating a new Markdown file for your Pelican blog:

  1. Type meta and press your UltiSnips expansion key (typically Tab)
  2. The snippet will expand with auto-populated fields where possible
  3. Navigate through tab stops to fill in the remaining fields

Note

Pelican will not process articles with empty meta values. Either fill in all fields or remove empty ones to ensure your content is properly processed.

Enhancing the workflow

Integration with modern frontend tools

If you're using Alpine.js or Tailwind CSS in your Pelican theme, you might want to extend your snippets to include common patterns. For example:

snippet alpinejs
<div x-data="{ open: false }">
    <!-- Your Alpine.js component here -->
</div>
endsnippet

Custom metadata fields

Consider adding custom fields to your frontmatter for advanced Pelican features:

---
title: "Article title"
featured_image: "path/to/image.jpg"
estimated_reading_time: 5
table_of_contents: true
---

Conclusion

Using UltiSnips to generate YAML frontmatter for Pelican provides a streamlined content creation workflow. The combination of FrontMark and UltiSnips allows you to focus on writing content rather than configuring metadata.

For more advanced usage, consider creating additional snippets for common content patterns in your blog posts, such as code blocks with your preferred languages or alert components based on your style guide.

Next steps

  • Explore other Pelican plugins that work well with YAML frontmatter
  • Create custom UltiSnips snippets for your most common content patterns
  • Consider automating publication workflows with shell scripts or Ansible