# TriliumNext WechatSync Publisher Patch # 将补丁应用到 TriliumNext 源码,并处理 Docker 网络差异 # # 用法: bash patch.sh /path/to/triliumnext set -e TRILIUM_DIR="${1:-/path/to/trilium}" HERE="$(cd "$(dirname "$0")" && pwd)" echo "Applying patches to $TRILIUM_DIR..." # --- 源码补丁 --- copy_patch() { local src="$1" dst="$2" label="$3" if [ -f "$src" ]; then cp "$src" "$dst" && echo " ✅ $label" else echo " ⚠️ 缺失: $src" fi } copy_patch "$HERE/wechatsync.ts" "$TRILIUM_DIR/apps/server/src/services/publisher/wechatsync.ts" "wechatsync.ts" copy_patch "$HERE/publisher_frontend.ts" "$TRILIUM_DIR/apps/server/src/routes/api/publisher_frontend.ts" "publisher_frontend.ts" copy_patch "$HERE/publish.html" "$TRILIUM_DIR/apps/client/public/publish.html" "publish.html" # --- 构建 --- echo "" echo "Rebuilding TriliumNext server..." cd "$TRILIUM_DIR" && pnpm server:build # --- Docker 运行时修正 --- # 若 Trilium 在容器内运行,源码里的 host.docker.internal 已生效; # 但若你直接用镜像(未从源码构建),需对已编译的 main.cjs 打补丁: if [ -f "/.dockerenv" ] || grep -qa docker /proc/1/cgroup 2>/dev/null; then echo "" echo "🐳 Docker 环境:修正已编译产物桥接地址..." MC="$TRILIUM_DIR/apps/server/dist/main.cjs" if [ -f "$MC" ]; then sed -i 's|http://localhost:9601|http://host.docker.internal:9601|g' "$MC" 2>/dev/null || true sed -i 's|http://localhost:${Hca}/status|http://host.docker.internal:${Hca}/status|g' "$MC" 2>/dev/null || true sed -i 's|SYNC_WS_PORT:"9600"|SYNC_WS_PORT:"9600",SYNC_WS_HOST:"host.docker.internal"|g' "$MC" 2>/dev/null || true echo " ✅ main.cjs 桥接地址已修正" fi # 在容器内安装 wechatsync CLI 并修正其 hostname if ! command -v wechatsync &>/dev/null; then echo " 📦 安装 wechatsync CLI..." npm install -g @wechatsync/cli 2>/dev/null && echo " ✅ CLI installed" || echo " ⚠️ CLI 安装失败" fi CLI_JS=$(find /usr/local/lib/node_modules/@wechatsync/cli/dist -name "index.js" 2>/dev/null | head -1) if [ -n "$CLI_JS" ]; then sed -i 's/hostname: "localhost"/hostname: process.env.SYNC_WS_HOST || "host.docker.internal"/g' "$CLI_JS" 2>/dev/null \ && echo " ✅ CLI hostname 已修正" || true fi # 把前端页面拷入已编译产物目录(镜像内路径可能不同) for dst in "$TRILIUM_DIR/apps/server/dist/public/publish.html" /usr/src/app/dist/public/publish.html; do [ -d "$(dirname "$dst")" ] && cp "$HERE/publish.html" "$dst" 2>/dev/null && echo " ✅ 页面已更新: $dst" || true done fi echo "" echo "✅ 补丁完成!重启 TriliumNext 生效:" echo " systemctl restart triliumnext # 裸机" echo " docker restart triliumnext # Docker"