How to remove YAML frontmatter from markdown files

2019-06-25 05:43发布

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.

4条回答
来,给爷笑一个
2楼-- · 2019-06-25 06:07

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.

查看更多
Animai°情兽
3楼-- · 2019-06-25 06:21

If you want to remove the front matter and 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).

查看更多
小情绪 Triste *
4楼-- · 2019-06-25 06:28

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.

查看更多
男人必须洒脱
5楼-- · 2019-06-25 06:28

you use a bash file, create script.sh and make it executable using chmod +x script.sh and run it ./script.sh.

#!/bin/bash

#folder articles contains a lot of markdown files
files=./articles/*.md

for f in $files;
do
    #filename
    echo "${f##*/}"
    #replace frontmatter title attribute to "title"
    sed -i -r 's/^title: (.*)$/title: "\1"/' $f
    #...
done
查看更多
登录 后发表回答