| #!/usr/bin/env bash |
| # Licensed to the Apache Software Foundation (ASF) under one |
| # or more contributor license agreements. See the NOTICE file |
| # distributed with this work for additional information |
| # regarding copyright ownership. The ASF licenses this file |
| # to you under the Apache License, Version 2.0 (the |
| # "License"); you may not use this file except in compliance |
| # with the License. You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, |
| # software distributed under the License is distributed on an |
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| # KIND, either express or implied. See the License for the |
| # specific language governing permissions and limitations |
| # under the License. |
| |
| set -euo pipefail |
| |
| MODE="check" |
| FILES=() |
| |
| # Accept mode flags plus optional file paths. Pre-commit passes matching |
| # markdown files as positional arguments. |
| while [[ $# -gt 0 ]]; do |
| case "$1" in |
| --check) |
| MODE="check" |
| shift |
| ;; |
| --fix) |
| MODE="fix" |
| shift |
| ;; |
| --help|-h) |
| echo "Usage: $0 [--check|--fix] [files...]" |
| echo " --check Check markdown files for issues (default)" |
| echo " --fix Automatically fix markdown issues" |
| exit 0 |
| ;; |
| -*) |
| echo "Unknown option: $1" |
| echo "Use --help for usage information" |
| exit 1 |
| ;; |
| *) |
| FILES+=("$1") |
| shift |
| ;; |
| esac |
| done |
| |
| # Files to ignore (in addition to .gitignore) |
| # helm/charts/iggy/README.md is auto-generated by helm-docs |
| IGNORE_ARGS=(--ignore "helm/charts/iggy/README.md") |
| |
| # Default to the full repository for CI/manual runs, but use the explicit file |
| # list when pre-commit or a caller passes paths directly. |
| if [ ${#FILES[@]} -gt 0 ]; then |
| TARGETS=("${FILES[@]}") |
| else |
| TARGETS=('**/*.md') |
| fi |
| |
| # Check if markdownlint is installed |
| if ! command -v markdownlint &> /dev/null; then |
| echo "❌ markdownlint command not found" |
| echo "💡 Install it using: npm install -g markdownlint-cli" |
| exit 1 |
| fi |
| |
| if [ "$MODE" = "fix" ]; then |
| echo "🔧 Fixing markdown files..." |
| markdownlint "${TARGETS[@]}" --ignore-path .gitignore "${IGNORE_ARGS[@]}" --fix |
| echo "✅ Markdown files have been fixed" |
| else |
| echo "🔍 Checking markdown files..." |
| if markdownlint "${TARGETS[@]}" --ignore-path .gitignore "${IGNORE_ARGS[@]}"; then |
| echo "✅ All markdown files are properly formatted" |
| else |
| echo "❌ Markdown linting failed" |
| echo "💡 Run '$0 --fix' to auto-fix issues" |
| exit 1 |
| fi |
| fi |