kislyuk / yq

Command-line YAML, XML, TOML processor - jq wrapper for YAML/XML/TOML documents
https://kislyuk.github.io/yq/
Apache License 2.0
2.53k stars 81 forks source link

option to parse only first YAML block in file #151

Open casperdcl opened 1 year ago

casperdcl commented 1 year ago

Many markdown files start with a YAML header block. Would be nice for yq to support them.

current behaviour

Currently yq complains about the markdown:

$ cat doc.md
---
title: Foo
author: [Bar]
---
Some *Example* markdown

$ yq .title doc.md
"Foo"
jq: error (at <stdin>:2): Cannot index string with string "title"

desired behaviour

Option to parse only the first document per filestream.

$ yq --blocks=1 .title doc.md
"Foo"

alternative

$ awk '{ if ($1 == "---" || $1 == "...") n+=1; if(n>1) exit 0; print; }' doc.md | yq .title