Is there any way to generate YAML front matter block of each markdown files automatically?

The answer is yes! And there is more than one way to do it.

I want to introduce two ways, one is using rake tool, the other is using bash.

Rake tool

To generate YAML front matter blockk in markdown file, we need a program that automatically write a markdown file with YAML front matter block.

Here is the program tool “Rake”.

Rake is a Make-like program implemented in Ruby. Tasks and dependencies are specified in standard Ruby syntax.

Maybe you have some questions:

What is Make-like program ?

In software development, Make is a utility that automatically builds executable programs and libraries from source code by reading files called makefiles which specify how to derive the target program.

Makefiles are special format files that together with the make utility will help you to automagically build and manage your projects.

Do we need to write a makefile for Rake?

Of course. We need write a “Rakefile” for Rake utility.

Rakefiles (rake’s version of Makefiles) are completely defined in standard Ruby syntax. No XML files to edit. No quirky Makefile syntax to worry about (is that a tab or a space?)

Using Rake to generate YAML front matter block, step by step:

1.install Rake

gem install rake

Here is the simple usage of rake.

2.write Rakefile

Here is the Rakefile Format.

Tl;dr: Just see below.

require 'time'
# Usage: rake post title="A Title" date="2014-04-14" ## date is alternatively part
desc "Create a new post"
task :post do
    abort("rake aborted: '_posts' directory not found.")
  title = ENV["title"] || "new-post"
  slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
    date = (ENV['date'] ? Time.parse(ENV['date']) :
  rescue Exception => e
    puts "Error: date format must be YYYY-MM-DD!"
    exit -1
  filename = File.join('.', '_posts', "#{date}-#{slug}.md")
  if File.exist?(filename)
    abort("rake aborted: #{filename} already exists.")
  puts "Creating new post: #{filename}"
  open(filename, 'w') do |post|
    post.puts "---"
    post.puts "published: true" 
    post.puts "layout: post"
    post.puts "title: #{title.gsub(/-/,' ')}"
    post.puts "author: Yu" # change to your name
    post.puts "category:"
    post.puts "tags:"
    post.puts "---"

You can save those code in a Rakefile, and move the Rakefile to a jekyll website folder, for example When build a new post, we only need type rake post title="Hello, World" on terminal in the jekyll website folder.

BASH script

If you do not like rake tool, you can use bash to generate YAML front matter block.

Here is the script:

read -p "Enter the title: " title 
Title=`echo $title | tr -d '[:punct:]'`
for word in $Title
filename="`date +%Y-%m-%d`${dashedTitle}.md"
touch $filename
echo "---" >> $filename
echo "published: true" >> $filename
echo "layout: post" >> $filename
echo "title: \"${title}\"" >> $filename
echo "author: Yu" >> $filename
echo "categories:" >> $filename
echo "tags:" >> $filename
echo "-" >> $filename
echo "---" >> $filename
echo "" >> $filename