2023-08-06 01:08:38 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Function to sanitize the title
|
|
|
|
sanitize_title() {
|
|
|
|
title="$1"
|
|
|
|
sanitized_title=$(echo "$title" | tr -dc 'a-zA-Z0-9 ')
|
|
|
|
echo "$sanitized_title" | tr ' ' '-'
|
|
|
|
}
|
|
|
|
|
2024-02-24 21:39:40 +08:00
|
|
|
# Prompt the user for a title
|
|
|
|
read -e -p "Enter the title: " user_title
|
|
|
|
|
|
|
|
# Sanitize the title
|
|
|
|
sanitized_title=$(sanitize_title "$user_title")
|
|
|
|
|
|
|
|
# Generate the filename with the current date
|
|
|
|
current_date=$(date +'%Y-%m-%d')
|
|
|
|
filename="${current_date}-${sanitized_title}"
|
|
|
|
filepath="_posts/${filename}.md"
|
|
|
|
|
2023-08-06 01:08:38 +08:00
|
|
|
# Function to generate the front matter with the current date
|
|
|
|
generate_front_matter() {
|
|
|
|
current_date=$(date +'%Y-%m-%d %H:%M:%S %z')
|
|
|
|
echo "---"
|
|
|
|
echo "title: $1"
|
2023-08-09 20:46:48 +08:00
|
|
|
echo "author: peter"
|
2023-08-06 01:08:38 +08:00
|
|
|
echo "date: $current_date"
|
2024-10-29 16:22:43 +08:00
|
|
|
echo "categories: [TOP_CATEGORY, SUB_CATEGORY] # 0-2 categories. Blogging | Electronics | Programming | Mechanical | SelfHosting | Guides | University"
|
2024-10-13 04:41:15 +08:00
|
|
|
echo "tags: [getting started] # 0-\infty. systems | embedded | rf | microwave | electronics | solidworks | automation | tip"
|
2024-02-24 21:39:40 +08:00
|
|
|
echo "# image: assets/img/${filename:0:31}/preview.png"
|
2023-08-06 01:08:38 +08:00
|
|
|
echo "---"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if the file already exists
|
|
|
|
if [ -e "$filepath" ]; then
|
|
|
|
echo "A file with the name '$filename' already exists in the '_posts' subdirectory."
|
|
|
|
else
|
|
|
|
# Create the new file and add the front matter
|
2024-10-29 16:22:43 +08:00
|
|
|
generate_front_matter "$user_title" >"$filepath"
|
2023-08-09 20:38:56 +08:00
|
|
|
mkdir -p "assets/img/${filename:0:31}"
|
2023-08-06 22:12:27 +08:00
|
|
|
echo "File '$filename.md' created successfully in the '_posts' subdirectory."
|
2023-08-06 01:08:38 +08:00
|
|
|
fi
|