diff --git a/.github/workflows/broken-img.yml b/.github/workflows/broken-img.yml new file mode 100644 index 00000000..d3112b45 --- /dev/null +++ b/.github/workflows/broken-img.yml @@ -0,0 +1,57 @@ +name: Validating Images + +on: + pull_request: + types: [opened, synchronize, reopened] + paths: + - '**/*.md' + +jobs: + read-markdown: + runs-on: ubuntu-22.04 + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 1 + + - name: validate image formats + run: | + changed_files=$(git diff --name-only HEAD^ HEAD | grep '\.md$') + + has_invalid_images=false + + for file in $changed_files; do + if [ -f "$file" ]; then + echo "Checking $file for invalid image lines:" + # Find lines in ![alt](image-path) format + grep -n -E '!\[.*\]\(.*\)' "$file" | while IFS= read -r line; do + # Extract the image path syntax + image_path=$(echo "$line" | sed -E 's/.*!\[.*\]\((.*)\).*/\1/') + + # Check if image path is empty + if [ -z "$image_path" ]; then + echo "::warning file=$file::Empty image path in line: $line" + has_invalid_images=true + continue + fi + + # Check image path extension (png, jpg, jpeg, gif, svg, webp) + + if ! echo "$image_path" | grep -qE '\.(png|jpg|jpeg|gif|svg|webp)$'; then + echo "::warning file=$file::Invalid or missing image extension in line: $line" + has_invalid_images=true + fi + done + else + echo "::warning::File $file not found" + has_invalid_images=true + fi + done + + if [ "$has_invalid_images" = true ]; then + echo "Validation failed: Found invalid image formats in Markdown files" + exit 1 + else + echo "All image formats are valid" + fi \ No newline at end of file