关于图片处理、多设备同步写作和自动化脚本

1. archetypes/default.md 使用方法

什么是 archetypes?

Hugo 的文章模板,新建文章时自动填充 frontmatter。

创建模板文件

mkdir -p archetypes
cat > archetypes/default.md << 'EOF'
---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
tags: []
categories: []
description: ""
cover:
    image: "images/cover.jpg"
    alt: "封面图"
    caption: ""
---

## 引言

<!-- 文章简介 -->

## 正文

<!-- 主要内容 -->

## 参考

- 
EOF

使用模板新建文章

# 自动套用 archetypes/default.md
hugo new content/posts/我的新文章.md

# 结果:生成的文件已包含模板内容
cat content/posts/我的新文章.md

2. 图片处理方案

推荐目录结构

my-blog/
├── content/
│   └── posts/
│       ├── 我的文章/
│       │   ├── index.md          # 文章正文
│       │   └── images/           # 配图放这里
│       │       ├── diagram.png
│       │       └── screenshot.jpg
│       └── 另一篇文章/
│           ├── index.md
│           └── images/
└── static/
    └── images/                   # 全局共享图片
        └── logo.png

文章内引用图片

方式 A:文章 bundle(推荐)

![截图](images/screenshot.jpg)

方式 B:全局图片

![Logo](/images/logo.png)

图片压缩脚本(保持轻量)

# 安装 imagemagick
sudo apt install imagemagick

# 压缩脚本 compress-images.sh
cat > compress-images.sh << 'EOF'
#!/bin/bash
find content -name "*.jpg" -o -name "*.png" | while read img; do
    convert "$img" -resize 1200x1200\> -quality 85 "$img"
    echo "Compressed: $img"
done
EOF
chmod +x compress-images.sh

3. 多设备同步写作

方案:Git + Gitee/GitHub

设备 操作
电脑 A 写作 → git push
电脑 B git pull → 继续写作
手机 GitHub App 或 Working Copy 查看

配置双推送(Gitee + GitHub)

# 添加两个 remote
git remote add gitee https://gitee.com/用户名/博客.git
git remote add github https://github.com/用户名/博客.git

# 推送脚本 push-all.sh
cat > push-all.sh << 'EOF'
#!/bin/bash
git add .
git commit -m "update: $(date '+%Y-%m-%d %H:%M')"
git push gitee main
git push github main
EOF
chmod +x push-all.sh

4. 自动部署脚本(完整版)

Linux/Mac/WSL 版本(deploy.sh

#!/bin/bash

PROJECT_NAME="你的项目名"

echo "=== 1. 压缩图片 ==="
./compress-images.sh 2>/dev/null || echo "跳过图片压缩"

echo "=== 2. 构建站点 ==="
hugo --minify

echo "=== 3. 部署到 Cloudflare ==="
wrangler pages deploy public --project-name=$PROJECT_NAME

echo "=== 4. 完成 ==="
echo "访问: https://$PROJECT_NAME.pages.dev"

Windows 版本(deploy.bat

@echo off
set PROJECT_NAME=你的项目名

echo === 1. 构建站点 ===
hugo --minify

echo === 2. 部署到 Cloudflare ===
wrangler pages deploy public --project-name=%PROJECT_NAME%

echo === 3. 完成 ===
pause

一键写作+部署脚本(new-and-deploy.sh

#!/bin/bash

# 用法: ./new-and-deploy.sh "文章标题"

TITLE="${1:-新文章}"
SLUG=$(echo "$TITLE" | sed 's/ /-/g' | tr '[:upper:]' '[:lower:]')

echo "=== 创建文章: $TITLE ==="
hugo new content/posts/$SLUG/index.md

echo "=== 打开编辑器 ==="
code content/posts/$SLUG/index.md  # VS Code

echo "=== 等待编辑完成,按 Enter 部署 ==="
read

echo "=== 构建并部署 ==="
hugo && wrangler pages deploy public --project-name=你的项目名

使用:

./new-and-deploy.sh "我的新文章"

5. 完整工作流图

┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│  本地写作    │ ──→ │  hugo server │ ──→ │  本地预览    │
│  (VS Code)  │     │  (localhost) │     │ (127.0.0.1) │
└─────────────┘     └─────────────┘     └─────────────┘
┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│  图片压缩    │ ──→ │  hugo构建    │ ──→ │  wrangler   │
│ compress.sh  │     │  (--minify)  │     │   deploy    │
└─────────────┘     └─────────────┘     └─────────────┘
                                         ┌─────────────┐
                                         │ Cloudflare  │
                                         │   Pages     │
                                         │   (上线)    │
                                         └─────────────┘

6. 快速指令汇总

需求 指令
新建文章 hugo new content/posts/xxx.md
本地预览 hugo server -D
构建 hugo --minify
部署 ./deploy.shdeploy.bat
写作+部署 ./new-and-deploy.sh "标题"
双推送 ./push-all.sh