基于Hugo框架建立个人网站

由简入难,由难归简

Hugo

  • 一个静态站点生成器;
  • 允许使用简单的标记语言创建发布Web内容;
  • 应用主题生成不同网站风格;
  • 利用git传递托管在Web服务器或主机上。
  • 安装Hugo,参考官方网站goHugo

如此看来,正是孤一直想找的。

主要命令

Hugo常用命令较少,复杂性虽不高,但还需摸索一番。

  • 可以用Hugo --help获取

创建新网站

hugo new site /path/<New site name>

创建新网页

有默认和自定义模式2种。

默认模式

当不指定网页创建模版,Hugo默认调用archetype文件夹种预设模版default.md

  • 新网页必须放置在content文件夹中,并可根据需要建立子目录夹。
cd /path/<New site name> # 进入网站文件夹
hugo new content/path/<newpagename>.md

编辑新网页

---
title: "My First Post"  # 网页主题名称
date: 2019-03-26T08:47:11+01:00  # 编辑时间,一般自动生成
draft: true  # 设置为草稿状况
tags: ['标签','标签']
categories: ['类别1','类别2']
description: "网页内容简介"
---
此部分为内容摘要,显示在主页中
<!--more-->  
以下为正文内容

自定模式

archetype文件夹中存在自定义模版<模版名称.md>时,可以利用--kind参数指定模版创建新的网页。

hugo new --kind 模版名称 content/path/<newpagename>.md

使用网站主题theme建站

Hugo建站需要使用指定主题theme, 可从Hugo Theme中选择下载。

beautiful Hugo主题,主要设置过程如下:

cd themes/ # 进入主题目录
git submodule add https://github.com/halogenica/beautifulhugo.git beautifulhugo # git submodule 在主项目中添加分支项目
git submodule update --init --recursive # 下载更新分支模块

修改hugo.toml配置文件

参考主题的beautiful Hugo 设置说明, 修改网站目录中的hugo.toml文件,或者是以yamljson结尾的配置文件。

主要设置如下:

# 全局参数
baseurl = "https://******" # 网站地址
DefaultContentLanguage = "zh-CN" # 设置默认中文
hasCJKLanguage = true   # 网站内容包含中文CJK语言
title = "****"      # 网站名称
theme = "beautifulhugo"  # 设置主题模版

# Beautiful Hugo参数,关于代码高亮
pygmentsStyle = "trac"
pygmentsUseClasses = true
pygmentsCodeFences = true
enableRobotsTXT = true  # 准许搜索引擎爬虫
pygmentsCodefencesGuessSyntax = true
#pygmentsUseClassic = true  # 第二种高亮方式,设置参考主题网页
pygmentOptions = "linenos=inline"


[languages]
  [languages.zh-CN] 
    contentDir = "content/zh" # 中文模块

[Params]
  homeTitle = "" # Set a different text for the header on the home page
  subtitle = "欲论人者,必先自论;欲知人者,必先自知;欲胜人者,必先自胜。" # 设置简介或者自论
  mainSections = ["post","book"]  
  logo = "img/logo.png" # Expecting square dimensions
  favicon = "img/faviconlogo.ico"
  dateFormat = "January 2, 2006" # 网页日期格式
  commit = false   # 用于git commit提交修改日期记录
  rss = true        
  comments = true
  readingTime = true
  wordCount = true
  useHLJS = true
  socialShare = false
  showRelatedPosts = true

# 个人信息页面设置
[Author]
  name = "xx" # 作者名称,全局设置参数,也会显示在网页footer中copyright部分
  website = "xxxx" # 网页footer中copyright的主页链接
  email = "xxxx@xxxx"

# 链接栏设置
[[menu.main]]
    name = "home"
    url = ""
    weight = 1  # 子项目的权重,用于子项目之间排序,例如 home, about, categories

[[menu.main]]
    name = "About"
    url = "page/about"
    weight = 3

[[menu.main]]
    name = "Tags"
    url = "tags"
    weight = 3

[[menu.main]]
    name = "Catetories"
    url = 'categories'
    weight = 3

编译网站

利用以下代码编译网站,如果config.toml文件中没有设置theme 时,需要指定编译的依赖模版-t 模版名称

hugo -t beautifulhugo -D

启动服务器

启动服务器,如果带上参数-D,会同时编译包括Draft: true状况在内的网页,

  • 在服务器启动状态下,对网页或者设置的修改会实时更新。
hugo server -D

也可以指定服务器监听地址和端口

  • baseURL 监听网址,例如https://sigma2.cn
  • bind 登入权限地址,0.0.0.0表示任何地址
  • port 监听端口
hugo server -D --baseURL=localhost --bind=0.0.0.0 --port=端口

生成发布版本

hugo版本,需要利用以下命令生成完整的发布版本

hugo

发布到服务器

首先需要在服务器上创建一个git空仓, 在利用git hook转发, 可参考[此文](todo).

git add public
git commit -m '信息'
git push origin master # origin是远程仓的别名,master是项目分支别名,默认为主分支。

总结

Hugo 能简单化静态网站建立过程,说明文档较为专业,初入坑有不少弯路要走,但孤喜欢挑战。

Todo

  • 代码高亮如何设置?
  • 如何实现站内搜索?
  • archives如何实现?
  • 文档中主题层级的对应字体大小如何调整?

也可以看看