Hexo 迁移到 Hugo
迁移理由:Hugo 更快,Golang 信仰,支持 Google AMP,即时渲染更改,换个博客系统折腾一下。
Hugo 是一个基于 Go 语言开发的静态网站生成器(SSG),使用Go模板,目前由 @bep 领衔开发,Hugo 的突出特点是简洁、灵活、高效,目前有很多知名网站都在使用 Hugo:Netlify、Let’s Encrypt、IPFS、Cloudflare Developers、DigitalOcean Docs、1Password 等等。与目前国内流行的 Hexo 相比,Hugo 的速度可称为飞速🚀——在安装和使用上都是如此。
安装
brew install hugo
hugo new site blog
cd blog
git clone https://github.com/alexandrevicenzi/soho.git themes/soho
主题选用的 https://themes.gohugo.io/soho/
配置
资源文件:
- static/favicon.png
- static/images/profile.png
迁移
hexo 的文章在 source/_posts
中,复制到 hugo 的 content/post
中即可。
测试
hugo server -D
写文章
hugo new post/my-first-post.md
在文章头部设置 draft
为 false 才可见,否则就是草稿,不会发布。
迁移涉及的改造
归档
Hugo 默认是不支持生成归档文件的,要自己实现。
首先,在 themes/soho/layouts
目录创建一个目录 archives
然后在里面新建一个文件 single.html
, 达到覆盖 themes/soho/layouts/_default/baseof.html
模板中的 main
模块:
{{ define "main" }}
<!-- <link rel="stylesheet" href="{{ "css/archives.css" | absURL }}"> -->
<article class="article article-type-post" itemscope="" itemprop="blogPost">
<div class="article-inner">
<div class="post-archive">
{{ range (where (where .Site.Pages "Type" "in" (slice "post" "posts")) "Kind" "page").GroupByDate "2006" }}
<h2> {{ .Key }} </h2>
<ul class="listing">
{{ range .Pages }}
<li>
<date class="meta-date">{{ .Date.Format "2006/01/02" }}</date>
<a class="archive-title" href="{{ .Permalink }}" title="{{ .Title }}">{{ .Title }}</a>
</li>
{{ end }}
</ul>
{{ end }}
</div>
</div>
</article>
{{ end }}
文章路径问题
Update:折腾一下后,最好还是入乡随俗 Nginx 上来个重定向
# 把 /post/*.html => /post/*/ ,301定向
rewrite ^/post/(.+?).html$ /post/$1/ permanent;
有兴趣可以参考:
实现如下路径 https://yryz.net/post/aws-ipv6.html
如果你还是想在 Hugo 中保持文章的 URL 的
.html
后缀,也不是没有办法,利用 Hugo 配置中的uglyURLs
和 Output Formats 的一个配置项noUgly
即可实现。首先在配置文件config.toml
的顶部加上uglyURLs = true
,然后继续添加以下代码:[outputFormats.NoUglyHTML] mediaType = "text/html" noUgly = "true" [outputFormats.NoUglyRSS] mediaType = "application/rss+xml" noUgly = "true" [outputs] taxonomyTerm = ["NoUglyHTML", "NoUglyRSS"] taxonomy = ["NoUglyHTML", "NoUglyRSS"]
这样就能实现仅文章的 URL 是有
.html
后缀的,但需要注意的是:据说有博主说这样是会产生一些 bugs 的,例如自定义的 archives 归档也是 .html 结尾了。