Skip to content

Instantly share code, notes, and snippets.

@FallenMax
Last active March 20, 2021 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FallenMax/e845f215b44b6cfd822958f1dfe68751 to your computer and use it in GitHub Desktop.
Save FallenMax/e845f215b44b6cfd822958f1dfe68751 to your computer and use it in GitHub Desktop.
remove orphan images from markdown files generated by Typora
#!/usr/bin/env bash
# does $1 contains $2?
#
# $1: array
# $2: element to test
function array_contains() {
# local IFS=$'\n'
local array=($1)
local i
for ((i = 0; i < ${#array[@]}; i++)); do
if [[ "${array[$i]}" == "$2" ]]; then
return 0
fi
done
return 1
}
function prune() {
set -euo pipefail
local root=${1:-.}
echo "start prune: $root"
echo "------------------"
local IFS=$'\n'
local imageRefs=($(grep --ignore-case -E "[^\/]+\.(png|jpg|jpeg|gif)" -RI "$root" --no-filename --only-matching))
local images=($(find "$root" -name '*.png' -or -name '*.jpg' -or -name '*.jpeg' -or -name '*.gif'))
local image
local imagename
local i
for ((i = 0; i < ${#images[@]}; i++)); do
image="${images[$i]}"
imagename=${image##*/}
if ! array_contains "${imageRefs[*]}" "$imagename"; then
echo "removing: ${image}"
rm "${image}"
fi
done
echo "------------------"
echo "done prune: $root"
}
prune "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment