I just got tired of copying pasting the Jekyll front matter and renaming the post file name with the precise date and title. So, I created a small Ruby script that will take the title and generate a post template for me.

1. Create the file

In your Jekyll directory create a file create_post.rb in your ./_posts folder and open in a text editor:

  $ cd _posts
  $ touch create_post.rb
  $ nano create_post.rb

2. Write the script.

Here is the script I wrote.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env ruby

class JekyllHelper
  ESC_CHAR = %w(the a an and in of but if to)

  def self.titalize(input)
    title = input.split(" ").map do |word|
      if !ESC_CHAR.include? word
        word.capitalize
      else
        word
      end
    end

    title.first.capitalize!
    '"' + title.join(" ") + '"'
  end

  def self.create_post(title)
    file = File.open(Time.now.strftime("%Y-%m-%d-" "#{title.downcase.split(" ").join("-")}.markdown"), "w")
    file.puts("--- \nlayout: post \nauthor: Bishal \ntitle: #{self.titalize(title)} \ndate: #{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')} \npermalink: posts/#{title.downcase.split(" ").join("-")}/ \ncategories: \ndescription: \ncomments: true \n---")
    file.close
  end
end

JekyllHelper.create_post(ARGV[0])

Don’t forget to change the author to your name.

3. Run the script

In your project directory cd to _posts folder and run the script passing your post title as parameter.

  $ ruby create_post.rb "How to create posts easily in Jekyll"

Now you will have a new post markdown in your _posts folder with the current date and the front matter template. description and categories fields are left empty for you to fill up.

That’s it! Hope this was helpful.