~kris/dots

srice

ref: 512b7381d0155d6247fad64b640b0704496a8035 srice/.local/bin/blog-backlinks -rwxr-xr-x 653 bytes
512b7381 — Kris Yotam mksh: port bashisms to mksh-safe (process subs, DEBUG-trap history hook, direnv, cd- alias) 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env sh
set -eu

tmp=${TMPDIR:-.}/blog-backlinks.$$
trap 'rm -f "$tmp"' EXIT HUP INT TERM

find . \( -path '*/.git' -o -path '*/node_modules' -o -path '*/.next' -o -path '*/_site' \) -prune -o -type f \( -name '*.md' -o -name '*.mdx' -o -name '*.org' \) -print > "$tmp"

while IFS= read -r target; do
  slug=$(basename "$target" | sed -e 's/\.[^.]*$//' -e 's/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-//')
  [ -n "$slug" ] || continue
  hits=$(xargs -r rg -l --fixed-strings "$slug" < "$tmp" | grep -Fv -- "$target" || true)
  [ -n "$hits" ] || continue
  printf '## %s\n\n' "$target"
  printf '%s\n\n' "$hits" | sed 's/^/- /'
done < "$tmp"