Rename folders and markdown files based on h1 titles #299
This file contains 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: Rename Markdown Files and Folders in-place | |
on: | |
push: | |
branches: | |
- main | |
workflow_dispatch: | |
jobs: | |
process-markdown: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.GITBOOKKEY }} | |
fetch-depth: 0 | |
- name: Set UTF-8 Encoding | |
run: | | |
export LC_CTYPE="UTF-8" | |
- name: Rename Folders and Files Based on README H1 In-place | |
shell: bash | |
run: | | |
set -euxo pipefail # λλ²κΉ μ μν μ Έ μ΅μ μ€μ | |
# λͺ¨λ README.md νμΌμ κ²½λ‘μ ν€λ©μ 미리 μμ§ | |
mapfile -t readme_files < <(find developLog -type f -name 'README.md') | |
declare -A dir_mappings # μλ³Έ λλ ν 리μ μλ‘μ΄ λλ ν 리μ 맀νμ μ μ₯ | |
# λλ ν 리 μ΄λ¦ λ³κ²½μ μν μ 보 μμ§ | |
for file in "${readme_files[@]}"; do | |
echo "Processing $file" | |
# README.md νμΌμμ 첫 λ²μ§Έ h1 ν€λ© μΆμΆ | |
heading=$(grep -m 1 '^# ' "$file" | sed 's/^# //') | |
# ν€λ©μ΄ μλ κ²½μ° κ±΄λλλλ€ | |
if [[ -z "$heading" ]]; then | |
echo "ν€λ©μ μ°Ύμ μ μμ΅λλ€: $file" | |
continue | |
fi | |
# ν€λ© μ μ (νμΌλͺ μ μ ν©νλλ‘) | |
heading=$(echo "$heading" | tr -cd '[:alnum:] _-') | |
# README.md νμΌμ΄ μλ λλ ν 리 κ²½λ‘ | |
dir=$(dirname "$file") | |
parent_dir=$(dirname "$dir") | |
# μλ‘μ΄ λλ ν 리 μ΄λ¦ μμ± | |
new_dir="$parent_dir/$heading" | |
# λλ ν 리 맀ν μ μ₯ | |
dir_mappings["$dir"]="$new_dir" | |
done | |
# λλ ν 리 μ΄λ¦ λ³κ²½ (κΉμ΄ μμΌλ‘ μ λ ¬νμ¬ νμ λλ ν 리λΆν° λ³κ²½) | |
for dir in "${!dir_mappings[@]}"; do | |
new_dir="${dir_mappings[$dir]}" | |
echo "Renaming $dir to $new_dir" | |
# λλ ν λ¦¬κ° μ΄λ―Έ μ‘΄μ¬νλ©΄ 건λλλλ€ | |
if [[ -d "$new_dir" ]]; then | |
echo "λλ ν λ¦¬κ° μ΄λ―Έ μ‘΄μ¬ν©λλ€: $new_dir. 건λλλλ€." | |
continue | |
fi | |
mv "$dir" "$new_dir" | |
done | |
# λͺ¨λ λ§ν¬λ€μ΄ νμΌμ κ²½λ‘ μμ§ | |
mapfile -t markdown_files < <(find developLog -type f -name '*.md' ! -name 'README.md') | |
# νμΌ μ΄λ¦ λ³κ²½ | |
for file in "${markdown_files[@]}"; do | |
echo "Processing $file" | |
# 첫 λ²μ§Έ h1 ν€λ©μ μΆμΆνμ¬ μ λͺ©μΌλ‘ μ¬μ© | |
title=$(grep -m 1 '^#' "$file" | sed 's/^# //') | |
if [[ -n "$title" ]]; then | |
# μ λͺ© μ μ (νμΌλͺ μ μ ν©νλλ‘) | |
title=$(echo "$title" | tr -cd '[:alnum:] _-') | |
dir=$(dirname "$file") | |
ext="${file##*.}" | |
new_file="$dir/$title.$ext" | |
# λμΌν μ΄λ¦μ νμΌμ΄ μ΄λ―Έ μ‘΄μ¬νλ©΄ 건λλλλ€ | |
if [[ "$file" != "$new_file" && ! -e "$new_file" ]]; then | |
echo "Renaming $file to $new_file" | |
mv "$file" "$new_file" | |
else | |
echo "νμΌ μ΄λ¦μ λ³κ²½ν μ μμ΅λλ€: $file. 건λλλλ€." | |
fi | |
else | |
echo "μ ν¨ν μ λͺ©μ μ°Ύμ μ μμ΅λλ€: $file. 건λλλλ€." | |
fi | |
done | |
# λ³κ²½λ νμΌκ³Ό λλ ν 리 νμΈ | |
echo "λ³κ²½λ developLog λλ ν 리μ λ΄μ©:" | |
find developLog -type f | |
- name: Commit changes | |
shell: bash | |
run: | | |
set -euxo pipefail # λλ²κΉ μ μν μ Έ μ΅μ μ€μ | |
# Git μ¬μ©μ μ€μ (μ΄λ―Έ μ€μ λμ΄ μλ€λ©΄ μλ΅ κ°λ₯) | |
git config user.name 'GoldenPearls' | |
git config user.email 'prettylee620@naver.com' | |
# λ³κ²½λ νμΌ μΆκ° | |
git add developLog | |
# μ»€λ° μμ± | |
git commit -m "Rename folders and markdown files based on h1 titles" | |
# μ격 μ μ₯μμ νΈμ | |
git push origin HEAD:main --force |