Skip to content

Commit 264ec1f

Browse files
committed
Add post about Jekyll
1 parent eced2aa commit 264ec1f

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
layout: post
3+
title: Create a New Jekyll Post with a Simple Shell Command
4+
description: If you are lazy like me you need something to automate a new post for Jekyll
5+
categories: ["Jekyll", "Coding"]
6+
---
7+
8+
Let's build a shell command to automate the creation of a new post in Jekyll.
9+
10+
This will be a short one.
11+
12+
Jekyll has its quirks, and I always wondered why they don't have a simple command to create a new post. I used to copy/paste the previous one, but you know how things are... If you can automate something boring in a few minutes, just do it.
13+
14+
## Creating the Script
15+
We first need to create a simple bash script to do the job for us.
16+
17+
So, create a new file. I call mine `newjekyllpost.sh` and add the following parts to it:
18+
19+
```bash
20+
#!/bin/bash
21+
filename=`date +%Y-%m-%d-new-post.md`
22+
23+
cat > $filename <<EOF
24+
---
25+
layout: post
26+
title: A title
27+
description: A description
28+
categories: ["tag"]
29+
social_image: add here
30+
---
31+
EOF
32+
```
33+
34+
Let's rubberduck this.
35+
36+
The second line is a variable called `filename`. This will be used for our file name with today's date and the following format:
37+
```
38+
YYYY-MM-DD-new-post.md #
39+
```
40+
41+
The third line will create the file with the contents we like. I chose to add the YAML front matter with the most used content for my posts. If I don't want something, I remove it.
42+
43+
And that's it. Our script is that simple. Let's now try to execute it.
44+
45+
## Making the Script Executable
46+
47+
If you try to run it, you will get an error about permissions. There is one more step, which is to change the file's permissions.
48+
49+
```
50+
chmod u+x ./newjekyllpost.sh
51+
```
52+
53+
After that, running `./newjekyllpost.sh` will create a file in the specific folder with our contents.
54+
55+
## Adding an Alias
56+
57+
Let's do one more last thing to make our lives easier.
58+
59+
Right now, we have our script in a specific folder, so we need to write the path each time, which is also boring. Adding an alias for the script will work like magic.
60+
61+
Move the script to any path you want. I keep a `scripts` folder inside my `Users` folder.
62+
63+
Open the profile your shell is using. I'm using ZSH, so I have this line in my `.zshrc` file:
64+
65+
```
66+
alias njp="~/scripts/newjekyllpost.sh"
67+
```
68+
69+
Every time I run `njp` in a folder, the script creates a new file. You can choose any alias you want, of course.
70+
71+
Run `source pathoftheprofilefile` to reload the new changes, and we are done.
72+
73+
## Improving the Script
74+
75+
As I was writing this, I though there is one more thing we could do to improve the script. If there are argguments to the script use them to postfix the filename.
76+
77+
So our first part of the script becomes:
78+
79+
```
80+
if [ $# -gt 0 ]
81+
then
82+
filename=`date +%Y-%m-%d-`
83+
filename+=`echo "$@" | sed -e 's/ /-/g'`.md
84+
else
85+
filename=`date +%Y-%m-%d-new-post.md`
86+
fi
87+
```
88+
89+
In the "bash" language what that means is:
90+
91+
If I have arguments add the date in the filename. Then join with a space all the arguments as string with `"$@"` and then replace the spaces with `-` which is done by the `sed` part.
92+
93+
If I have no arguments, business as usual.
94+
95+
If we run `njp this is my new post` at 2022-12-30 the file will be create will have the following name:
96+
97+
```
98+
2022-12-30-this-is-my-new-post.md
99+
```
100+
101+
Just remember, all of this can be achieved with any language. The logic is the same. I just chose to do it in a bash script because, why not?

0 commit comments

Comments
 (0)