gh2md
gh2md copied to clipboard
with --multiple-files, generate readme.md index file
for better navigation
navigating an anonymous file list like
2020-09-22.1.issue.closed.md
2020-09-22.2.issue.closed.md
2020-11-04.3.issue.closed.md
2021-07-31.4.issue.open.md
2022-05-31.7.issue.open.md
2022-06-01.8.issue.open.md
2022-06-05.9.issue.open.md
is not user friendly
workaround in bash: archive-github-issues.sh
archive-github-issues.sh
#! /usr/bin/env bash
set -e
set -u
set -x
# check dependencies
git --version
gh2md --version
pandoc --version
if ! (git branch --format="%(refname)" | grep -q -x "refs/heads/github-issues"); then
echo creating orphan branch github-issues
git branch --copy master master-temp-copy
git worktree add github-issues master-temp-copy
git -C github-issues switch --orphan github-issues
git branch --delete master-temp-copy
else
echo mounting branch github-issues
git worktree add github-issues github-issues || true
fi
# run gh2md
git -C github-issues rm -rf ghmd || true
rm -rf github-issues/ghmd || true
gh2md --multiple-files --file-extension .ghmd --idempotent btdig/dhtcrawler2 github-issues/ghmd
# convert ghmd to md files
git -C github-issues rm -rf md || true
rm -rf github-issues/md || true
mkdir github-issues/md || true
find github-issues/ghmd -type f -name "*.ghmd" | while read ghmd_path; do
md_path=${ghmd_path%.*}.md
md_path=github-issues/md/${md_path#*/*/}
pandoc -f gfm -t commonmark "$ghmd_path" -o "$md_path" --wrap=none
done
# remove ghmd files to save space
git -C github-issues rm -rf ghmd || true
rm -rf github-issues/ghmd || true
# generate index file
# workaround for https://github.com/mattduck/gh2md/issues/33
(
#echo "# github issues"
#echo
# loop sections
# TODO add more sections
# TODO? generate multiple index files, one file per section
# FIXME skip empty sections
for selector in "open issue" "closed issue" "merged pr"; do
section="${selector}s"
selected_status=${selector% *}
selected_type=${selector#* }
echo "<h2>$section</h2>"
echo '<details>'
echo "<summary>$section</summary>"
echo '<table>'
find github-issues/md -type f -name "*.$selected_type.$selected_status.md" | sort -n --reverse | while read md_path; do
# 2021-06-16.1.pr.merged.md 2023-08-07.2.issue.open.md 2023-08-07.3.issue.open.md 2023-08-08.4.issue.open.md
md_name=$(basename "$md_path")
md_url="md/$md_name"
number=$(echo "$md_name" | cut -d. -f 2)
type=$(echo "$md_name" | cut -d. -f 3) # "pr" or "issue"
status=$(echo "$md_name" | cut -d. -f 4) # pr: merged/?/?, issue: open/?
num_comments=$(cat "$md_path" | grep -E '^#### <img src="https://avatars\.githubusercontent\.com/u/[0-9]+\?(u=[0-9a-f]+&)?v=[0-9]+" width="50">\[.*?\]\(https://github.com/.*?\) commented at \[.*\):$' | wc -l)
text=$(pandoc -f commonmark -t plain --wrap=none "$md_path")
title=$(echo "$text" | head -n1 | cut -d' ' -f4-)
author=$(echo "$text" | head -n3 | tail -n1 | cut -d' ' -f1)
datetime=$(echo "$text" | head -n3 | tail -n1 | cut -d' ' -f5-6 | sed 's/:$//')
# FIXME: use only the first comment
start=$(echo "$text" | tail -n +5 | head -c 200)
start_escaped=$(echo "$start" | sed 's/"/\"/g' | perl -0777 -pe 's/\n/ /g')
# debug
echo "start: $start" >&2
echo "start_escaped: $start_escaped" >&2
#echo '<tr>'
if false; then
# column: icon
if [[ "$type" == "issue" ]]; then
if [[ "$status" == "open" ]]; then
echo '<td><svg class="octicon octicon-issue-opened open" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z"></path><path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM1.5 8a6.5 6.5 0 1 0 13 0 6.5 6.5 0 0 0-13 0Z"></path></svg></td>'
else
echo '<td>TODO icon</td>'
fi
else
echo '<td>TODO icon</td>'
fi
else
# column: type
#echo "<td>$type</td>"
# column: status
#echo "<td>$status</td>"
# column: status + type
#echo "<td>$status $type</td>"
:
fi
# column: title, number, date, author
#echo "<td><b><a href=\"$md_url\">$title</a></b><br>#$number opened on $datetime by $author</td>"
comments_str=$(if ((num_comments > 0)); then echo " 💬 $num_comments"; else echo ""; fi)
echo "<tr><td><b><a href=\"$md_url\" title=\"$start_escaped\">$title</a></b><br>#$number opened on $datetime by $author$comments_str</td></tr>"
# column: number of replies
# https://www.fileformat.info/info/unicode/char/1f4ac/index.htm
if false; then
if ((num_comments > 0)); then
echo "<td>💬 $num_comments</td>"
else
echo "<td></td>"
fi
fi
#echo '</tr>'
done
echo '</table>'
echo '</details>'
done
) >github-issues/readme.md
git -C github-issues add .
git -C github-issues commit -m "update github issues"
example result: github-issues