# Function to get user input get_user_input() { local prompt="$1" local var_name="$2" local default_value="$3" if [ -n "$default_value" ]; then echo -n "$prompt (default: $default_value): " else echo -n "$prompt: " fi read -r input if [ -z "$input" ] && [ -n "$default_value" ]; then eval"$var_name=\"$default_value\"" else eval"$var_name=\"$input\"" fi }
# Function to validate input validate_input() { local input="$1" local field_name="$2" if [ -z "$input" ]; then print_error "$field_name cannot be empty!" return 1 fi return 0 }
# Function to sanitize filename sanitize_filename() { local filename="$1" # Remove special characters and replace spaces with hyphens echo"$filename" | sed 's/[^a-zA-Z0-9\u4e00-\u9fff\s-]//g' | sed 's/\s\+/-/g' | tr'[:upper:]''[:lower:]' }
处理流程:
sed 's/[^a-zA-Z0-9\u4e00-\u9fff\s-]//g' - 移除除字母、数字、中文字符、空格、连字符外的所有字符
# Function to process tags properly process_tags() { local tags_input="$1" local processed_tags="" # Split by comma and process each tag IFS=','read -ra TAG_ARRAY <<< "$tags_input" for tag in"${TAG_ARRAY[@]}"; do # Trim whitespace from tag tag=$(echo"$tag" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') if [ -n "$tag" ]; then if [ -z "$processed_tags" ]; then processed_tags="$tag" else processed_tags="$processed_tags\n - $tag" fi fi done echo"$processed_tags" }
# Get post title get_user_input "Enter post title""post_title""" while ! validate_input "$post_title""Post title"; do get_user_input "Enter post title""post_title""" done
# Create new post using hexo if hexo new "$post_title"; then print_success "Post created successfully!" else print_error "Failed to create post!" exit 1 fi
# Find the created markdown file - improved logic print_message "Searching for created post file..."
# First try to find by exact sanitized title post_file="" today=$(date +%Y-%m-%d)
# Look for files with today's date and sanitized title for file insource/_posts/*.md; do if [[ "$file" == *"$today-$sanitized_title"* ]]; then post_file="$file" break fi done
# If not found, try to find by partial match if [ -z "$post_file" ]; then for file insource/_posts/*.md; do if [[ "$file" == *"$sanitized_title"* ]]; then post_file="$file" break fi done fi
# If still not found, try to find the most recent file if [ -z "$post_file" ]; then post_file=$(ls -t source/_posts/*.md | head -n 1) if [ -n "$post_file" ]; then print_warning "Could not find exact match, using most recent file: $post_file" fi fi
if [ -z "$post_file" ] || [ ! -f "$post_file" ]; then print_error "Could not find the created post file!" print_message "Available files in source/_posts/:" ls -la source/_posts/ exit 1 fi
# Add tags if provided if [ -n "$post_tags" ]; then front_matter="$front_matter\ntags:" processed_tags=$(process_tags "$post_tags") if [ -n "$processed_tags" ]; then front_matter="$front_matter\n - $processed_tags" fi fi
# Open Typora for editing print_message "Opening Typora for editing..." if [ -f "$TYPORA_PATH" ]; then "$TYPORA_PATH""$post_file" & print_success "Typora opened successfully!" else print_warning "Typora not found at expected path. Please open the file manually:" echo"File location: $post_file" fi
if [[ "$publish_choice" =~ ^[Yy]$ ]]; then print_message "Starting deployment process..." # Generate static files print_message "Generating static files..." if hexo g; then print_success "Generation completed!" else print_error "Generation failed!" exit 1 fi # Deploy to server print_message "Deploying to server..." if hexo d; then print_success "Deployment completed successfully!" print_success "Your post has been published!" else print_error "Deployment failed!" exit 1 fi