Files
alexanderwhitestone.com/scripts/aw-post

53 lines
1.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# aw-post — Quick-post a scroll from the command line
#
# Usage:
# aw-post "Title of the Post"
# aw-post "Title" < body.md
# echo "Post body here" | aw-post "Title"
#
# Creates a new markdown file in blog/posts/ with frontmatter,
# then rebuilds the blog index, post pages, and RSS feed.
set -euo pipefail
REPO_DIR="$(cd "$(dirname "$0")/.." && pwd)"
BLOG_DIR="${REPO_DIR}/blog/posts"
if [ $# -lt 1 ]; then
echo "Usage: aw-post \"Title of the Post\""
echo " Pipe or redirect body content via stdin."
exit 1
fi
TITLE="$1"
DATE=$(date +%Y-%m-%d)
SLUG=$(echo "$TITLE" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -cd 'a-z0-9-')
FILENAME="${DATE}-${SLUG}.md"
FILEPATH="${BLOG_DIR}/${FILENAME}"
if [ -f "$FILEPATH" ]; then
echo "Error: $FILEPATH already exists"
exit 1
fi
# Read body from stdin if available
BODY=""
if [ ! -t 0 ]; then
BODY=$(cat)
fi
cat > "$FILEPATH" << EOF
---
title: "${TITLE}"
date: ${DATE}
---
${BODY}
EOF
echo "Created: ${FILEPATH}"
# Rebuild the blog
python3 "${REPO_DIR}/scripts/build.py"