配置 #
我是使用 Doom Emacs 的配置,所以在添加 ox-hugo 支持方面比较简单。
(doom!
;; 其他配置
:lang
(org +pretty
+hugo)
)
在你的博客目录下,开一个用来放 org 的目录,比如根目录下:
~/W/Blog/mox-blog [main] λ tree -L 1
.
├── archetypes
├── assets
├── config
├── content
├── data
├── hugo.toml
├── i18n
├── layouts
├── org
├── public
├── resources
├── static
└── themes
13 directories, 1 file
在 org 目录下就可以用 org-mode 写博客了。
确定好目录位置后,需要在博客的头部元数据里指定 hugo 工程的目录,也就是当前博客的根目录:
#+title: 如何在 Hugo 里写 Org
#+author: Mox W
#+date: <2025-10-11 六>
#+hugo_base_dir: ../
#+filetags: Emacs Org Hugo @技术教程
顺便说明一下 Tag 和 Category 怎么配置:
#+filetags: tag1 tag2 @cat1 @cat2
可以看到,标签或分类之间是用空格分割的。如果希望标签或分类本身就包含空格呢?
ox-hugo 使用如下规则:
abc_def -> abc-def
abc__def -> abc def
abc___def -> abc_def
生成 Markdown #
直接使用 org 写就的文章,需要转换成 Markdown 才能被 Hugo 识别。
在 Emacs 里,我们直接祭出 M-x 大法: M-x org-export-dispatch
然后在弹出的菜单里敲击 H h 即可。
这里注意一定是生成 Hugo Capatible Markdown ,选择 File to Md file 。
其他选项里可能有 subtree to md file ,这相当于只将某一个标题(子树)导出。
只要配置好 hugo_base_dir ,导出的 md 文件应该就会正常地落到 content/posts/ 里。
试一试吧。