#!/bin/bash
set -euo pipefail
# set -x
HTML=${1:-}
MARKDOWN=${2:-}
if [ -z "$HTML" -o -z "$MARKDOWN" ]; then
echo "Usage: $0 HTML MARKDOWN"
exit 1
fi
if [ ! -f "${HTML}" ]; then
echo "HTML file not found"
exit 1
fi
if [ ! -f "${MARKDOWN}" ]; then
echo "MARKDOWN file not found"
exit 1
fi
regex='([^<]+)0; i--)); do toc="${toc} "; done
toc="${toc}* [${header}](#${id})"
fi
done < "${HTML}"
# fix sed on mac
sed='sed -i'
if [[ "$(uname)" == "Darwin" ]]; then
sed='sed -i ""'
fi
ts="<\!--ts-->"
te="<\!--te-->"
existingTOC="$(cat "$MARKDOWN" | sed -n "/${ts}/,/${te}/p" | sed '1,1d; $d')"
if [ "$toc" != "$existingTOC" ]; then
tmp="$(mktemp)"
function finish {
rm "$tmp"
}
trap finish EXIT
echo "${toc}" > "${tmp}"
# clear old toc
$sed "/${ts}/,/${te}/{//!d;}" "$MARKDOWN"
# insert toc
$sed "/${ts}/r ${tmp}" "$MARKDOWN"
fi