withai

GitHub Actions를 이용한 index.md 자동 업데이트 구현

작업 내용

update-indexes.yml 파일 수정

  1. workflow 트리거 조건 설정
    on:
      push:
        branches: 
          - main
        paths:
          - 'content-production/**/*.md'
          - 'foreign-lang/**/*.md'
          - 'webdev/**/*.md'
          - '.github/workflows/update-indexes.yml'
    
    • 특정 경로의 .md 파일이 변경될 때만 workflow 실행
  2. index.md 파일 업데이트 로직 개선
    def update_index_file(index_path, folder_path, is_foreign_lang=False):
        # H1 태그 찾기
        h1_match = re.search(r'^# .*$', content, re.MULTILINE)
        if not h1_match:
            print(f"Warning: No H1 header found in {index_path}")
            return False
           
        h1_pos = h1_match.end()
           
        # Last updated 섹션 업데이트 또는 추가
        last_updated = f"\n#### Last updated: {get_korean_time()}\n"
        if '#### Last updated:' in content:
            content = re.sub(r'#### Last updated:.*\n', last_updated, content)
        else:
            # H1 바로 다음에 Last updated 추가
            content = content[:h1_pos] + last_updated + content[h1_pos:]
    
  3. foreign-lang 폴더 특별 처리 로직
    if is_foreign_lang:
        # foreign-lang 폴더 특별 처리
        for section, folder in [('## English', 'en'), ('## Japanese', 'jp')]:
            section_match = re.search(f'{section}.*?\n', content)
            if section_match:
                section_pos = section_match.end()
                   
                # 해당 언어 폴더의 파일들 수집
                md_files = []
                target_folder = os.path.join(folder_path, folder)
                if os.path.exists(target_folder):
                    for file in os.listdir(target_folder):
                        if re.match(r'^\d{8}\.md$', file):
                            date_str = file[:8]
                            formatted_date = f"{date_str[:4]}-{date_str[4:6]}-{date_str[6:8]}"
                            link_path = f"{folder}/{file}"
                            if f"]({link_path})" not in content:  # 중복 체크
                                md_files.append((formatted_date, link_path))
                   
                # 정렬된 링크 추가
                for date, link in sorted(md_files):
                    content = content[:section_pos] + f"- [{date}]({link})\n" + content[section_pos:]
    

주요 변경사항

  1. H1 태그 처리 방식 개선
    • 정규식을 사용하여 H1 태그 위치 정확히 찾기
    • Last updated를 H1 바로 다음에 위치시키기
  2. foreign-lang 폴더 처리 개선
    • English/Japanese 섹션별 파일 관리
    • 각 섹션 아래에 정렬된 링크 추가
    • 중복 링크 방지 로직 추가
  3. 파일 경로 처리 개선
    • Windows/Unix 경로 구분자 호환성 처리
    • 상대 경로 처리 로직 개선
  4. 에러 처리 강화
    • 파일 존재 여부 확인
    • H1 태그 존재 여부 확인
    • 섹션 존재 여부 확인

결과