#!/bin/bash # ============================================================================ # SEO 检查脚本 - 检查文章中的链接和SEO问题 # ============================================================================ # 配置 WORKSPACE="/root/.openclaw/workspace" HQBSH_DIR="${WORKSPACE}/hqbsh" SEO_LOG="/var/log/openclaw/seo-check.log" mkdir -p "$(dirname "$SEO_LOG")" log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$SEO_LOG" } # ============================================================================ # 检查问题链接 # ============================================================================ check_broken_links() { local file="$1" local issues=() # 1. 检查伪链接格式 [标题]() while IFS= read -r line; do if echo "$line" | grep -qE '\[.+\]\(\s*\)'; then issues+=("空括号链接: $line") fi done < "$file" # 2. 检查锚点伪链接 [标题](#) while IFS= read -r line; do if echo "$line" | grep -qE '\[.+\]\(#\)'; then issues+=("空锚点链接: $line") fi done < "$file" # 3. 检查纯文本"原贴"字样但无链接 if grep -qE '原贴.*:$' "$file"; then issues+=("原贴后面没有链接") fi # 4. 检查链接格式不完整 [标题](链接不完整 if grep -qE '\]\([^\)]+$' "$file"; then issues+=("未闭合的链接括号") fi printf '%s\n' "${issues[@]}" } # ============================================================================ # 检查SEO元素 # ============================================================================ check_seo_elements() { local file="$1" local issues=() # 检查是否缺少 H1/H2 标题 if ! grep -qE '^# ' "$file"; then issues+=("缺少 H1 标题 (# 标题)") fi # 检查图片是否缺少 alt 文本 while IFS= read -r line; do if echo "$line" | grep -qE '!\[\]\('; then issues+=("图片缺少 alt 文本: $line") fi done < "$file" # 检查段落是否太长(超过300字) local content=$(cat "$file" | sed 's/```.*//g' | sed 's/```//g') local paragraphs=$(echo "$content" | awk '/^[^#*>]/ {if(length > 200) print NR": "$0}' | head -5) if [ -n "$paragraphs" ]; then issues+=("长段落可能影响阅读性") fi printf '%s\n' "${issues[@]}" } # ============================================================================ # 生成SEO优化建议 # ============================================================================ generate_seo_suggestions() { local file="$1" local suggestions=() # 建议1: 外部链接应该添加真实URL if grep -qE '\[.+\]\(\s*\)' "$file"; then suggestions+=("需要修复空链接") fi # 建议2: 内部链接应该添加锚点ID if grep -qE '\[.+\]\(#.*\)' "$file"; then suggestions+=("内部锚点链接格式正确 ✓") else if grep -qE '^## ' "$file"; then suggestions+=("建议添加目录和锚点链接") fi fi # 建议3: 元数据 if ! grep -qE '^> ' "$file"; then suggestions+=("建议添加作者/时间等元数据") fi printf '%s\n' "${suggestions[@]}" } # ============================================================================ # 修复问题链接 # ============================================================================ fix_links() { local file="$1" local fixed=0 # 修复空括号链接 [标题]() if grep -qE '\[.+\]\(\s*\)' "$file"; then echo "发现空链接,正在修复..." # 将 [标题]() 转换为 [标题](真实链接占位) sed -i 's/\]\(\s*\)/](请添加真实链接)/g' "$file" fixed=$((fixed + 1)) fi # 修复原贴链接 if grep -qE '原贴.*:$' "$file"; then echo "发现原贴占位符,正在修复..." sed -i 's/原贴.*:$/原贴链接: 请添加真实URL/g' "$file" fixed=$((fixed + 1)) fi if [ $fixed -gt 0 ]; then echo "✅ 已修复 $fixed 个问题" return 0 else echo "✅ 未发现问题链接" return 1 fi } # ============================================================================ # 主程序 - 检查最近文章 # ============================================================================ check_recent_articles() { local count=${1:-5} echo "╔════════════════════════════════════════════════════╗" echo "║ SEO 检查 - 最近 $count 篇文章 ║" echo "╚════════════════════════════════════════════════════╝" echo "" # 查找最近修改的markdown文件 local files=$(find "${HQBSH_DIR}" -name "*.md" -mmin -180 2>/dev/null | head "$count") if [ -z "$files" ]; then echo "⚠️ 最近没有找到hqbsh文章" echo "" echo "检查 workspace 下的其他文章:" files=$(find "${WORKSPACE}" -name "*.md" -mmin -180 2>/dev/null | grep -v sleep-learning | head "$count") fi local total_issues=0 for file in $files; do local filename=$(basename "$file") echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "📄 检查: $filename" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" # 检查问题链接 local issues=$(check_broken_links "$file") if [ -n "$issues" ]; then echo "❌ 问题链接:" echo "$issues" | head -5 total_issues=$((total_issues + $(echo "$issues" | wc -l))) else echo "✅ 链接检查:正常" fi # 检查SEO元素 local seo_issues=$(check_seo_elements "$file") if [ -n "$seo_issues" ]; then echo "⚠️ SEO建议:" echo "$seo_issues" | head -3 fi # 生成优化建议 local suggestions=$(generate_seo_suggestions "$file") if [ -n "$suggestions" ]; then echo "" echo "💡 优化建议:" echo "$suggestions" fi echo "" done echo "╔════════════════════════════════════════════════════╗" echo "║ 共发现 $total_issues 个问题 ║" echo "╚════════════════════════════════════════════════════╝" } # ============================================================================ # 主程序 # ============================================================================ case "$1" in check) check_recent_articles "${2:-5}" ;; fix) fix_links "$2" ;; *) echo "用法: $0 {check [数量]|fix <文件>}" echo "" echo "示例:" echo " $0 check 10 # 检查最近10篇文章" echo " $0 fix file.md # 修复指定文件的链接问题" ;; esac