You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.0 KiB
37 lines
1.0 KiB
#!/usr/bin/env bash
|
|
|
|
# format-markdown.sh
|
|
# Author: Matthew Raymer
|
|
# Date: 2025-07-09
|
|
# Description: Format markdown files to comply with project markdown ruleset
|
|
|
|
set -e
|
|
|
|
if [ $# -eq 0 ]; then
|
|
echo "Usage: $0 <file-or-directory> [more files...]"
|
|
exit 1
|
|
fi
|
|
|
|
for target in "$@"; do
|
|
if [ -d "$target" ]; then
|
|
files=$(find "$target" -type f -name "*.md")
|
|
else
|
|
files="$target"
|
|
fi
|
|
for file in $files; do
|
|
# Remove trailing spaces
|
|
sed -i 's/[[:space:]]*$//' "$file"
|
|
# Remove multiple consecutive blank lines
|
|
awk 'NF{blank=0} !NF{blank++} blank<2' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
|
|
# Ensure file ends with a single newline
|
|
awk '1; END{if (NR && $0!="") print ""}' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
|
|
# Optionally run markdownlint (requires npx and markdownlint-cli)
|
|
if command -v npx >/dev/null 2>&1; then
|
|
npx markdownlint "$file"
|
|
else
|
|
echo "npx/markdownlint not found, skipping lint check for $file"
|
|
fi
|
|
done
|
|
done
|
|
|
|
echo "Markdown formatting complete."
|