使用 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
-
使用 ox-hugo 导出
- Subtree:
C-c C-e H H
- All subtrees
C-c C-e H A
- Subtree:
-
启动 hugo 服务
hugo server -D
-
浏览器预览
open http://localhost:1313/
-
自动导出
M-x org-hugo-auto-export-mode
Publish to github pages
-
生成静态文件
hugo -D
-
初始化 git 仓库
cd public git init git remote add origin git@github.com:vritser/vritser.github.io.git
-
提交&推送
git add . git commit -m "init" git push origin master
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
- Org-mode: https://orgmode.org/
- Hugo: https://gohugo.io/getting-started/quick-start/
- Hugo themes: https://themes.gohugo.io/
- Hugo markup: https://gohugo.io/getting-started/configuration-markup
- Highlight: https://xyproto.github.io/splash/docs/all.html
- ox-hugo: https://ox-hugo.scripter.co/
- My Emacs config: https://github.com/vritser/.emacs.d