Files
Serendipity 8ccc32be0b feat(halo): 添加图片上传功能并完善发布流程
- 实现图片上传服务,支持检测并上传本地图片到 Halo
- 优化发布流程,添加详细日志和错误处理
- 更新任务清单和检查列表以反映完成状态
- 添加 Halo 博客写作技能文档
2026-04-26 16:47:41 +08:00

253 lines
5.3 KiB
Markdown
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.
# Halo CLI 高级用法
本文件包含 Halo CLI 的高级用法和示例场景。
## 内容目录
1. [批量操作](#批量操作)
2. [自动化脚本](#自动化脚本)
3. [内容迁移](#内容迁移)
4. [SEO 优化](#seo-优化)
---
## 批量操作
### 批量导出文章
```powershell
# 导出所有文章为 JSON
halo post list --page 1 --size 100 --json > all_posts.json
# 批量导出为 Markdown
$posts = halo post list --page 1 --size 100 --json | ConvertFrom-Json
foreach ($post in $posts.items) {
halo post export-markdown $post.name --output "./export/$($post.name).md"
}
```
### 批量更新标签
```powershell
# 给多篇文章添加标签
$posts = halo post list --page 1 --size 20 --json | ConvertFrom-Json
foreach ($post in $posts.items) {
halo post update $post.name --tags "Python,教程,技术分享"
}
```
### 批量发布
```powershell
# 将所有草稿发布
$posts = halo post list --page 1 --size 100 --json | ConvertFrom-Json
foreach ($post in $posts.items) {
if ($post.status -eq "draft") {
halo post update $post.name --publish true
Write-Host "已发布: $($post.title)"
}
}
```
---
## 自动化脚本
### 文章备份脚本
```powershell
# backup-halo-posts.ps1
param(
[int]$PageSize = 50
)
$backupDir = ".\halo-backup\$(Get-Date -Format 'yyyy-MM-dd')"
New-Item -ItemType Directory -Path $backupDir -Force | Out-Null
$page = 1
$totalExported = 0
do {
Write-Host "正在导出第 $page 页..."
$result = halo post list --page $page --size $PageSize --json | ConvertFrom-Json
foreach ($post in $result.items) {
$filename = "$backupDir\$($post.name).md"
halo post export-markdown $post.name --output $filename
$totalExported++
}
$page++
} while ($result.hasNext)
Write-Host "备份完成!共导出 $totalExported 篇文章到 $backupDir"
```
### 定时发布脚本
```powershell
# scheduled-publish.ps1
param(
[string]$FilePath
)
if (-not (Test-Path $FilePath)) {
Write-Error "文件不存在: $FilePath"
exit 1
}
$content = Get-Content $FilePath -Raw
$title = [System.IO.Path]::GetFileNameWithoutExtension($FilePath)
Write-Host "正在发布: $title"
halo post create --title $title --content $content --publish true
if ($LASTEXITCODE -eq 0) {
Write-Host "发布成功!"
} else {
Write-Error "发布失败"
exit 1
}
```
---
## 内容迁移
### 从其他平台导入
```powershell
# 从 WordPress 导出文件导入
halo post import-markdown --file .\wordpress-export\post-1.md
halo post import-markdown --file .\wordpress-export\post-2.md
# 批量导入
Get-ChildItem .\wordpress-export\*.md | ForEach-Object {
halo post import-markdown --file $_.FullName
Write-Host "已导入: $($_.Name)"
}
```
### 迁移到其他平台
```powershell
# 导出为标准格式
halo post list --page 1 --size 100 --json > migration.json
# 转换为其他 CMS 格式(如 Hugo、Jekyll
# 需要额外的转换脚本处理 JSON 到目标格式
```
---
## SEO 优化
### 检查文章 SEO
```powershell
# 获取最近 10 篇文章的 SEO 信息
$posts = halo post list --page 1 --size 10 --json | ConvertFrom-Json
foreach ($post in $posts.items) {
$detail = halo post get $post.name --json | ConvertFrom-Json
Write-Host "文章: $($detail.title)"
Write-Host " - 字数: $($detail.wordCount)"
Write-Host " - 标签: $($detail.tags -join ', ')"
Write-Host " - URL: $($detail.slug)"
Write-Host ""
}
```
### 批量更新 SEO 描述
```powershell
# 为所有文章添加 SEO 描述
$posts = halo post list --page 1 --size 50 --json | ConvertFrom-Json
foreach ($post in $posts.items) {
$detail = halo post get $post.name --json | ConvertFrom-Json
# 生成描述(前 160 个字符)
$excerpt = $detail.content -replace '<[^>]+>', '' # 去除 HTML 标签
$excerpt = $excerpt.Substring(0, [Math]::Min(160, $excerpt.Length))
halo post update $post.name --description $excerpt
}
```
---
## 高级配置
### 使用不同 Profile
```powershell
# 列出所有配置
halo auth profile list
# 使用指定配置
halo post list --profile production
# 创建新配置
halo auth login --profile aliyun --url http://101.133.128.193:8091 --auth-type bearer --token <token>
```
### 环境变量配置
```powershell
# 临时修改配置目录
$env:HALO_CLI_CONFIG_DIR = "d:\halo-config"
# 使用代理
$env:HTTPS_PROXY = "http://proxy:8080"
# 禁用 SSL 验证(不推荐)
$env:HALO_CLI_INSECURE_SSL = "true"
```
### 调试模式
```powershell
# 显示详细输出
halo post list --page 1 --size 20 --verbose
# 查看配置
halo auth current --verbose
# 查看请求/响应
halo post get <id> --debug
```
---
## 常见问题解决
### Token 过期
```powershell
# 刷新 Token
halo auth login --profile local --url http://192.168.5.8:8091 --auth-type bearer --token <new-token>
```
### 网络问题
```powershell
# 使用超时设置
halo post list --timeout 30
# 重试机制
$maxRetries = 3
for ($i = 1; $i -le $maxRetries; $i++) {
halo post list --page 1 --size 20
if ($LASTEXITCODE -eq 0) { break }
Start-Sleep -Seconds 5
}
```
### 并发限制
Halo API 有请求限制,高并发操作可能导致失败。建议:
- 使用 `--rate-limit 10`(如果支持)
- 添加延迟:`Start-Sleep -Milliseconds 100`
- 批量操作使用 `--batch` 标志(如果支持)