How to automate jekyll blog posting.
This post is going to be both a tutorial, and a test for the script I am about to show you.
It’s quite a simple script, but saves me a lot of time when it comes to posting.
Now all I have to do is write the markdown file, and run the command blogpost path/to/file.md, and voila! the article should be posted.
Here is the script (feel free to use at and replace the variables).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env bash
# Replace with the name of your blog
REPO_DIR="$HOME/duffparis-dev.github.io"
POSTS_DIR="$REPO_DIR/_posts"
# Get the markdown file from argument
MD_FILE="$1"
# Put the md file in the _posts folder
cp "$MD_FILE" "$POSTS_DIR/"
# Posting
cd "$REPO_DIR"
git pull origin main
git add "_posts/$(basename "$MD_FILE")"
git commit -m "Add post $(basename "$MD_FILE")"
git push origin main
echo "Posted successfully!"
Now you can save this as an sh file and run it like so:
1
./script.sh file.md
But that is rather annoying, as you have to either having the correct working directory or write out the full path.
Alternatively, you can install the script to your PATH:
1
2
3
sudo nano /bin/blogpost
# paste in the script from before and save
chmod +x /bin/blogpost
This way, you can run the script with:
1
blogpost file.md
regardless of your working directory.
Hope you find this helpful!
-Duff
This post is licensed under CC BY 4.0 by the author.