HUGO

使用 Org-Mode、ox-hugo 编写博客

Hugo site config

config.yaml

baseURL: "your site url"
languageCode: "en-us"
title: "title"
theme: contrast
buildDrafts: false

# Code Fences 渲染相关
markup:
  highlight:
    codeFences: true
    guessSyntax: false
    hl_Lines: ""
    lineNoStart: 1
    lineNos: false
    lineNumbersInTable: true
    noClasses: true
    style: emacs
    tabWidth: 4

ox-hugo

(use-package ox-hugo
  :ensure t            ;Auto-install the package from Melpa (optional)
  :after ox)

(add-to-list 'org-export-backends 'hugo)

将以下代码片段放入你的 blog.org 文件头部, 配置含义

#+HUGO_BASE_DIR: ~/Documents/org/blog
#+SEQ_TODO: TODO DRAFT DONE
#+HUGO_AUTO_SET_LASTMOD: t
#+PROPERTY: header-args :eval no

Org Capture Setup

(defun v-org-hugo-new-subtree-post-capture-template ()
  "Returns `org-capture` template string for the new Hugo post."
  (let* ((title (read-from-minibuffer "Post Title: "))
         (fname (org-hugo-slug title)))
    (mapconcat #'identity `(
                            ,(concat "** TODO " title)
                            ":PROPERTIES:"
                            ,(concat ":EXPORT_FILE_NAME: " fname)
                            ":END:"
                            "%?\n")
               "\n")))

(add-to-list 'org-capture-templates
             '("b"                ;`org-capture' binding + b
               "Hugo post"
               entry
               ;; It is assumed that below file is present in `org-directory'
               ;; and that it has a "Blog Ideas" heading. It can even be a
               ;; symlink pointing to the actual location of all-posts.org!
               (file+headline "/path/to/your/org" "INBOX")
               (function org-hugo-new-subtree-post-capture-template))))

Preview

Publish to github pages

Auto publish

(defun publish-hugo-posts-to-github-pages ()
  "Auto publish to github pages."
  (interactive)
  (org-hugo-export-wim-to-md t nil nil)
  (async-shell-command "sh ~/Documents/org/deploy.sh"))

(defalias 'hp 'publish-hugo-posts-to-github-pages)

deploy.sh

#!/bin/bash
cd blog
hugo -D

cd public
git add .
git commit -m "rebuild site `date`"
git push

以上文件路径按需修改

Resources