zs/default/.zs/nav

58 lines
1.7 KiB
Plaintext
Raw Normal View History

#!/bin/sh
# Get the directory of the current page
current_dir=$(dirname "$ZS_SOURCE_FILE")
# Remove leading './' if present
case "$current_dir" in
./*) current_dir="${current_dir#./}" ;;
esac
# Initialize the navigation HTML with the 'Home' link
if [ "$ZS_URL" = "index.html" ]; then
nav_html="<li><a href=\"/index.html\" class=\"active\">Home</a></li>"
else
nav_html="<li><a href=\"/\">Home</a></li>"
fi
# Create a temporary file to store filenames
tmpfile=$(mktemp)
# Find all Markdown files in the current directory, excluding 'README.md' and 'index.md'
find "$current_dir" -maxdepth 1 -type f -name "*.md" ! -name "README.md" ! -name "index.md" -print >"$tmpfile"
# Sort the files alphabetically
sort "$tmpfile" >"${tmpfile}.sorted"
# Read each file and process
while IFS= read -r md_file; do
# Get the base filename without extension
base_name=$(basename "$md_file" .md)
# Replace hyphens and underscores with spaces for display
display_name=$(echo "$base_name" | tr '-' ' ' | tr '_' ' ')
# Capitalize the first letter of each word using awk
display_name=$(echo "$display_name" | awk '{ for (i=1; i<=NF; i++) { $i = toupper(substr($i,1,1)) substr($i,2) } print }')
# Construct the link href relative to the site root
if [ "$current_dir" = "." ]; then
href="/${base_name}.html"
else
href="/${current_dir}/${base_name}.html"
fi
# Append the navigation item
if [ "$ZS_URL" = "$base_name.html" ]; then
nav_html="${nav_html}<li><a href=\"${href}\" class=\"active\">${display_name}</a></li>"
else
nav_html="${nav_html}<li><a href=\"${href}\">${display_name}</a></li>"
fi
done <"${tmpfile}.sorted"
# Remove temporary files
rm -f "$tmpfile" "${tmpfile}.sorted"
# Output the navigation HTML
echo "$nav_html"