I’m tired of manually creating a new Jekyll blog post which for me included copying an existing post, renaming the file
with today’s date and the title of the post, updating the title
attribute of the markdown file, and deleting the
contents of the copied post.
That workflow ends today.
I’ve created a script (scripts/new-post.sh
):
#!/usr/bin/env bash
set -e
repo_folder=`git rev-parse --show-toplevel`
category=$1
title=$2
timestamp=`date "+%F %T %z"`
date_value=`date "+%F"`
file=`echo $title | awk '{print tolower($0)}' | sed -e 's/[^a-zA-Z0-9]/-/g'`
file_path="$repo_folder/_posts/$date_value-$file.md"
echo "---
layout: post
title: '$title'
date: $timestamp
categories: $category
tags: $category
---
" > $file_path
echo $file_path
Usage:
scripts/new-post.sh <category> "<title>"
It takes two arguments. category
and title
. Your title will probably contain spaces so you’ll want to generally wrap
your blog post title in quotes.
Example:
scripts/new-post.sh blogging "My New Post"
I’ve decided to write the new file path to stdout
so that I can command
+ click the file name to quickly start
editing in Visual Studio Code.
Demo:
Edit:
Since writing this post, I found out that there is a plugin jekyll-compose
which provides this functionality as well as more. I plan to stick with this script though. Its one less dependency, and
does everything I need.