mirror of
https://github.com/wahyd4/wahyd4.github.com.git
synced 2026-08-09 05:15:56 +10:00
- 308 posts reverse-extracted from wahyd4.github.com (Hexo 6.3.0 + NexT 8.25) - Hexo project with NexT theme, reading-experience custom styles - publish-post.sh: write post -> hexo build -> PR (master untouched)
103 lines
2.9 KiB
Bash
Executable File
103 lines
2.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# ============================================================
|
|
# publish-post.sh — 写博客并发布(Hermes 调用)
|
|
#
|
|
# 用法:
|
|
# publish-post.sh "文章标题" <content.md 文件路径> ["tag1,tag2"] [--date "YYYY-MM-DD HH:MM:SS"]
|
|
#
|
|
# 流程:
|
|
# 1. 把 markdown 写入 hexo-blog/source/_posts/
|
|
# 2. hexo generate 构建产物
|
|
# 3. 产物同步到 wahyd4.github.com 工作树的新分支 preview/<slug>
|
|
# 4. 推送分支 + 创建 PR (base=master) — master 不动,不部署
|
|
# 5. 输出 PR 链接
|
|
#
|
|
# 预览: 用 demo-service 单独上传 public/ 到 demo.junv.cc/blog-preview/
|
|
# ============================================================
|
|
set -euo pipefail
|
|
|
|
TITLE="$1"
|
|
CONTENT_FILE="$2"
|
|
TAGS="${3:-}"
|
|
DATE_ARG="${4:-}"
|
|
|
|
BLOG_DIR="$HOME/code/blog/hexo-blog"
|
|
POSTS_DIR="$BLOG_DIR/source/_posts"
|
|
PROD_DIR="$HOME/code/blog/wahyd4.github.com"
|
|
REPO="wahyd4/wahyd4.github.com"
|
|
|
|
# --- slug 生成(python 处理中文/特殊字符)---
|
|
slug="$(python3 -c "
|
|
import sys, re
|
|
title = sys.argv[1]
|
|
s = title.lower()
|
|
s = re.sub(r'[^a-z0-9\u4e00-\u9fff]+', '-', s).strip('-')
|
|
print(s[:80] or 'post')" "$TITLE")"
|
|
# 避免与现有 slug 冲突
|
|
n=2
|
|
base="$slug"
|
|
while [ -f "$POSTS_DIR/$slug.md" ]; do
|
|
slug="${base}-${n}"
|
|
n=$((n+1))
|
|
done
|
|
|
|
# --- 生成 front-matter ---
|
|
if [ -n "$DATE_ARG" ]; then
|
|
DATE_LINE="date: $DATE_ARG"
|
|
else
|
|
DATE_LINE="date: $(date '+%Y-%m-%d %H:%M:%S')"
|
|
fi
|
|
|
|
{
|
|
echo "---"
|
|
echo "title: \"$TITLE\""
|
|
echo "$DATE_LINE"
|
|
if [ -n "$TAGS" ]; then
|
|
echo "tags: [$TAGS]"
|
|
fi
|
|
echo "---"
|
|
echo ""
|
|
cat "$CONTENT_FILE"
|
|
} > "$POSTS_DIR/$slug.md"
|
|
|
|
echo "✅ 已写入 $POSTS_DIR/$slug.md"
|
|
|
|
# --- 构建 ---
|
|
cd "$BLOG_DIR"
|
|
npx hexo clean >/dev/null 2>&1 || true
|
|
npx hexo generate 2>&1 | tail -1
|
|
|
|
# --- 同步产物到 wahyd4.github.com 分支 ---
|
|
BRANCH="preview/${slug}"
|
|
cd "$PROD_DIR"
|
|
git fetch origin --quiet
|
|
git checkout -B "$BRANCH" origin/master --quiet
|
|
|
|
# 清理旧产物(除 .git),再拷贝新产物
|
|
find . -path ./.git -prune -o -type f -print0 | xargs -0 rm -f 2>/dev/null || true
|
|
find . -path ./.git -prune -o -type d -empty -print0 | xargs -0 rmdir 2>/dev/null || true
|
|
cp -r "$BLOG_DIR/public/." .
|
|
|
|
git add -A
|
|
if git diff --cached --quiet; then
|
|
echo "⚠️ 产物无变化,跳过提交"
|
|
else
|
|
git -c user.name="Hermes Bot" -c user.email="hermes@junv.cc" commit -m "Site updated: $(date '+%Y-%m-%d %H:%M:%S') — $TITLE" --quiet
|
|
git push -u origin "$BRANCH" --quiet 2>&1 | tail -1 || true
|
|
fi
|
|
|
|
# --- 创建 PR ---
|
|
EXISTING_PR="$(gh pr list --repo "$REPO" --head "$BRANCH" --json number --jq '.[0].number' 2>/dev/null || true)"
|
|
if [ -n "$EXISTING_PR" ]; then
|
|
echo "PR 已存在: https://github.com/$REPO/pull/$EXISTING_PR"
|
|
else
|
|
PR_URL="$(gh pr create --repo "$REPO" --base master --head "$BRANCH" \
|
|
--title "📝 新博客: $TITLE" \
|
|
--body "自动发布:**$TITLE**
|
|
|
|
- 源码: \`source/_posts/$slug.md\`
|
|
- 合并后自动上线 toozhao.com
|
|
- 请 review 后 merge" 2>&1)"
|
|
echo "PR: $PR_URL"
|
|
fi
|