-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoc.sh
executable file
·56 lines (46 loc) · 1.54 KB
/
toc.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# Output markdown file
output_file="TOC.md"
# Clear the output file
echo "" > $output_file
# Function to check if a directory contains any markdown files
contains_markdown() {
local dir_path=$1
# Use find to look for markdown files only
find "$dir_path" -maxdepth 1 -type f -name "*.md" | grep -q .
}
# Function to generate bullet points for each directory containing markdown files
generate_toc() {
local dir_path=$1
local indent=$2
# Check if the directory contains any markdown files
if contains_markdown "$dir_path"; then
echo "${indent}- **${dir_path}**" >> $output_file
# List markdown files in the current directory
find "$dir_path" -maxdepth 1 -type f -name "*.md" | while read file; do
file_name=$(basename "$file")
echo "${indent} - [$file_name]($file)" >> $output_file
done
fi
# Recursively process subdirectories, ignoring the .git directory
find "$dir_path" -mindepth 1 -type d -not -path '*/.git/*' | while read subdir; do
if contains_markdown "$subdir"; then
generate_toc "$subdir" "$indent "
fi
done
}
# Check if the input directory is provided as a parameter
if [ -z "$1" ]; then
# Default to current directory if no argument is provided
root_dir="."
else
root_dir="$1"
fi
# Check if the directory exists
if [ ! -d "$root_dir" ]; then
echo "Error: Directory $root_dir does not exist."
exit 1
fi
# Start generating the TOC
generate_toc "$root_dir" ""
echo "TOC generated in $output_file"