add link fixing tool

This commit is contained in:
David Holdeman 2021-02-15 20:21:10 -06:00
parent 2d8240368d
commit c7c9b5d848
1 changed files with 28 additions and 0 deletions

28
wiki-tools/brokenlinks.sh Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env bash
escape() {
sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$1";
}
export -f escape
escapeReplace() {
sed 's/[&/\]/\\&/g' <<<"$1";
}
export -f escapeReplace
searchfile() {
LINKS=$(grep -oP '(?<=\]\().+?(?=[\)| ])' "$1")
for link in $LINKS; do
if echo $link | grep -E '^http' >/dev/null; then
continue
fi
if echo $link | grep -E ".md$" && find . -name "$link"; then
continue
fi
echo $link
FILES=$(find . -iname "*$(basename $link)*")
echo "$FILES" | cat --number
read PICK
FILE=$(basename $(echo "$FILES" | head -n $PICK | tail -n 1) .md)
sed -i "s/$(escape '('$link')')/\($(escapeReplace $FILE)\)/" "$1"
done
}
export -f searchfile
find . -iname "*.md" -exec bash -c 'searchfile "$0"' {} \;