利用hugo搭建个人博客

# 1. 准备:

  1. Mac电脑
  2. Git 工具
  3. GitHub 账号

# 2. 安装及初始化Git仓库

# 2.1 Mac安装hugo

1
brew install hugo

# 2.2 创建一个站点

进入到放置站点的文件夹下,例如~/Blog,然后运行下面的命令

1
hugo new site TestBlog

# 2.3 初始化 git 仓库(可选)

在 TestBlog 目录下运行命令:

1
git init .

在根目录新增.gitignore文件,内容如下

1
2
3
4
public
resources
assets/jsconfig.json
.hugo_build.lock

再提交到本地git仓库中

1
2
git add . # 将文件添加到git缓存
git commit -m "初始化" # 将文件提交到本地git仓库

若需要提交到GitHub、GitLab等,请自行配置。

# 3. 添加 hugo-theme-stack 主题

# 3.1 拉取主题源码

在站点根目录下,即 TestBlog/,通过git拉取主题源代码,放置位置在 TestBlog/themes/目录下:

  • 若执行了2.3初始化Git仓库的操作,就运行下面的命令获取主题
1
git submodule add https://github.com/CaiJimmy/hugo-theme-stack/ themes/hugo-theme-stack
- git submodule add 的意思是作为一个子模块添加到当前Git仓库中。
  • 若未执行 2.3初始化Git仓库的操作,就运行下面的命令获取主题
1
git clone https://github.com/CaiJimmy/hugo-theme-stack/ themes/hugo-theme-stack

# 3.2修改配置文件

在站点根目录下操作

1
2
mv hugo.toml hugo.toml.bak
cp themes/hugo-theme-stack/exampleSite/hugo.yaml .

然后暂时修改languageCodethemeDefaultContentLanguage 这三个配置,内容如下:

 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
baseurl: https://example.com

# languageCode: en-us

languageCode: zh-cn

theme: hugo-theme-stack

paginate: 3

title: 爱生活的Geek

copyright: 洪亮

  

# Theme i18n support

# Available values: ar, bn, ca, de, el, en, es, fr, hu, id, it, ja, ko, nl, pt-br, th, uk, zh-cn, zh-hk, zh-tw

DefaultContentLanguage: zh-cn

  

# Set hasCJKLanguage to true if DefaultContentLanguage is in [zh-cn ja ko]

# This will make .Summary and .WordCount behave correctly for CJK languages.

hasCJKLanguage: false

  

languages:

# en:

# languageName: English

# title: Example Site

# weight: 1

# params:

# description: Example description

zh-cn:

languageName: 中文

title: 爱生活的Geek

weight: 2

params:

description: 计算机技术与摄影

# ar:

# languageName: عربي

# languagedirection: rtl

# title: موقع تجريبي

# weight: 3

# params:

# description: وصف تجريبي

# 3.3 修改内容区

在站点根目录下操作

1
2
3
cp -r ./themes/hugo-theme-stack/exampleSite/content/categories ./content
cp -r ./themes/hugo-theme-stack/exampleSite/content/page ./content
cp -r ./themes/hugo-theme-stack/exampleSite/content/_index.zh-cn.md ./content

# 4. 运行

在站点根目录下执行:

1
hugo server

参考文章

  1. 【Hugo】Stack主题的使用记录
  2. hugo-theme-stack 官方文档
  3. hugo 官方文档
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus