How to Manage Multiple Drafts in Jekyll

Although Jekyll is a static website generator, it allows for the use of plugins to extend its functionality. Continuing our discussion from the previous post, we previously created drafts and placed them in a special folder named _drafts.

When ready to publish, we move these drafts to the _posts folder and ensure they follow the proper naming format.

This process is straightforward for managing one or two drafts, but handling 5-10 drafts can be cumbersome. Renaming files and specifying the correct dates manually can be tedious. Let’s explore how we can simplify this workflow using a Jekyll plugin.

Getting Started

First, create a new folder named _plugins. Jekyll searches and executes plugins from within this folder. Next, create a file named publisher.rb in this folder. You can choose any name for this file.

We will use a Jekyll plugin created by Jeffrey Sambells. This plugin streamlines the process of publishing drafts by properly renaming files, setting the date, and updating the Front Matter section of the post.

Copy the following code from this Gist page and paste it into the publisher.rb file:

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 do |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
end

If your Jekyll server is running, restart it to apply the plugin.

Using the Plugin

To use the plugin, create a new folder named _publish. Move your post drafts to this folder when ready to publish. Set the date in the draft’s Front Matter to unpublished as shown below:

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

Next, move the draft to the _publish folder.

Moving draft to _publish folder

Jekyll will automatically move the file to the _posts folder, set the post date, and publish the post.

Published post in _posts folder

Final Thoughts

We’ve seen how Jekyll can be extended with plugins. In this post, we used a plugin to simplify the process of publishing drafts. For more Jekyll plugins, visit the Available Plugins page.

In the next post, we’ll demonstrate how to publish a Jekyll blog to an online server via FTP. Stay tuned!

WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail