Hugo学习笔记一,初步运行一遍

安装hugo
  1. 官网地址: https://gohugo.io/
  2. github官网地址:https://github.com/gohugoio/hugo,进入后再进入release页,然后下载 linux的安装文件,安装之。
初步使用
  1. 生成hugo 博客目路并自动初始化

    1
    2
    3
    $ mkdir myblog
    $ cd myblog
    $ hugo new site myhugo #在当前目录下创建myweb站点目录,生成的目录结构如下:

    archetypes 目录中定义了Hugo创建markdown文件的模板。
    content 存放markdown文件。
    data 存放数据文件。
    layouts 存放模板文件。
    static 存放静态文件。
    themes 存放主题。
    config.toml 配置文件。

  2. 下载主题,并使用之

    1
    2
    3
    4
    5
    6
    $ cd ~/myblog/myhugo
    $ git clone https://github.com/dillonzq/LoveIt.git themes/LoveIt # 下载LoveIt主题
    $ git clone https://github.com/panr/hugo-theme-terminal.git themes/terminal # 下载terminal主题
    $ git clone https://github.com/wowchemy/starter-hugo-research-group themes/research-group

    $ hugo server -t LoveIt # 运行hugo,并采用LoveIt主题。这只是初步使用,可通过设置,不用在命令行来指定
  3. 采用下载的主题范例
    以 hugo-serif-theme主题为例

    1
    2
    3
    4
    5
    6
    $ cd myblog/themes/hugo-serif-theme/ #进入主题目录
    $ cp -r exampleSite ../../ # 把主题的范例目录copy到myblog目录下
    $ cd ~/myblog/ # 回到myblog目录
    $ mv exampleSite serif # 给范例目录改名
    $ hugo server --source=serif # 指定文件的目录名进行编译
    # 如果出错,修改 serif目录下的config.toml文件,把主题目录改成正确的就行
  4. 修改 ~/myblog/myhugo/config.toml文件,增加
    theme = “XXXX”,可指定默认主题

编写第一个文章
  1. 生成文档
    1
    $ hugo new posts/first_post.md # 在content下生成posts目录,并生成first_post.md文档
  2. 内容
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ---
    title: "Hugo学习笔记一,初步运行一遍"
    date: 2022-11-18T11:40:38+08:00
    tags: ["Hugo"]
    categories: ["stucy"]
    toc:
    enable: true
    description: 学习hugo的笔记1,初接触
    draft: true
    ---
  3. 一些经验
    其实 hugo 在新建文章时,不一定要 hugo new posts/xxxx.md。

可以 hugo new xxxx.md,这样会在 content 目录下新建文件 xxxx.md,网站中可以通过 /xxxx 来访问。

还可以 hugo new /xxxx.md,这样会在 content 目录下的 目录下新建文件 xxxx.md(若文件夹 不存在则会自动新建),网站中可以通过 //xxxx 来访问。此时,在网站中访问 / 即可显示目录下所有文章的列表,实现文章聚合。

对于每一个 的聚合,我们可以在网站目录下的 archetypes 目录中,新建一个 .md 文件,则可创建该类聚合的模板,后续的每一次 hugo new /xxxx.md 都会以 .md 为模板。

在本地启动网站
1
$ hugo server -D --bind "0.0.0.0" # 浏览器中用http://0.0.0.0:1313/查看
部署到GitHub
  1. 在github上建立自己的仓库,比如 laoshiren,那么未来你的主页就会是laoshiren.github.io
  2. 修改 ~/myblog/myhugo/config.toml文件
    修改config.toml文件中的baseURL为https://laoshiren.github.io
  3. 执行
    1
    2
    3
    4
    5
    6
    7
    cd public
    git init
    git remote add origin https://github.com/GitHub账号名/laoshiren.github.io.git
    #此URL可在你的repo中找到
    git add .
    git commit -m "update %date%,%time%"
    git push origin master