linkify script initial

This commit is contained in:
David Holdeman 2022-01-24 16:16:31 -06:00
parent ced305dbf0
commit 262203a4d2
1 changed files with 30 additions and 0 deletions

30
wiki-tools/linkifyurls.sh Normal file
View File

@ -0,0 +1,30 @@
#!/usr/bin/env bash
# These two functions are used to escape variables for use in a sed command
# Passed a single string
escape() {
sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$1";
}
export -f escape
escapeReplace() {
sed 's/[&/\]/\\&/g' <<<"$1";
}
export -f escapeReplace
# Main processing function
# Passed the path to a .md file
searchfile() {
while IFS= read -r -u 3 link; do
echo $link
read -m "Enter a title or leave empty to use URL as title: " TITLE
if [ $(echo -n "$TITLE" | wc -c) -lt 1 ]; then
TITLE="$link"
fi
REPLACE=$(escape "$link")
REPLACEWITH=$(escapeReplace '['"$TITLE"']''('"$link"')')
sed -i "s/$REPLACE/$REPLACEWITH/" "$1"
done 3< <(grep -oP '((?<!\()http[s]?:\/\/[^\s]*)' "$1")
}
export -f searchfile
# run searchfile on every .md file in the repo
find . -iname "*.md" -exec bash -c 'searchfile "$0"' {} \;