Skip to content

Update Rename and Commit Markdown Files.yml #292

Update Rename and Commit Markdown Files.yml

Update Rename and Commit Markdown Files.yml #292

name: Rename and Add New Markdown Files
on:
push:
branches:
- main
workflow_dispatch:
jobs:
process-markdown:
runs-on: ubuntu-latest
env:
destination_directory: koreanPageTitle # 환경 변수로 선언
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 Based on README H1 and Copy Markdown Files
run: |
set -euxo pipefail # 디버깅을 위한 셸 옵션 설정
# 목적지 디렉토리 설정
destination_directory="${destination_directory}"
# koreanPageTitle 디렉토리 생성 (없을 경우)
mkdir -p "$destination_directory"
# README.md 파일을 찾아서 디렉토리 이름 변경
find developLog -type f -name 'README.md' | while read file; 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"
# 동일한 이름의 디렉토리가 존재하는지 확인
if [[ -d "$new_dir" ]]; then
echo "이미 존재하는 디렉토리입니다: $new_dir"
continue
fi
# 디렉토리 이름 변경
mv "$dir" "$new_dir"
done
# 디렉토리 이름 변경 후, 모든 마크다운 파일 처리
find developLog -type f -name '*.md' | while read file; do
echo "Processing $file"
# README.md 및 SUMMARY.md 파일 처리
if [[ "$(basename "$file")" == "README.md" ]] || [[ "$(basename "$file")" == "SUMMARY.md" ]]; then
# README.md 및 SUMMARY.md 파일을 목적지 디렉토리로 복사
cp "$file" "$destination_directory/$(basename "$file")"
continue
fi
# 첫 번째 h1 헤딩을 추출하여 제목으로 사용
title=$(grep -m 1 '^#' "$file" | sed 's/^# //')
if [ -n "$title" ]; then
# 제목 정제 (파일명에 적합하도록)
title=$(echo "$title" | tr -cd '[:alnum:] _-')
dir=$(dirname "$file")
new_dir="$destination_directory/$dir"
new_filename="$new_dir/$title.md"
mkdir -p "$new_dir"
if [ -f "$new_filename" ]; then
if cmp -s "$file" "$new_filename"; then
echo "파일 $new_filename 이 이미 존재하며 동일합니다. 건너뜁니다..."
else
echo "파일 $new_filename 이 이미 존재하지만 다릅니다. 덮어씁니다."
cp "$file" "$new_filename"
fi
else
echo "$file 을(를) $new_filename 으로 복사합니다"
cp "$file" "$new_filename"
fi
else
echo "유효한 제목을 찾을 수 없습니다: $file, 건너뜁니다."
fi
done
# .gitbook/assets 디렉토리 복사
if [ -d "developLog/.gitbook/assets" ]; then
mkdir -p "$destination_directory/developLog/.gitbook/assets"
cp -r developLog/.gitbook/assets/* "$destination_directory/developLog/.gitbook/assets/"
echo ".gitbook/assets 디렉토리를 $destination_directory/developLog 으로 복사했습니다."
fi
# 복사된 파일 확인
echo "$destination_directory 디렉토리의 내용:"
find "$destination_directory" -type f
- name: Commit changes in koreanPageTitle branch
run: |
set -euxo pipefail # 디버깅을 위한 셸 옵션 설정
# 목적지 디렉토리 설정
destination_directory="${destination_directory}"
# Git 사용자 설정 (이미 설정되어 있다면 생략 가능)
git config user.name 'GoldenPearls'
git config user.email 'prettylee620@naver.com'
# 새로운 브랜치 생성 및 체크아웃
git checkout -b koreanPageTitle
# 변경된 파일 추가
git add "$destination_directory"/*
# 커밋 작성
git commit -m "Rename and update Markdown files based on h1 titles in koreanPageTitle"
# 원격 저장소에 브랜치 푸시
git push origin koreanPageTitle --force