Files
jsc-marketplace/plugins/jsc-wiki/scripts/wiki.sh
rootandClaude Sonnet 5.5 7ecb846767 feat: 新增 jsc-wiki 外掛
讀取本 repo wiki 的「JSC 知識庫」,依領域提供 /jsc-wiki:wiki、system、network、virt、container、database、dev、ai、apps 指令。

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 04:51:23 +00:00

36 lines
1.4 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 讀取 JSC 知識庫 wiki(唯讀)。第一次 clone 到快取,之後每次先 pull;連不上就用現有快取。
# 用法:wiki.sh toc | chapter <N> | page <編號或關鍵字> | search <關鍵字>
set -u
REPO="${JSC_WIKI_URL:-https://gitea.jsc.idv.tw/jiantw83/jsc-marketplace.wiki.git}"
DIR="${JSC_WIKI_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/jsc-wiki}"
sync() {
if [ -d "$DIR/.git" ]; then
timeout 20 git -C "$DIR" pull -q --ff-only 2>/dev/null || echo "(無法更新 wiki,使用本機快取)" >&2
else
mkdir -p "$(dirname "$DIR")"
timeout 60 git clone -q --depth 1 "$REPO" "$DIR" 2>/dev/null || { echo "無法取得 wiki:$REPO" >&2; exit 1; }
fi
}
cmd="${1:-toc}"; shift || true
sync
cd "$DIR" || exit 1
case "$cmd" in
toc) cat Home.md ;;
chapter)
n="${1:?請給章號 1-8}"
cat "第${n}章-"*.md | sed -E "s/\]\([^)]*\)/]/" ;;
page)
q="${1:?請給頁面編號(例 2.3)或標題關鍵字}"
f=$(ls | grep -E "^${q//./\\.}-" | head -1)
[ -n "$f" ] || f=$(ls | grep -i -- "$q" | grep -v '^第' | head -1)
[ -n "$f" ] || { echo "找不到頁面:$q" >&2; exit 1; }
echo "## 頁面:${f%.md}"; echo; cat "$f" ;;
search)
q="${1:?請給關鍵字}"
grep -rniI --include='*.md' -- "$q" . | grep -v '^./Home.md' | cut -c3-200 | head -40 ;;
*) echo "未知指令:$cmd" >&2; exit 2 ;;
esac