Skip to content

Commit 3336c3c

Browse files
committed
feat: add section toc pages
1 parent 1b2d5c9 commit 3336c3c

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

tools/git_script.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ find "$KB" -maxdepth 1 -type f ! -name "p8_*" -exec rm -v {} \;
5656

5757
cd "$GIT_REPO_LOCAL"
5858
"$SCRIPT_DIR/generate_toc.sh" ./ "$PYDIO_V8_RELATIVE_URL"
59+
"$SCRIPT_DIR/section_toc_index.sh" ./ 3
5960

6061
git add .
6162
git commit -m "Add pydio-v8 documentation"
@@ -174,6 +175,7 @@ git checkout cells-flows
174175

175176
cd "$GIT_REPO_LOCAL"
176177
"$SCRIPT_DIR/generate_toc.sh" ./ "$CELLS_V4_RELATIVE_URL"
178+
"$SCRIPT_DIR/section_toc_index.sh" ./ 3
177179

178180
git add .
179181
git commit -m "Add cells-v4 documentation"

tools/section_toc_index.sh

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/bin/bash
2+
3+
DOCS_DIR="${1:-docs}"
4+
MAX_DEPTH="${2:-2}"
5+
6+
slugify() {
7+
echo "$1" | iconv -t ascii//TRANSLIT | \
8+
tr '[:upper:]' '[:lower:]' | \
9+
sed -E 's/[^a-z0-9]+/-/g' | \
10+
sed -E 's/^-+|-+$//g'
11+
}
12+
13+
extract_slug_from_nav() {
14+
local dir="$1"
15+
local nav_file="$dir/.nav.yaml"
16+
if [[ -f "$nav_file" ]]; then
17+
grep -m1 '^slug:' "$nav_file" | cut -d':' -f2- | sed 's/^ *//' | sed 's/"//g'
18+
fi
19+
}
20+
21+
extract_title_from_nav() {
22+
local dir="$1"
23+
local nav_file="$dir/.nav.yaml"
24+
if [[ -f "$nav_file" ]]; then
25+
grep -m1 '^title:' "$nav_file" | cut -d':' -f2- | sed 's/^ *//' | sed 's/"//g'
26+
fi
27+
}
28+
29+
extract_slug_from_md() {
30+
local file="$1"
31+
grep -m1 '^slug:' "$file" | cut -d':' -f2- | sed 's/^ *//' | sed 's/"//g'
32+
}
33+
34+
extract_title_from_md() {
35+
local file="$1"
36+
grep -m1 '^title:' "$file" | cut -d':' -f2- | sed 's/^ *//' | sed 's/"//g'
37+
}
38+
39+
build_slug_path() {
40+
local path="$1"
41+
local slug_parts=()
42+
43+
while [[ "$path" != "$DOCS_DIR" && "$path" != "." && "$path" != "/" ]]; do
44+
slug=$(extract_slug_from_nav "$path")
45+
[[ -z "$slug" ]] && slug=$(basename "$path")
46+
slug_parts=("$slug" "${slug_parts[@]}")
47+
path=$(dirname "$path")
48+
done
49+
50+
IFS=/; echo "${slug_parts[*]}"
51+
}
52+
53+
generate_toc() {
54+
local base_dir="$1"
55+
local rel_dir="$2"
56+
local depth="$3"
57+
local indent="$4"
58+
local toc=""
59+
60+
[[ "$depth" -gt "$MAX_DEPTH" ]] && return
61+
62+
for item in "$base_dir/$rel_dir"/*; do
63+
[[ "$(basename "$item")" =~ ^(images|urlmaps)$ ]] && continue
64+
65+
if [[ -f "$item" && "$item" == *.md ]]; then
66+
[[ "$(basename "$item")" == "index.md" ]] && continue
67+
title=$(extract_title_from_md "$item")
68+
[[ -z "$title" ]] && title=$(basename "$item" .md)
69+
slug=$(extract_slug_from_md "$item")
70+
[[ -z "$slug" ]] && slug=$(basename "$item" .md)
71+
parent_slug_path=$(build_slug_path "$(dirname "$item")")
72+
relative_slug_path="${parent_slug_path#*/}"
73+
link="../${relative_slug_path}/${slug}"
74+
toc+="$(printf "\n%s* [%s](%s)\n" "$indent" "$title" "$link")"
75+
76+
elif [[ -d "$item" ]]; then
77+
title=$(extract_title_from_nav "$item")
78+
[[ -z "$title" ]] && title=$(basename "$item")
79+
slug=$(extract_slug_from_nav "$item")
80+
[[ -z "$slug" ]] && slug=$(basename "$item")
81+
parent_slug_path=$(build_slug_path "$item")
82+
relative_folder_path="${parent_slug_path#*/}"
83+
folder_link="../${relative_folder_path}/index/"
84+
toc+="$(printf "\n%s* **[%s](%s)**\n" "$indent" "$title" "$folder_link")"
85+
toc+=$(generate_toc "$base_dir" "$(realpath --relative-to="$base_dir" "$item")" $((depth + 1)) "$indent ")
86+
fi
87+
done
88+
89+
echo "$toc"
90+
}
91+
92+
generate_index() {
93+
local folder="$1"
94+
local index_file="$folder/index.md"
95+
96+
if [[ ! -f "$index_file" ]]; then
97+
echo "ℹ️ Creating missing index.md in $folder"
98+
touch "$index_file"
99+
fi
100+
101+
local title
102+
title=$(extract_title_from_nav "$folder")
103+
[[ -z "$title" ]] && title=$(basename "$folder")
104+
105+
local toc
106+
toc=$(generate_toc "$folder" "." 1 "")
107+
108+
{
109+
echo "---"
110+
echo "title: $title"
111+
echo "weight: 0"
112+
echo "---"
113+
echo
114+
echo "$toc"
115+
} > "$index_file.tmp" && mv "$index_file.tmp" "$index_file"
116+
117+
echo "✅ TOC generated in: $index_file"
118+
}
119+
120+
# Process 1st-level folders under DOCS_DIR
121+
for folder in "$DOCS_DIR"/*; do
122+
[[ -d "$folder" ]] || continue
123+
[[ "$(basename "$folder")" =~ ^(images|urlmaps)$ ]] && continue
124+
125+
generate_index "$folder"
126+
done
127+
128+
echo "🎉 Recursive TOC generation completed (max depth: $MAX_DEPTH)"

0 commit comments

Comments
 (0)