基于 Emacs org-mode 和 hugo 的 Blog 工作流
Introduction 我经常使用 Emacs Org-mode 来写blog。虽然有很多其他笔记工具和方案,但我一直觉得没法取代 Org-mode,比如Org-mode里的 Babel 代码块,内嵌LaTex公式,GUI下图表预览,表格自动求值等功能,实在是很方便。Emacs Org-mode 也天然支持将众多 org 文件笔记通过链接组织成个人知识库。 不过,现有的工作流基于标准的 ox-publish 导出 HTML,样式略显陈旧,如果支持更美观的主题,需要很多手动配置。 最近对orgmode工作进行了一次升级:引入 Hugo 渲染引擎(搭配 PaperMod 主题),同时保留了纯粹的 Org-mode 创作体验。 工作流的演进 过去:Org -> HTML (ox-publish) 优点:流程简单,完全受 Emacs 控制。 缺点:网页样式极其基本,移动端适配差,缺乏搜索和标签功能。 现在:Org -> HTML Fragment -> Hugo (PaperMod) 改进:采用 Hugo 作为渲染引擎,利用 PaperMod 提供现代化的 UI。 核心逻辑:利用 Emacs 的 ox-html 将 Org 导出为纯净的 HTML 片段,再交给 Hugo 进行最终装配。 关键技术点 1. 自动化导出脚本:publish-hugo.el 这是整个流程的“心脏”。它是一个 Emacs Lisp 脚本,通过 Org 内置的 ox-html 导出器实现精准渲染,同时负责元数据注入和路径修正。 (defun pz/export-all-to-hugo () (dolist (section '("notes" "quantum" "me")) (dolist (file (directory-files (expand-file-name section) t "\\.org$")) (with-current-buffer (find-file-noselect file) ;; 导出为 HTML 片段 (let ((html-content (org-export-as 'html nil nil t))) ;; 修复资源路径 (setq html-content (replace-regexp-in-string "\\.\\./images/" "/images/" html-content)) ;; 写入 Hugo content 目录 (with-temp-file dest-file (insert "---\n" (format "title: \"%s\"\n" title) "math: true\n---\n\n") (insert html-content))))))) 2. 构建与部署:build.sh & deploy.sh 实现“一键发布”的两个 Bash 脚本: ...