Skip to content

Instantly share code, notes, and snippets.

@ichadhr
Last active February 8, 2024 10:27
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save ichadhr/0b4e35174c7e90c0b31b to your computer and use it in GitHub Desktop.
Save ichadhr/0b4e35174c7e90c0b31b to your computer and use it in GitHub Desktop.
Jekyll create post with command

CREATE JEKYLL POSTS FROM THE COMMAND LINE

I got tired on creating new files manually for each new post a write so I put together this little command line task with Thor.

It creates a new file in the _posts directory with today’s date, parses the parameters to command as the post’s title and adds that as a slug to the new file. It then writes a default yaml template to the file (as specified in the script).

Running thor jekyll:new New and shiny post will for example create the file _posts/2012-12-28-new-and-shiny-post.markdown, populate it with an yaml template and finally open the file in my favorite editor.

HOW TO

Add the following to your Gemfile:

gem 'thor'
gem 'stringex'

Run bundle install and create a jekyll.thor file with the following contents:

require "stringex"
class Jekyll < Thor
  desc "new", "create a new post"
  method_option :editor, :default => "subl"
  def new(*title)
    title = title.join(" ")
    date = Time.now.strftime('%Y-%m-%d')
    filename = "_posts/#{date}-#{title.to_url}.markdown"

    if File.exist?(filename)
      abort("#{filename} already exists!")
    end

    puts "Creating new post: #{filename}"
    open(filename, 'w') do |post|
      post.puts "---"
      post.puts "layout: post"
      post.puts "title: \"#{title.gsub(/&/,'&amp;')}\""
      post.puts "tags:"
      post.puts " -"
      post.puts "---"
    end

    system(options[:editor], filename)
  end
end

Use the new command:

$ thor jekyll:new The title of the new post

You can even specify which editor to open the new file with:

$ thor jekyll:new The title of the new post --editor=vim

The default editor is Sublime Text 2, just change on line 4 in jekyll.thor if an other editor is preferred.

@ZeroLiam
Copy link

thank you! very useful!

@efojs
Copy link

efojs commented Nov 7, 2019

Thank you! I wonder why isn't it built in. Or is it?

@bhaveshpp
Copy link

bhaveshpp commented May 23, 2021

Great idea good job!
Thank you.

@bhaveshpp
Copy link

How can I add another file-like category inside the category directory?
Ex:

 open(filename, 'w') do |post|
      post.puts "---"
      post.puts "layout: posts_by_category"
      post.puts "categories: composer"
      post.puts "title: Composer"
      post.puts "permalink: /category/composer"
      post.puts "---"
    end

Thank you.

@Merovex
Copy link

Merovex commented Feb 7, 2024

Why use Thor when Rake is available?

# Define a Rake task for creating a new Jekyll post
namespace :blog do
  desc 'Create a new Jekyll post'
  task :new, [:title] do |_, args|
    title = args[:title]
    date = Time.now.strftime('%Y-%m-%d')
    filename = "_posts/#{date}-#{title.downcase.gsub(/\s+/, '-')}.md"

    abort("#{filename} already exists!") if File.exist?(filename)

    # Create a new post file with default front matter
    File.open(filename, 'w') do |file|
      file.puts '---'
      file.puts "layout: post"
      file.puts "title: #{title}"
      file.puts "date: #{date}T00:00:00Z"
      file.puts 'categories: []'
      file.puts 'tags: []'
      file.puts '---'
    end

    puts filename
  end
end

Then from the command line: rake "blog:new[test article]" | xargs code since you're already on the command line. If you are using zshrc, you can alias that with

alias blog='f() { local lower=$(echo "$1" | tr "[:upper:]" "[:lower:]"); rake "blog:new[$lower]" | xargs code; unset -f f; }; f'

and just type blog "New Post".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment