-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew
executable file
·60 lines (48 loc) · 1.28 KB
/
new
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
# This script creates a new post or draft in jekyll with pre-filled front matter.
# Run `./new <some title for your post>` to generate a post.
# Run `./new -d <title AFTER -d flag>` for a draft.
ISDRAFT=false
while getopts "d" opt; do
case $opt in
d)
ISDRAFT=true
;;
esac
done
shift $((OPTIND-1))
TITLE=""
for word in $@
do
# `sed` to replace all chars except alphanum or - with empty string.
alphanumericword=$(echo $word | sed "s/[^A-Za-z0-9-]//g")
# ${alphanumericword,,} <-- This converts the word to lowercase.
TITLE+="-${alphanumericword,,}"
done
FILEDIR="posts"
FILENAME="$FILEDIR/$(date +%Y-%m-%d)$TITLE.md"
# Insert frontmatter in post
echo "---" > $FILENAME
echo "layout: post" >> $FILENAME
if [ $# -gt 0 ]
then
echo "title: \"$@\"" >> $FILENAME
fi
echo "tags: DON'T FORGET!!" >> $FILENAME
if $ISDRAFT; then
echo "draft: true" >> $FILENAME
fi
echo "---" >> $FILENAME
echo "" >> $FILENAME
# Don't forget to put the marker to hide some of the post on the front page :)
echo "<!--more-->" >> $FILENAME
# Write the post
if command -v nvim >/dev/null 2>&1
then
nvim $FILENAME
else
vi $FILENAME
fi
# Useful post-post stuff
git add -N $FILENAME
echo "New post \"$FILENAME\" has been added to git tracked files"