system-prompts/generate_toc_for_readme.sh
2023-11-20 20:56:21 +08:00

26 lines
No EOL
917 B
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
generate_toc() {
local dir=$1
local base_dir=$(pwd)
for file in "$dir"/*; do
if [ -d "$file" ]; then
local dir_name=$(basename "$file")
echo "- $dir_name"
echo " $(generate_toc "$file")"
elif [ -f "$file" ] && [[ $file == *.md ]]; then
local title=$(basename "$file" .md)
# Replace Windows incompatible characters with '_'
local safe_title=$(echo "$title" | sed 's/[<>:"\/\\|?*]/_/g')
# Rename the source file with the safe_title
mv "$file" "$dir/$safe_title.md"
# shellcheck disable=SC2001
local link=$(echo "$dir/$safe_title.md" | sed "s|^$base_dir/||")
local encoded_link=$(python -c "import urllib.parse; print(urllib.parse.quote('''$link'''))")
echo " - [$safe_title]($encoded_link)"
fi
done
}
generate_toc .