Extract frontmatter metadata
Goal: Pull a document’s YAML frontmatter out as structured data, e.g. to read title/tags for a static-site index.
Prerequisites: None.
Query
.yaml | frontmatter
$ mq '.yaml | frontmatter' post.md
Input
---
title: Hello
tags: [a, b]
---
# Body
Output
{"title": "Hello", "tags": ["a", "b"]}
Notes
- Grab a single field by chaining
get("title"), e.g..yaml | frontmatter | get("title"). A plain.titleselector won’t work, since that’s a Markdown-node selector, not a dict-key accessor. Bracket access also works (frontmatter()["title"]), but only with the parentheses kept.frontmatter["title"](no parens) tries to index the function itself and errors. - Gotcha:
-F json/-F csvdon’t give a clean metadata index here. Since the result is coming from a Markdown document, mq wraps the dict in a synthetic node before serializing it (e.g.-F jsongives{"type": "Text", "value": "{\"title\": ...}"}instead of the dict’s fields directly). Default output doesn’t have this problem:mq '.yaml | frontmatter' docs/**/*.mdprints one JSON object per file, one per line, which you can pipe intojq -sto build an array, or process as JSON Lines directly.