Posted on Jan 1, 1

tags:: tool

想搭建一个blog系统,写一些总结自娱自乐,然后发现了Hugo。

最后方案如下:

  1. 部署在云端,nginx做web服务器
  2. 使用hugo
    1. hugo的theme为eureka
    2. 留言评论:集成disqus js插件
  3. 文档格式:主要是markdown格式,用typora编辑
  4. 版本管理:文档以git方式存储在github

Hugo是什么?

不同于动态网站,是静态renderer的。

Hugo is a static HTML and CSS website generator written in Go. It is optimized for speed, ease of use, and configurability. Hugo takes a directory with content and templates and renders them into a full HTML website.

Hugo relies on Markdown files with front matter for metadata, and you can run Hugo from any directory.

安装比较简单,注意linux上apt源可能不够新,最好从github上下载最新的版本。

暂时使用nginx的反向代理实现,static文件目录直接让nginx访问,效率更高。

hugo #This generates your website to the `public/` directory by default

遇到的问题

baseurl中去掉port

问题:输出html中url为blog.time9.xyz:8889,默认带有端口,使得在nginx反向代理时有问题。

解决方案:命令行中设置appendPort=false

nohup hugo server -D --bind 127.0.0.1 --port 8889 -d public -b blog.time9.xyz --appendPort=false &

markdown中图片的相对位置的问题

参考配置Hugo的图片路径

本地图片是相对网络图片而言,如果你有图床也就无所谓是否相对路径了

  1. content目录下 例如图片content/a.png,在文章content/post/a.md中引用就需要是![](assets/a.png)
  2. static目录下 例如图片static/images/a.png,在文章content/post/a.md中引用就需要是![](assets/a.png)

使用typora的约定,xxx.md的文档所对应的本地资源保存在xxx.assets/目录下。使用如下命令将资源复制到static目录下,为static/posts/xxx/xxx.assets,注意符号链接不支持。

rm -rf static/posts/
mkdir -p static/posts
#(cd content; find posts -name '*.assets' -exec cp -R "$(pwd)/{}" ../static/"{}" \;)
#上述这个方式链接要修改 (xxx.assets/a.jpeg)为(../xxx.assets/a.jpeg)

find content/posts -name '*.assets' | while read asset_dir; do
    local parent_dir="${asset_dir%.assets}"
    parent_dir="${parent_dir/content/static}"
    mkdir -p "$parent_dir"
    cp -R "$asset_dir" "${parent_dir}/$(basename "$asset_dir")"
done

重新启动hugo

kill -9 $(pgrep hugo); nohup hugo server -D --bind 0.0.0.0 --port 8889 -b https://blog.time9.xyz/ --appendPort=false &

需要注意:

  1. appendPort=false中需要=
  2. appendPort设置到config中似乎没有用。
  3. {{ .Site.BaseURL }} 读取baseurl,并不是config中直接设置的,而是通过-b选项从命令行读取。

参考

  1. 官方

  2. appendPort=false works for .Site.BaseURL but not .Permalink

  3. Excluding port from sitemap baseurl

  4. https://www.wangchucheng.com/zh/docs/hugo-eureka/getting-started/

  5. RTFM