|
| 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