Mac에서 작성한 Obsidian 문서를 윈도나 웹에서 열어보면 간혹 깨진 문자가 섞여있다.
Mac 화면에선 보이지 않고 나머지 시스템에선 보인다.

 <- Mac이 아닌 시스템에서만 보임

이 문자의 정체가 뭘까. JS inspector에 붙여넣어보니

Uncaught SyntaxError: illegal character U+0008

U+0008BACKSPACE였다.

시도들

옵시디언 플러그인

  • 어떤 상황에 저런 글자가 생성되는지 알아보고싶어서 Obsidian에 Control Character라는 플러그인을 찾아 설치해봤다. 여전히 문제의 0x08 문자는 화면 출력이 안된다. 이 플로그인 설치 후 한 가지 문제가 발생했는데, 스페이스 다음의 첫 한글은 작은 점 안에 표시된다.
  • Show Whitespace 플러그인 역시 0x08은 표시해주지 못한다.
  • 이 플러그인들은 공백, 탭, 개행 문자에 특화되어있는 듯 하다. 둘 다 삭제했다.

커밋 시점에 수정?

  • git hook을 이용해 커밋 이벤트에 tr을 사용해 특수문자를 삭제해야하나?
#!/bin/bash
 
# Function to remove control characters from each .md file found
remove_control_chars() {
  local file="$1"
  local lockfile="$file.lock"
 
  # Attempt to create a lock file to prevent concurrent processing
  if ! mkdir "$lockfile" 2>/dev/null; then
    echo "File is already being processed: $file"
    return
  fi
 
  # Ensure the lock is removed even if the script fails
  trap 'rm -rf "$lockfile"' EXIT
 
  # Create a temporary file and remove control characters while preserving multibyte UTF-8
  awk '{
    gsub(/[\x00-\x1F\x7F]/, "");
    print
  }' "$file" > "$file.tmp"
 
  # If the file was modified, update it and echo the updated file name
  if ! cmp -s "$file" "$file.tmp"; then
    echo "Updated Markdown file: $file"
    chmod --reference="$file" "$file.tmp" && mv "$file.tmp" "$file"
  else
    # Remove the temporary file if no changes were made
    rm "$file.tmp"
  fi
 
  # Release the lock
  rm -rf "$lockfile"
}
 
# Export the function so it can be used by 'find'
export -f remove_control_chars
 
# Use find to read all .md files recursively and call remove_control_chars on each file using parallel execution
find . -type f -name "*.md" -print0 | xargs -0 -P 4 -I {} bash -c 'remove_control_chars "$1"' _ {}