Hugo 是一款快速搭建网站的主题框架,相对于 Hexo 来说, Hugo 的主题以及性能更加出色,并且类型上更加多元化,下面我们试着用 Hugo 来搭建网站。详情可以去 Hugo官网 查看.
Hugo 是一个用 Go 语言编写的静态网站生成器。
在 Windows 上安装 Hugo
在 Windows 上需要先安装包管理工具,这样再安装 Hugo 就会节省很多时间,这里有两种方式来让你安装包管理工具。
第一种方法:Chocolatey 包管理安装
- 首先利用 PowerShell 打开,运行
Set-ExecutionPolicy Bypass -Scope Process -Force
如果有权限提示,输入 Y,
- 接着输入这一串
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
复制代码
- 成功后输入
choco install hugo -confirm
安装 Hugo,即可开始安装。另外需要主要的是,如果等待很久还没有出现安装成功的提示,那这个就要考虑是否已经翻墙,翻墙的更容易安装上?
第二种方法:Scoop 包管理安装
- 打开 PowerShell ,输入
iex(new-object net.webclient).downloadstring(' https://get.scoop.sh ')
- 安装成功后输入
scoop install hugo
接下来跟着下面步骤即可
在 Mac上安装
在 Mac 上安装也需要先安装包管理工具,我们假设已经安装了 brew ,接着开始安装 Hugo
第1步:安装 Hugo
brew install hugo
安装完成后,我们看一下当前版本,如果出来版本,说明我们已经安装成功了:
hugo version
第2步:创建一个新网站
选择一个在你要存放网站的地方,创建文件夹名为 xxx 的网站,然后在当前文件夹内执行:
hugo new site xxx
创建的站点文件目录说明:
|- archetypes :存放default.md,头文件格式
|- content :content目录存放博客文章(.markdown/.md文件)
|- data :存放自定义模版,导入的toml文件(或json,yaml)
|- layouts :layouts目录存放的是网站的模板文件
|- static :static目录存放js/css/img等静态资源
|- config.toml :config.toml是网站的配置文件
这个时候,创建出来的网站是没有任何内容的,需要下载个主题跑起来才有内容。
第3步:添加主题
这里有各种各样的主题包:themes.gohugo.io,选上一种进行下一步
cd xxx
git init
git submodule add https://github.com/budparr/gohugo-theme-ananke.git themes/ananke
注意:有些主题步骤会跟上面不一致,可以直接到该主题下按照步骤操作即可
第4步:添加一些内容
下载好之后,现在还没有文章,我们来写一篇文章:
hugo new posts/my-first-post.md
这样就会在posts目录下生成一个markdown的文件,用markdown工具打开编辑内容即可。
现在启动Hugo本地服务器 在xxx目录下: hugo server -t 主题名字
出现
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1) Press Ctrl+C to stop
说明已经成功。打开浏览器输入http://localhost:1313
看一看预览效果。
第五步 上传到github
这个时候本地网址已经搭建好了,可以上传到github上。
-
在xxx目录下,如果已经有public文件夹,则删除:`rm -rf public完全删除该public目录
-
clone 仓库:
git submodule add -b master git@github.com:<USERNAME>/<USERNAME>.github.io.git public
出现 Not a git repository 提示的话,是因为没有.git这样一个目录,创建一个即可:git init
-
在本地测试好之后:
hugo --theme=xxx --baseUrl="https://xxx.github.io/"
-
更新git
-
cd 到 public 目录,
-
git add *
全部更新 -
git commit -m
"更新内容" -
git push https://github.com/xxx/xxx.github.io.git master
-
注意的地方
- 如果出现:
To https://github.com/aniruddhabarapatre/learn-rails.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/aniruddhabarapatre/learn-rails.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
复制代码
用:git push -f https://github.com/xxx/xxx.github.io.git master
强制更新