Reading front matter without a YAML library

Two markers, one colon per line, and a date check. That is the whole parser, and it fits in a screen.

A post needs a title, a date, and a few optional fields. That is close enough to a config file that pulling in a YAML parser feels reasonable. It is not. The format here is smaller than YAML, and the parser is smaller than the dependency.

A dark placeholder card reading Front matter

The format

---
title: "Reading front matter without a YAML library"
date: 2026-10-20
description: "One sentence summary."
tags: ["go", "markdown"]
draft: false
---

The first line must be ---. The block ends at the next ---. Each line splits at the first colon, and one pair of surrounding quotes is stripped.

A dark placeholder panel showing the front matter block

The parser

Two rules keep it honest: it accepts exactly the shapes it documents, and it fails loudly on anything else.

func parse(src []byte) (meta, error) {
	lines := strings.Split(string(src), "\n")
	if len(lines) == 0 || strings.TrimSpace(lines[0]) != "---" {
		return meta{}, errors.New("has no front matter")
	}
	m := meta{}
	for i := 1; i < len(lines); i++ {
		line := strings.TrimSpace(lines[i])
		if line == "---" {
			return m, m.check()
		}
		key, value, ok := strings.Cut(line, ":")
		if !ok {
			return meta{}, fmt.Errorf("line %d is not key: value", i+1)
		}
		switch strings.TrimSpace(key) {
		case "title":
			m.Title = unquote(value)
		case "date":
			m.Date = strings.TrimSpace(value)
		}
	}
	return meta{}, errors.New("front matter is not closed")
}

strings.Cut is the whole trick. It splits at the first colon, so a title that contains a colon still works as long as it is quoted.

Why not YAML

YAML is a data language with anchors, multi document streams, and a small type system. A post header uses none of that. Every feature the parser does not support is a feature a writer cannot accidentally depend on, which makes the failure mode predictable.

The best format for a header is the one whose error message the writer can act on without opening a spec.

What it rejects, and why that helps

Mistake Error
Missing opening marker has no front matter
Missing closing marker front matter is not closed
Line with no colon line N is not key: value
Empty title has no title
Date that is not YYYY-MM-DD date must be YYYY-MM-DD

A bad post stops the server at startup instead of rendering a broken page. Set a date format once and every list sorts correctly without a locale library.

Back to all posts