-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.sh
More file actions
executable file
·85 lines (70 loc) · 2.22 KB
/
Copy pathpost.sh
File metadata and controls
executable file
·85 lines (70 loc) · 2.22 KB
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
#!/bin/bash
# Blog post creation script
# Usage: ./post.sh "Your Blog Post Title"
set -e # Exit on error
# Check if title provided
if [ -z "$1" ]; then
echo "Usage: $0 \"Your Blog Post Title\""
exit 1
fi
# Get script directory for absolute paths
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BLOG_DIR="${SCRIPT_DIR}/src/content/blog"
# Generate date and slug
DATE=$(date +%Y-%m-%d)
TITLE="$1"
# Better slug generation: preserve hyphens, handle edge cases
# Convert to lowercase, replace spaces with hyphens, remove special chars except hyphens
# Collapse multiple hyphens, trim leading/trailing hyphens
SLUG=$(echo "$TITLE" | \
tr '[:upper:]' '[:lower:]' | \
sed 's/[[:space:]]/-/g' | \
sed 's/[^a-z0-9-]//g' | \
sed 's/--*/-/g' | \
sed 's/^-//;s/-$//')
# Validate slug is not empty
if [ -z "$SLUG" ]; then
echo "Error: Generated slug is empty. Please use a title with alphanumeric characters."
exit 1
fi
FOLDER_NAME="${DATE}-${SLUG}"
FOLDER_PATH="${BLOG_DIR}/${FOLDER_NAME}"
# Check if folder already exists
if [ -d "$FOLDER_PATH" ]; then
echo "Error: Post folder already exists: ${FOLDER_PATH}"
echo "Please use a different title or remove the existing folder."
exit 1
fi
# Create the folder
mkdir -p "$FOLDER_PATH"
# Check if pnpm is available
if ! command -v pnpm &> /dev/null; then
echo "Error: pnpm not found. Please install pnpm."
rm -rf "$FOLDER_PATH"
exit 1
fi
# Use newmd to create the post with a temporary name
TEMP_NAME="${FOLDER_NAME}-temp"
TEMP_PATH="${BLOG_DIR}/${TEMP_NAME}.md"
# Change to src directory for newmd (it requires this)
cd "${SCRIPT_DIR}/src"
# Create the post
if ! pnpm exec newmd blog --slug "${TEMP_NAME}" "$TITLE" 2>/dev/null; then
echo "Error: Failed to create blog post with newmd"
rm -rf "$FOLDER_PATH"
exit 1
fi
# Move the created file to index.md in the new folder
if [ -f "$TEMP_PATH" ]; then
mv "$TEMP_PATH" "${FOLDER_PATH}/index.md"
echo ""
echo "Created new blog post: ${FOLDER_PATH}/index.md"
echo ""
echo "Add any images to: ${FOLDER_PATH}/"
echo ""
else
echo "Error: Expected file not created: ${TEMP_PATH}"
# Clean up - use rm -rf for robust cleanup
rm -rf "$FOLDER_PATH"
exit 1
fi