用 Hugo + Cloudflare Pages 搭了个博客
一直想搭个博客,但一直在"想"的阶段。直到最近觉得再不动手就永远不会动手了,花了一个下午把整个站点从零搭到上线。这篇文章把过程记录下来,不是教程——网上 Hugo 教程已经够多了——更像是一份带注释的踩坑日志。 为什么是 Hugo 选静态站点生成器这件事,我其实没纠结太久。之前用过 Hexo,知道 Node.js 生态的依赖地狱是什么体验。Jekyll 太慢,Gatsby 太重。Hugo 是 Go 写的单二进制文件,brew install hugo 之后就能用,没有 node_modules,没有依赖冲突,构建速度快到几乎感觉不到在构建。 brew install hugo hugo version # hugo v0.160.0+extended darwin/arm64 跑完这两行就算装好了。extended 版本自带 SCSS 编译,后面主题需要。 主题选了 PaperMod。原因很简单:干净、快、功能够用。暗色模式、搜索、目录、代码高亮、多语言——开箱即用,不需要自己从零写前端。用 git submodule 引入,这样主题更新的时候只需要拉一下 submodule 就行: hugo new site blog && cd blog git init git submodule add --depth=1 https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod 配置:一个 YAML 文件搞定大部分事情 Hugo 的配置集中在 hugo.yaml 一个文件里(也支持 toml 和 json,我习惯 yaml)。我的博客是中英双语的,中文是默认语言放在根路径,英文加 /en/ 前缀。配置大概长这样: defaultContentLanguage: zh defaultContentLanguageInSubdir: false # 中文不加前缀 languages: zh: languageName: "中文" weight: 1 title: "Forest's Blog" params: author: "北海" en: languageName: "EN" weight: 2 title: "Forest's Blog" params: author: "Chunhao Zhang" 双语文章的文件命名用后缀区分:中文 build-blog-with-hugo.md,英文 build-blog-with-hugo.en.md。Hugo 会自动把它们关联起来,语言切换的时候跳到对应版本。 ...