以前一直使用的是hexo,但是随着开始使用emacs,就想使用org mode来管理博客,正好emacs有个ox-hugo的插件可以比较方便的和hugo结合

相关工具介绍

hugo: Hugo是由Go语言实现的静态网站生成器

ox-hugo: emacs插件,用于将org导出为hugo可以识别的md文件

https://github.com/kaushalmodi/ox-hugo

easy-hugo: 用于管理 Hugo 的 Emacs major mode

https://github.com/masasam/emacs-easy-hugo

hugo相关配置

  • 安装hugo hugo的安装比较简单,可以参考官网的示例,直接下载二进制既可 Hugo中文文档

  • 创建博客目录

    1
    2
    
        # 博客项目的名字为myblog
        hugo new site myblog
    

    目录结构如下

    1
    2
    3
    4
    5
    6
    7
    8
    
        ├── archetypes
        │   └── default.md
        ├── config.toml         # 博客站点的配置文件
        ├── content             # 博客文章所在目录
        ├── data
        ├── layouts             # 网站布局
        ├── static              # 一些静态内容
        └── themes              # 博客主题
    

emacs相关配置

  • 安装相关插件后配置变量

    1
    2
    3
    4
    
        (setq org-hugo-base-dir "~/blog")
        (setq easy-hugo-basedir "~/blog/")
        (setq easy-hugo-postdir "content/post")
        (setq org-hugo-section "post")
    
  • 尝试在emacs里导出org

    使用 org-export-hugo-to-md 命令,可以看到post目录下面新生成了一个md文件

github相关配置

在github下面创建一个名称为 <帐号名称>.github.io 的仓库

在本地blog文件夹下面的public目录设置关联

1
2
3
4
  git init .
  git remote add origin <git地址>
  git add .
  git push

使用浏览器打开 <帐号名称>.github.io 是否成功

  • 在blog目录下面创建deploy.sh脚本

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
        #!/bin/sh
    
        # If a command fails then the deploy stops
        set -e
    
        printf "\033[0;32mDeploying updates to GitHub...\033[0m\n"
    
        # Build the project.
        hugo -D
    
        # Go To Public folder
        cd public
    
        # Add changes to git.
        git add .
    
        # Commit changes.
        msg="rebuilding site $(date)"
        if [ -n "$*" ]; then
                msg="$*"
        fi
        git commit -m "$msg"
    
        # Push source and build repos.
        git push
    
    
  • 使用easy-hugo发布

    命令 easy-hugo-github-deploy

参考资料

博客写作流程之工具篇: emacs, orgmode, hugo & ox-hugo - 贤民的比特记忆