#!/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:
-
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.
-
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.).
- The script assumes that Neovim (
Usage Instructions:
-
Place the Script:
-
Save the script as
new_post.sh
inside thescripts
folder of your project:your-project/ ├── content/ ├── scripts/ │ └── new_post.sh └── ...
-
-
Make the Script Executable:
chmod +x scripts/new_post.sh
-
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
-
-
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.
- Create a new markdown file in your
-
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:
-
Customizing the Editor:
-
If you prefer to use a different editor, replace
nvim
in the script with your editor's command.-
For Vim:
vim "$filepath"
-
For Nano:
nano "$filepath"
-
For Visual Studio Code:
code "$filepath"
-
You might need to add the
--wait
flag if you want the script to pause until the editor is closed:code --wait "$filepath"
-
-
-
-
Handling Editor Availability:
- Ensure that the editor you specify is installed and accessible from the command line.
- If the editor is not found, the script will output an error like
command not found: nvim
.
-
Running the Script from Anywhere:
-
Because the script calculates paths relative to its own location, you can run it from any directory, and it will still correctly locate your project's
content
directory./full/path/to/your/project/scripts/new_post.sh "Another Blog Post"
-
-
Error Handling:
- The script includes checks to ensure the
content
directory exists. - If the
content
directory is missing or mislocated, you'll receive an error message.
- The script includes checks to ensure the
-
Shebang Line:
- The shebang line (
#!/bin/bash
) assumes thatbash
is located at/bin/bash
. - If your system uses a different path for
bash
, adjust the shebang line accordingly (e.g.,#!/usr/bin/env bash
).
- The shebang line (
-
Permissions:
- Make sure you have the necessary permissions to create files in the
content
directory and to execute the script.
- Make sure you have the necessary permissions to create files in the
Summary:
With this updated script, you can streamline your blogging workflow:
- Create: Run the script to generate a new markdown file with the appropriate frontmatter.
- Edit: The script automatically opens the new file in Neovim, allowing you to start writing immediately.
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!