#!/bin/sh

getyml() {
	grep "^$1:" "$2" | head -n 1 | sed "s/$1"': //;s/^"\(.*\)"$/\1/'
}

prev_year=''

ls _content/posts | tac | while read -r l; do
	yml="_content/posts/$l/index.md"
	if grep -q 'draft: true' "$yml"; then
		continue
	fi

	year=$(echo "$l" | cut -d- -f1)
	if [ "$year" != "$prev_year" ]; then
		echo
		echo "## $year"
		echo
		prev_year="$year"
	fi

	echo "- [$(getyml 'title' "$yml")](/posts/$l/){data-tags=\"$(getyml 'tags' "$yml" | sed 's/^\[\(.*\)\]$/\1/;s/, */ /g')\"}"
done

echo
echo '<script src="/static/blogsearch.js" type="module"></script>'
