添加slug,改版文章目录命名结构,添加了一个实用的新建文章Shell脚本。
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 进入项目根目录
|
||||
cd ~/Git仓库/Home-of-CS/ || { echo "无法进入项目目录"; exit 1; }
|
||||
|
||||
POST_DIR="content/post"
|
||||
|
||||
# 检查 content/post 是否存在
|
||||
if [ ! -d "$POST_DIR" ]; then
|
||||
echo "错误:目录 $POST_DIR 不存在"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 1. 扫描所有子目录,提取唯一前缀
|
||||
echo "正在扫描已有前缀..."
|
||||
prefixes=$(find "$POST_DIR" -maxdepth 1 -type d -name "*_*" | while read -r dir; do
|
||||
basename=$(basename "$dir")
|
||||
# 提取第一个下划线之前的部分
|
||||
echo "${basename%%_*}"
|
||||
done | sort -u)
|
||||
|
||||
if [ -n "$prefixes" ]; then
|
||||
echo "已存在的前缀:"
|
||||
echo "$prefixes" | tr '\n' ' '
|
||||
echo
|
||||
else
|
||||
echo "未找到任何符合格式(含下划线)的子目录"
|
||||
fi
|
||||
|
||||
# 2. 读取用户输入的前缀并转为大写
|
||||
read -p "请输入要使用的目录前缀: " input_prefix
|
||||
if [ -z "$input_prefix" ]; then
|
||||
echo "前缀不能为空"
|
||||
exit 1
|
||||
fi
|
||||
prefix=$(echo "$input_prefix" | tr '[:lower:]' '[:upper:]')
|
||||
|
||||
# 3. 获取当前日期时间
|
||||
year=$(date +%Y)
|
||||
month=$(date +%m)
|
||||
day=$(date +%d)
|
||||
hour=$(date +%H)
|
||||
|
||||
# 4. 生成基础目录名(如 FRA_2026_05_27_22)
|
||||
base_name="${prefix}_${year}_${month}_${day}_${hour}"
|
||||
target_name="$base_name"
|
||||
counter=2
|
||||
|
||||
# 5. 统计当前目录数量(不包括新目录)
|
||||
# 使用 find 统计子目录数量,排除当前目录(.)
|
||||
current_count=$(find "$POST_DIR" -maxdepth 1 -type d ! -path "$POST_DIR" | wc -l)
|
||||
MAX_DIRS=238328 # 62^3
|
||||
|
||||
if (( current_count + 1 > MAX_DIRS )); then
|
||||
echo "错误:目录数量已达上限 ${MAX_DIRS},无法创建新目录"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 6. 处理重名(避免与已有目录冲突)
|
||||
while [ -d "$POST_DIR/$target_name" ]; do
|
||||
target_name="${base_name}_${counter}"
|
||||
((counter++))
|
||||
done
|
||||
|
||||
echo "将创建目录:$target_name"
|
||||
|
||||
# 7. 执行 hugo new 命令创建文章
|
||||
if ! hugo new "content/post/$target_name/index.md"; then
|
||||
echo "❌ hugo new 命令执行失败,请检查 hugo 是否安装及路径是否正确"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 8. 计算 base62 编码(索引 = 新目录的序号 = current_count + 1)
|
||||
index=$((current_count +1))
|
||||
num=$((index - 1)) # 编码时从 0 开始
|
||||
|
||||
# base62 字符集:0-9(0-9), A-Z(10-35), a-z(36-61)
|
||||
chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
encode=""
|
||||
temp=$num
|
||||
for i in 1 2 3; do
|
||||
remainder=$((temp % 62))
|
||||
encode="${chars:$remainder:1}${encode}"
|
||||
temp=$((temp / 62))
|
||||
done
|
||||
# 若 temp 不为 0 说明 num 超过了 62^3-1,但前面已做上限检查,此处不会发生
|
||||
|
||||
slug_value="$encode"
|
||||
|
||||
# 9. 修改 index.md 中的 slug 字段
|
||||
index_file="$POST_DIR/$target_name/index.md"
|
||||
if [ -f "$index_file" ]; then
|
||||
# 使用 sed 替换 slug 行(兼容单引号或双引号,保留原有引号格式)
|
||||
# 匹配 slug: "..." 或 slug: '...',将引号内容替换为编码
|
||||
sed -i.bak -E "s/^(slug:[[:space:]]*['\"])(.*)(['\"])$/\1${slug_value}\3/" "$index_file"
|
||||
rm -f "${index_file}.bak"
|
||||
echo "已设置 slug: \"$slug_value\""
|
||||
else
|
||||
echo "⚠️ 警告:未找到文件 $index_file,slug 未修改"
|
||||
fi
|
||||
|
||||
echo "成功创建文章:content/post/$target_name/index.md"
|
||||
Reference in New Issue
Block a user