26年博客状况更新

从上一篇介绍我怎么搭博客的文章到现在也有些时间了
期间,虽然样式都没怎么变,但还是优化了许多配置体验

主题更新

现在,博客使用的是v4版本的Stack主题
具体优化了什么确实看不太出来,但在配置方面做了很多优化

首先是可以通过hugo module配置
这个功能除了要安装hugo-extend还需要安装go

配置详见Stack|开始使用
这样配置之后更新主题只需要通过命令就可以了

hugo mod get -u github.com/CaiJimmy/hugo-theme-stack/v4
hugo mod tidy

主题的配置文件从yaml变成了toml,大部分的键是没变的
虽然也可以使用yaml,但为保险起见,还是使用toml
下面给出我的配置以供参考

title = "MLAcookie"
defaultContentLanguage = "zh"

[[module.imports]]
    path = "github.com/CaiJimmy/hugo-theme-stack/v4"

[Params]
    favicon = "favicon.webp"
    rssFullContent = true
    comments.enabled = false

    [Params.sidebar]
        compact = false
        emoji = "🤯"
        subtitle = "随便记点东西的记事本"
        avatar = "img/avatar.png"

    [Params.widgets]
        homepage = [
            { type = "search" },
            { type = "archives", params = { limit = 5 } },
            { type = "categories", params = { limit = 10 } },
            { type = "tag-cloud", params = { limit = 10 } },
        ]
        page = [{ type = "toc" }]

    [Params.article]
        headingAnchor = false
        math = true
        toc = true
        readingTime = false

        [Params.article.license]
            enabled = true
            default = "CC BY-NC-SA 4.0"

    [Params.footer]
        since = 2023

    [Params.colorScheme]
        toggle = false
        default = "dark"

[[menu.social]]
    identifier = "github"
    name = "MLAcookie@Github"
    url = "https://github.com/MLAcookie"
    params.icon = "brand-github"
[[menu.social]]
    identifier = "bilibili"
    name = "MLA_cookie@Bilibili"
    url = "https://space.bilibili.com/13629146"
    params.icon = "brand-bilibili"

脚本优化

托LLM的福,优化了大部分部署的脚本

首先是全局配置脚本config.ps1
添加了Github Page自定义Url需要的CNAME值

# 获取脚本文件夹路径
$global:ShellScriptDir = Get-Location

# 获取主目录
Set-Location ..
$global:SiteDir = Get-Location
Set-Location $global:ShellScriptDir
# 构建目录
$global:PublicDir = Join-Path -Path $global:SiteDir -ChildPath "public"
# 内容目录
$global:ContentDir = Join-Path -Path $global:SiteDir -ChildPath "content"

# 基础URL
$global:BaseURL = "https://XXX"
# 远程Git仓库链接
$global:RemoteGitURL = "https://github.com/XXX/XXX"
# 目标分支
$global:TargetBranch = "XXX"

# Github Page CNAME
$global:CNAME = "XXX"

然后是新建文章NewPost.ps1
现在会自动根据目录名,添加categories的值

param(
    [string]
    [ValidateNotNullOrEmpty()]
    $PostName,
    [string]
    $CategoryName
)

function ValidatedName {
    param (
        [string]
        [ValidateNotNullOrEmpty()]
        $Name
    )
    $SpecialCharacters = ' !"$%&''()*,-/:;<=>?@[\]^_`{|}~'.ToCharArray()
    $Name = $Name.Replace("`#", "Sharp")
    # 先每个替换成-
    foreach ($Character in $SpecialCharacters) {
        $Name = $Name.Replace($Character, "-")
    }
    # 返回时使用正则表达式匹配连续的-,并替换为单个-
    return $Name -Replace '([-])+', '-'
}

# 导入配置变量
. .\configs.ps1
# 保存当前的工作路径
$localDir = Get-Location
# 切换工作路径
Set-Location $global:SiteDir
# 合法化路径名,不然图片会出问题
$validPath = ValidatedName -Name $PostName

# 普通文章创建
if ($CategoryName -eq "") {
    # 新建Hugo文章
    $newPostPath = Join-Path -Path "content\post\articles" -ChildPath (Get-Date -Format "yyyy")
    $newPostPath = Join-Path -Path (Join-Path -Path $newPostPath -ChildPath $validPath) -ChildPath "index.md"
    hugo new --kind NormalArticle $newPostPath
}
# 需要分类的文章
else {
    # 新建Hugo文章
    $categoryPath = Join-Path -Path "content\post\categories" -ChildPath $CategoryName
    $newPostPath = Join-Path -Path (Join-Path -Path $categoryPath -ChildPath $validPath) -ChildPath "index.md"
    hugo new --kind CategorizedArticle $newPostPath
    $fullPath = Join-Path -Path $global:SiteDir -ChildPath $newPostPath
    (Get-Content -Path $fullPath -Raw) -replace 'categories: ""', "categories: ""$CategoryName""" | Set-Content -Path $fullPath -NoNewline
}
# 返回之前的工作路径
Set-Location $localDir

最后是部署脚本Deploy.ps1
添加了自动复制WebMaster的认证文件,自动添加CNAME标识文件

param(
    [switch]
    $Force
)

function Invoke-GitInit {
    param (
        [switch]
        $Force
    )
    Push-Location $global:PublicDir
    git init
    git remote add origin $global:RemoteGitURL
    git branch -u $global:TargetBranch
    Invoke-GitPush -Force:$Force
    
    Pop-Location
}

function Invoke-GitPush {
    param(
        [switch]$Force
    )
    Push-Location $global:PublicDir
    git pull
    git add .
    git commit -m ("Deploy: " + (Get-Date -Format "yyMMdd-HH:mm"))
    if ($Force) {
        git push origin HEAD:$global:TargetBranch --force
    }
    else {
        git push origin HEAD:$global:TargetBranch
    }
    Pop-Location
}

function Export-WebInfo {
    Push-Location $global:PublicDir
    if ($global:CNAME -and -not (Test-Path "CNAME")) {
        Set-Content -Path "CNAME" -Value $global:CNAME
    }
    $authFile = Join-Path -Path $global:ShellScriptDir -ChildPath "BingSiteAuth.xml"
    if (Test-Path $authFile) {
        Copy-Item -Path $authFile -Destination "BingSiteAuth.xml"
    }
    Pop-Location
}

# 导入配置变量
. .\configs.ps1
# 保存当前的工作路径
$localDir = Get-Location
# 切换工作路径
Set-Location $global:SiteDir
# 清除Hugo构建缓存
if ($Force) {
    # 递归删除所有文件
    Remove-Item -Path (Join-Path -Path $global:PublicDir -ChildPath "*") -Recurse
}
# 构建网页
hugo --gc --minify --baseURL $global:BaseURL
Export-WebInfo
# 准备推送
# 没有初始化git的化就先初始化并强制推送
if (-not (Test-Path (Join-Path -Path $global:PublicDir -ChildPath ".git"))) {
    Invoke-GitInit -Force
}
else {
    Invoke-GitPush -Force:$Force
}

# 返回之前的工作路径
Set-Location $localDir
CC BY-NC-SA 4.0
使用 Hugo 构建
主题 StackJimmy 设计