Last active
March 20, 2021 09:59
-
-
Save FallenMax/e845f215b44b6cfd822958f1dfe68751 to your computer and use it in GitHub Desktop.
remove orphan images from markdown files generated by Typora
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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