Last active
February 17, 2024 19:23
-
-
Save creotiv/4fe6ee7393d3bd5621536ea759b81deb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Update README with Structured Directory Listings | |
on: | |
push: | |
branches: | |
- main # Adjust if your default branch has a different name | |
jobs: | |
update-readme: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Update README with Directory Structure | |
run: | | |
MAIN_README="./README.md" | |
echo "# Articles" > "$MAIN_README" | |
# Recursive function to process directories | |
process_directory() { | |
local directory=$1 | |
local indent_level=$2 | |
local prefix="" | |
# Create indent based on the directory level | |
for ((i=0; i<indent_level; i++)); do | |
prefix+="#" | |
done | |
# Avoid printing root directory title (.) | |
if [ "$directory" != "." ]; then | |
# Convert directory path to title | |
local title=$(basename "$directory") | |
title="${title//_/ }" | |
title="${title//-/ }" | |
echo -e "\n$prefix $title\n" | awk '{print toupper($0)}' >> "$MAIN_README" | |
fi | |
while IFS= read -r -d $'\0' md_file; do | |
local name=$(basename "$md_file" .md) | |
name="${name//_/ }" | |
name="${name//-/ }" | |
name="${name^}" # Capitalize the first letter | |
local relpath="${md_file#./}" # Remove leading ./ | |
relpath="${relpath// /%20}" # Replace spaces with '%20' for URL encoding | |
echo "- [$name]($relpath)" >> "$MAIN_README" | |
done < <(find "$directory" -maxdepth 1 -type f -name '*.md' ! -name 'README.md' -print0) | |
# Recurse into subdirectories, excluding .github | |
while IFS= read -r -d $'\0' subdir; do | |
process_directory "$subdir" $((indent_level + 1)) | |
done < <(find "$directory" -maxdepth 1 -mindepth 1 -type d ! -path '*/.github' ! -path '*/.git' -print0) | |
} | |
# Start processing from the root directory | |
process_directory "." 1 | |
# Commit and push changes if there are any | |
git config --local user.name actions-user | |
git config --local user.email "[email protected]" | |
git add "$MAIN_README" | |
git commit -m "Updated README with a structured list of markdown files" && git push || echo "No changes was made" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment