-
Notifications
You must be signed in to change notification settings - Fork 1
/
table_of_contents_md.sh
34 lines (29 loc) · 1.08 KB
/
table_of_contents_md.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
#!/bin/bash
# Function to scan directories recursively
scan_dir() {
local dir=$1
local indent=$2
local -a subdirs=()
# Loop through the files and directories in the current directory
for file in "$dir"/*; do
# If it's a directory, add to the subdirs array for later processing
if [ -d "$file" ]; then
subdirs+=("$file")
elif [ -f "$file" ]; then
# If it's a file, add to the table of contents with appropriate indentation
echo "${indent}- [$(basename "$file")]($file)" >> _tableofcontents.md
fi
done
# Recursively scan subdirectories
for subdir in "${subdirs[@]}"; do
# Print the directory name with indentation
echo "${indent}- $(basename "$subdir")/" >> _tableofcontents.md
# Scan the subdirectory with increased indentation
scan_dir "$subdir" "$indent "
done
}
# Start with the ./lib directory and no indentation
echo "# Table of Contents" > _tableofcontents.md
scan_dir "./lib" ""
# Output the result
echo "Table of contents generated in _tableofcontents.md"