forked from LouisShark/chatgpt_system_prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_toc_for_readme.sh
35 lines (30 loc) · 1.16 KB
/
generate_toc_for_readme.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash
generate_toc() {
local dir=$1
local base_dir=$(pwd)
local exclude_dir="knowledge" # 设置要排除的目录名称
for file in "$dir"/*; do
# 获取文件或目录的基本名称
local name=$(basename "$file")
# 如果是要排除的目录,则跳过
if [ "$name" == "$exclude_dir" ]; then
continue
fi
if [ -d "$file" ]; then
echo "- $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"
# Create a relative link
local relative_link="${dir#$base_dir/}/$safe_title.md"
# Manually encode only spaces and other few characters if needed
local encoded_link=$(echo "$relative_link" | sed 's/ /%20/g; s/"//g')
echo " - [$safe_title]($encoded_link)"
fi
done
}
generate_toc .