bliki & updating Jekyll post dates
talks about: bliki, Jekyll, dates, Git, and hooksIn case you decided to run your bliki[1] using Jekyll[2], consider using a Git[3] pre-commit hook[4] to update the date entries in the front matter of your posts. E.g. you want to modify the following front matter
---
layout: post
title: bliki & updating Jekyll post dates
tags: [bliki, Jekyll, dates, Git, hooks]
date: 2016-03-28
---
by updating its date entry with the current date to get
---
layout: post
title: bliki & updating Jekyll post dates
tags: [bliki, Jekyll, dates, Git, hooks]
date: 2016-04-02
---
on each commit to the Git repository that contains your Jekyll sources. In order to automate this process, follow these steps:
-
Create a file called
pre-commmit
inside the.git/hooks
directory of your repository-
touch .git/hooks/pre-commit
-
-
Mark it as executable
-
chmod +x .git/hooks/pre-commit
-
-
Place the following code in it:
#!/bin/bash
# pattern for any ISO8601 compatible date pattern
iso_date="[0-9][0-9][0-9][0-9]*-[0-9][0-9]*-[0-9][0-9]"
# pattern for any date in the front matter of a post
any_date="date\: $iso_date"
# Current date in ISO8601
now=`date +%Y-%m-%d`
# today's replacement for the above pattern
today="date\: $now"
# start- and end-delimiters of a post front matter
front_matter="---"
# project relative paths
modified_files=`git diff --name-only HEAD`
for file in $modified_files; do
# only modify Jekyll posts
if [[ $file == _posts/*.asciidoc ]]; then
# extract current post date from front matter (first match)
post_date=`grep -m 1 -o "$any_date" "$file" | head -1 | grep -o "$iso_date"`
# only modify posts that are not up-to-date yet
if [[ $post_date != $today ]]; then
# update the front matter of the post with today's date
sed -i -e "/$front_matter/,/$front_matter/s/$any_date/$today/" "$file"
# add the updated file to the git index so it becomes part of the current commit
git add "$file"
fi
fi
done
exit
This hook only works on posts that have a date entry in their front matter. |
OSX users have to replace the sed call with: sed -i -e "s/$any_date/$today/1" "$file" [5]
|
Once these steps are completed, every committed update to a post will set its date entry to the current date. The file modification will not trigger another commit, instead the change will be part of the soon-to-be-commit that triggered the pre-commit
hook.
Others came up with similar[6] solutions.