#!/bin/sh

# This script will pass provided arguments to markdownlint (mdl), with
# Kramdown warnings enabled, but with the GitLab-specific (and
# non-standard) [[_TOC_]] blob removed. This only works on single
# files, if provided a directory, it will just throw the entire thing
# at mdl.
for path in "$@"; do
    if [ -f "$path" ]; then
        case "$path" in
            *.md|*.mdwn|*.markdown)
                echo "checking file $path..."
                # this also removes [x] style checklists which kramdown doesn't like
                sed 's/^\[\[_TOC_\]\]/TOC_PLACEHOLDER/;s/^ *\([*-]\|[0-9][0-9]*\.\)  *\[[x ]\] /\* /' "$path" | mdl - ;;
        esac
    else
        echo "checking $path..."
        mdl "$path"
    fi
done
