This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkpost
executable file
·90 lines (70 loc) · 1.82 KB
/
mkpost
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
# --> WOY: Bash version 4.0+ required!
# --> USING: bash mkpost -b "My New Post Title"
# Check first params (as post type)
if [ -z "$1" ]; then
echo '⚠ Post type (-b or -g) is required'
echo 'example: bash mkpost -b "My New Post Title"'
exit 1
fi
# Validating first params
if [ $1 != "-b" ] && [ $1 != "-g" ]; then
echo '⚠ Invalid params '$1
echo 'accepted: -b for blog or -g for gallery'
exit 1
fi
# Check second params (as post title)
if [ -z "$2" ]; then
echo '⚠ Post title is required'
echo 'example: bash mkpost -b "My New Post Title"'
exit 1
fi
# Set pages directory
PAGES_DIR="src/pages"
# Post type
POST_TYPE="blog"
if [ "$1" == "-g" ]; then
POST_TYPE="gallery"
fi
# Get title from first param
TITLE="$2"
# Format current date
DATE=`date '+%Y-%m-%d %H:%M:%S'`
# Remove character in title expect letters and numbers
TARGET=$(echo $TITLE | tr -d -c '[:alnum:][:space:]')
# Limit only 5 words
TARGET=$(echo $TARGET | sed -r 's@^(\S+\s+\S+\s+\S+\s+\S+\s+\S+).*@\1@')
# Convert title to folder name
TARGET="${TARGET// /-}"
# Make target lowercase for consist folder name
TARGET="${TARGET,,}"
# Append pages directory, type, and target
TARGET="$PAGES_DIR/$POST_TYPE/$TARGET"
# Check if target folder is exists
if [ -d "$TARGET" ]; then
echo '⚠ Post url/directory already exists'
echo "url: $TARGET"
exit 1
fi
# Create folder recursively
mkdir -p "$TARGET"
# Write multiline string to index.md file in target directory
cat > $TARGET/index.md <<- EOM
---
title: $TITLE
date: $DATE
tags:
- example
category: example
cover: example.jpg
---
EOM
# Ask to open index file
read -e -p "Do you wanna open the index.md file 🧐? [y/n]: " ANSWER
if [[ ${ANSWER,,} =~ ^(y|yes|ya|ok)$ ]]; then
echo "🚀"
# Open index.md file with default makrdown editor in environment
xdg-open $TARGET/index.md
else
echo "😭"
fi