Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove YAML frontmatter from markdown files?

I have markdown files that contain YAML frontmatter metadata, like this:

---
title: Something Somethingelse
author: Somebody Sometheson 
---

But the YAML is of varying widths. Can I use a Posix command like sed to remove that frontmatter when it's at the beginning of a file? Something that just removes everything between --- and ---, inclusive, but also ignores the rest of the file, in case there are ---s elsewhere.

like image 762
Jonathan Avatar asked Jan 29 '15 18:01

Jonathan


3 Answers

I understand your question to mean that you want to remove the first ----enclosed block if it starts at the first line. In that case,

sed '1 { /^---/ { :a N; /\n---/! ba; d} }' filename

This is:

1 {              # in the first line
  /^---/ {       # if it starts with ---
    :a           # jump label for looping
    N            # fetch the next line, append to pattern space
    /\n---/! ba; # if the result does not contain \n--- (that is, if the last
                 # fetched line does not begin with ---), go back to :a
    d            # then delete the whole thing.
  }
}
                 # otherwise drop off the end here and do the default (print
                 # the line)

Depending on how you want to handle lines that begin with ---abc or so, you may have to change the patterns a little (perhaps add $ at the end to only match when the whole line is ---). I'm a bit unclear on your precise requirements there.

like image 106
Wintermute Avatar answered Dec 15 '22 00:12

Wintermute


If you want to remove only the front matter, you could simply run:

sed '1{/^---$/!q;};1,/^---$/d' infile

If the first line doesn't match ---, sed will quit; else it will delete everything from the 1st line up to (and including) the next line matching --- (i.e. the entire front matter).

like image 42
don_crissti Avatar answered Dec 15 '22 01:12

don_crissti


If you don't mind the "or something" being perl.

Simply print after two instances of "---" have been found:

perl -ne 'if ($i > 1) { print } else { /^---/ && $i++ }' yaml

or a bit shorter if you don't mind abusing ?: for flow control:

perl -ne '$i > 1 ? print : /^---/ && $i++' yaml

Be sure to include -i if you want to replace inline.

like image 37
Ben Grimm Avatar answered Dec 14 '22 23:12

Ben Grimm