Automating Promotion of Jekyll Posts from Draft to Post

When I used to use Jekyll, I had a workflow where a post would start as a draft, sitting in the _drafts folder, and once it was ready, I would "promote" it to a full-blown post.

This started to get a bit painful, so I automated the process with a little helper script.

Although this is fairly straightforward, the bit that I was really proud of was being able to use my shell's auto-complete to make this an easier process for me.

For instance:

$ ls _drafts
test.md  test2.md
$ jekyll_promote <TAB>
# auto-completes the `test`
$ jekyll_promote test<TAB>
completing file
test2.md  test.md

You can see the below Asciicast for a more visual example of the flow:

Let's assume that we have a function jekyll_promote which performs the following:

jekyll_promote() {
	git mv "_drafts/$1" "_posts/$(date -I)-$1"
}

Zsh

I'm running Zsh as my shell, which has a really straightforward autocomplete syntax which tells it to just autocomplete the files in the _drafts directory:

compdef '_path_files -W $PWD/_drafts' jekyll_promote

Bash

However, I know that lots of people are not using Zsh but instead use Bash, so I've also created the following autocomplete magic for Bash. This is a little more involved, and adapts a StackOverflow answer of a similar type:

# Adapted from https://askubuntu.com/a/707643
_jekyll_promote () {
	local cur
	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	k=0
	drafts_dir="$PWD/_drafts"
	for draft in $( compgen -f "$drafts_dir/$cur" ); do
		[[ -d "$draft" ]] && continue
		COMPREPLY[k++]=${draft#$drafts_dir/} # remove the directory prefix from the array
	done
	return 0
}

complete -o nospace -F _jekyll_promote jekyll_promote

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the GNU General Public License v3.0 or later.

#blogumentation #automation #jekyll #zsh #bash #shell.

This post was filed under articles.

Has this content helped you? Please consider supporting me so I can continue to create content like this!

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.