Managing Multiple Drafts Easily in Jekyll

Despite of only being a static website, we can utilize a plugin in Jekyll too. Let’s continue our discussion on creating drafts from the previous post. As you can see before – in the previous post – we created the drafts and put them within a special folder called _drafts first.

Then, as we are ready to publish it, we move them to the _posts folder with the proper naming format.

It sounds easy right? it should be no problem if you’d only manage 1 or 2 post drafts. But, when you have 5-10 post drafts, changing each file name, and specifying the correct date by hand can be a pain. Let’s take a look how we can simplify the workflow with a Jekyll plugin.

Getting Started

Before we go any further, let’s create a new folder named _plugins; this folder is required as Jekyll will search and execute plugins from within the folder. We also need to create a new file named publisher.rb in it; technically, you can freely name that file with any name you like.

We will be using a Jekyll plugin created by Jeffrey Sambells. This plugin will take care of the hassle when publishing a post from a draft in Jekyll. It will rename the file properly, along with the date. And it will also specify the date within the post Front Matter section.

The following is the source code of the plugin, available from this Gist page. Copy this code below and paste it to the publisher.rb file that we have just created.

 module Jekyll class PostPublisher < Generator safe false def replace(filepath, regexp, *args, &block) content = File.read(filepath).gsub(regexp, *args, &block) File.open(filepath, 'wb') { |file| file.write(content) } end def generate(site) @files = Dir["_publish/*"] @files.each_with_index { |f,i| now = DateTime.now.strftime("%Y-%m-%d %H:%M:%S") replace(f, /^date: unpublished/mi) { |match| "date: \"" + now + "\"" } now = Date.today.strftime("%Y-%m-%d") File.rename(f, "_posts/#{now}-#{File.basename(f)}") } end end end 

If your Jekyll server is currently running, restart it for the plugin to work.

Using the Plugin

To use the plugin, we need to create a new folder named _publish. We will move our post drafts to this folder, once we are ready to publish it. However, before doing so, set the date in the post draft’s front matter to unpublished, like so

 --- layout: post title: "This is My Second Post" date: unpublished --- 

Now, move the draft to the _publish folder.

jekyll-plugin/

As mentioned, Jekyll will automatically move the folder to _posts as well as set the post date, then publish the post for you.

Final Thought

We have seen that Jekyll is extensible with a plugin. In this post, for instance, we used one to simplify the process of publishing a draft. You can find more Jekyll plugins in this page: Available Plugins.

Now that we have learned how to setup Jekyll, and publish a post draft. In the next post, we will show you how to publish Jekyll blog to an online server via FTP. Stay tuned!




via hongkiat.com http://ift.tt/1jsN79z