hugo 文章编辑

新建文章命令

1
hugo new "post/hugo 文章编辑/index.md"

[!NOTE]
新建名为《hugo 文章编辑》的文章
文章路径:.\content\post\hugo 文章编辑\index.md

新建文章.bat

执行此批处理后,终端输入文章标题,自动创建文章并用记事本打开可以编辑文章内容。
我的项目路径:电脑桌面GitHub\hugo-main(自行修改)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@echo off
:: 设置工作目录(确保路径正确)
set "hugoDir=%USERPROFILE%\Desktop\GitHub\hugo"

:: 检查目录是否存在
if not exist "%hugoDir%" (
echo 目标目录 %hugoDir% 不存在,请检查路径是否正确。
pause
exit
)

:: 提示用户输入文件名
set /p fileName=请输入文章名:

:: 检查是否输入了文件名
if "%fileName%"=="" (
echo 文件名不能为空,请重新运行脚本并输入有效的文件名。
pause
exit
)

:: 切换到 Hugo 项目目录
cd /d "%hugoDir%"

:: 使用 hugo new 命令创建新文件
echo 正在创建新文件...
hugo new "post/%fileName%/index.md"

:: 检查文件是否创建成功
set "contentPath=%hugoDir%\content\post\%fileName%\index.md"
if not exist "%contentPath%" (
echo 文件 %fileName% 的创建失败,请检查 Hugo 是否正确安装并配置。
pause
exit
)

:: 提示用户文件已创建
echo 文件 %fileName% 已成功创建在 %contentPath%!

:: 打开记事本并加载文件后退出CMD
echo 正在打开记事本...
start notepad.exe "%contentPath%"
exit

文章模板

  • 文章模板路径:".\archetypes\default.md"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
---
title: "{{ replace .Name "-" " " | title }}"
slug: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
description: "" # 文章描述,用于搜索优化
summary: "" # 文章简要描述,展示在主页
math: true # 是否渲染数学公式:true 渲染,false 不渲染
hidden: false # 文章可见性:false 可见,true 隐藏
comments: true # 评论系统:false 关闭,true 显示
draft: false # 文章状态:true 草稿,false 发布
showToc: true # 是否显示目录:true 显示,false 不显示
TocOpen: true # 目录默认展开:true 展开,false 折叠
autonumbering: true # 目录自动编号:true 自动,false 不自动
hidemeta: false # 是否隐藏元信息:true 隐藏,false 显示
disableShare: true # 底部分享栏:true 不显示,false 显示
searchHidden: false # 搜索引擎收录:true 不收录,false 可收录
showbreadcrumbs: true # 当前路径显示:true 显示,false 不显示
# 文章顶置,输入1可以顶置文章
weight:
# 文章封面,例如:hugo.jpg
image:
# 文章分类
categories:
- 默认分类
# 文章标签
tags:
- Blog
---