diff --git a/README.md b/README.md index 3d14ad2..4f6ab74 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,33 @@ # mdcheckr -Practical CI testing of markdown files + +Practical CI testing for markdown files. + +## Installation + +Requirements: + +- curl +- xmllint +- pandoc + +```bash +git clone https://github.com/mike42/mdcheckr +cp mdcheckr/mdcheckr /usr/local/bin +``` + +## Usage + +Check a single markdown file + +Check all the markdown files in the current directory: + +find . -name '*.md' -print0 | xargs -0 mdcheckr + +Check all the markdown files in your Git repository: + +```bash +git ls-files | grep '\.md$' | tr '\n' '\0' | xargs -0 mdcheckr +``` + + + diff --git a/mdcheckr b/mdcheckr new file mode 100755 index 0000000..23dca36 --- /dev/null +++ b/mdcheckr @@ -0,0 +1,49 @@ +#!/bin/bash +set -e + +# Extract URLs from Markdown file via Pandoc +function extract_links() { + pandoc -f markdown_github "$fn" | (xmllint --nowarning --html --xpath '*//a/@href' - 2> /dev/null || true) | sed 's/ href="\([^"]*\)"/\1\n/g' +} + +function green() { + echo -e "\033[32m[ \e[1m$1\e[21m ]\e[0m" +} + +function red() { + echo -e "\033[31m[ \e[1m$1\e[21m ]\e[0m" +} + +function explain { + >&2 echo -e Warning:$1:$2 +} + +DIR=`pwd` + +for i in $@; do + cd "$DIR" + fn=$(basename "$i") + # Jump into same dir as file + cd `dirname "$i"` + echo "Checking $i .." + extract_links "$fn" | while read -r line; do + # Notify of check + echo -n "- $line "; + fail=0 + # Do check + if [ ! -f "$line" ]; then + if ! curl --silent --head --location --fail "$line" > /dev/null; then + fail=1 + REASON="Broken link to '$line'" + fi + fi + # Result + if [ "$fail" == 1 ]; then + red "FAIL" + explain "$i" "$REASON" + else + green "OK" + fi + done; +done +cd $DIR