Content: Markdown
Framework: Zola
Hosting: Github Pages
Styling: SCSS
Theme: Consoler Dark
Title: parkerjones.dev

create a new post script

3 minute read Published: 2025-01-06
#!/bin/bash

# Determine the directory of the script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Determine the project root directory (assuming scripts folder is in the project root)
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

# Set the content directory path
content_dir="$PROJECT_ROOT/content"

# Ensure the content directory exists
if [ ! -d "$content_dir" ]; then
  echo "Content directory not found at $content_dir. Please ensure you're running the script within your Zola project structure."
  exit 1
fi

# Get the title from arguments or set a default
if [ "$#" -gt 0 ]; then
  title="$*"
else
  datetime=$(date +"%Y-%m-%d %H:%M:%S")
  title="new draft $datetime"
fi

# Generate the date for the frontmatter
date_today=$(date +"%Y-%m-%d")

# Clean the title to create a filename-friendly string
cleaned_title=$(echo "$title" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -cd '[:alnum:]-')

# Set the filename and path
filename="${cleaned_title}.md"
filepath="${content_dir}/${filename}"

# Create the frontmatter in the markdown file
cat <<EOF > "$filepath"
+++
title = "$title"
date = $date_today

[taxonomies]
categories = []
tags = []
+++
EOF

echo "New markdown file created at $filepath"

# Open the new markdown file in Neovim
nvim "$filepath"

Explanation of the Updates:

  1. Added Neovim Command:

    # Open the new markdown file in Neovim
    nvim "$filepath"
    
    • After creating the new markdown file and confirming its creation, this line opens the file in Neovim.
    • It uses the nvim command followed by the path to the new file.
  2. Ensuring Neovim is Installed:

    • The script assumes that Neovim (nvim) is installed and available in your system's PATH.
    • If Neovim isn't installed, you'll need to install it, or you can modify the script to use a different editor (e.g., vim, nano, code, etc.).

Usage Instructions:

  1. Place the Script:

    • Save the script as new_post.sh inside the scripts folder of your project:

      your-project/
      ├── content/
      ├── scripts/
      │   └── new_post.sh
      └── ...
      
  2. Make the Script Executable:

    chmod +x scripts/new_post.sh
    
  3. Run the Script:

    • From the Project Root:

      ./scripts/new_post.sh "My Custom Blog Post Title"
      
    • From the Scripts Directory:

      cd scripts
      ./new_post.sh "My Custom Blog Post Title"
      
    • Without a Title (uses default):

      ./scripts/new_post.sh
      
  4. Editing the New Post:

    • After running the script, it will:

      • Create a new markdown file in your content directory.
      • Output a confirmation message showing the path to the new file.
      • Immediately open the new file in Neovim for editing.
    • For example:

      New markdown file created at /path/to/your/project/content/my-custom-blog-post-title.md
      

      Neovim will open with the new markdown file loaded, ready for you to start writing your blog post.


Additional Notes:


Summary:

With this updated script, you can streamline your blogging workflow:

Feel free to customize the script to fit your workflow or editor preferences. If you have any questions or need further assistance, don't hesitate to ask!