_index 참조 수정
스크립트
- update_index_link.py
import os
import re
def remove_index_references(source_dir):
# 주어진 디렉토리 내의 모든 파일을 순회
for root, dirs, files in os.walk(source_dir):
dirs[:] = [d for d in dirs if not d.startswith('.') and not d.startswith('_')]
for file in files:
file_path = os.path.join(root, file)
# 파일이 .md 확장자를 가졌는지 확인
if file.endswith('.md'):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# _index를 참조에서 제거
modified_content = re.sub(r'(\[.*?\]\(.*?)/_index\)', r'\1)', content)
# 파일이 변경된 경우에만 다시 저장
if content != modified_content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(modified_content)
print(f"\tModified: {file_path}")
if __name__ == "__main__":
# 기본 디렉토리를 설정하거나 환경 변수를 사용
target_dir = os.environ.get('TARGET_CONTENT_DIR', os.path.join(os.getcwd(), 'content'))
print(f"update_index_link start")
remove_index_references(target_dir)
print(f"update_index_link end")
print()
기능
Markdown 파일 검색
- 지정된 디렉토리와 그 하위 디렉토리의 모든
.md
파일을 검색합니다. - 숨김 폴더와
_
로 시작하는 폴더는 제외합니다.
_index
참조 제거
- 파일 내의 링크에서
_index
로 끝나는 부분을 찾아 제거합니다. - 예를 들어,
[About](/path/to/_index)
를[About](/path/to)
로 변경합니다. - 옵시디언의 링크가
_index.md
를 참조하는 경우, HUGO 에서는 에러로 처리되며, 디렉토리의 경로로 링크를 유지해야 합니다.