My Capture templates for ox-hugo
Apr 8, 2018

Creating a new ox-hugo blog post manually can be a bit of a pain. There are several properties you have to set for the org section headline of which the most important is :EXPORT_FILE_NAME:. Of course, setting the export file name automatically can be automated, and this is described in this blog post.

The instructions here are taken from / inspired by the ox-hugo documentation.

First, in the use-package declaration in the init.el file, add the following defun:

(use-package ox-hugo
:after ox
:ensure t
:config
(progn
(defun org-hugo-new-subtree-post-capture-template ()
"Returns org-capture' template string for new Hugo post.
(let* ((title (read-from-minibuffer "Post Title: ")) ;Prompt to enter the post title
(fname (org-hugo-slug title)))
(mapconcat #'identity
(
,(concat "* TODO " title)
":PROPERTIES:"
,(concat ":EXPORT_FILE_NAME: " fname)
":END:"
"%?\n")          ;Place the cursor here finally
"\n"))))
)


This will load ox-hugo after the org export back-end (ox) has been loaded, and will define the org-hugo-new-subtree-post-capture-template function. This function creates a capture template based on a title. Note that it automatically creates the blog post filename, which is exactly what we wanted.

Second, add the following to the use-package declaration of org-mode:

(use-package org
;; ...
:custom
;; ...
(org-capture-templates
'(("t" "To Do / Next Actions")
;; ...
("h" "Hugo post"
entry (file+olp "~/projects/blog/content-org/blog.org" "Drafts")
(function org-hugo-new-subtree-post-capture-template))
))
;; ...
)


The declaration shows how org-capture-templates is customized and a h hotkey is added to create a new Hugo post. The entry is added to the Drafts heading in ~/projects/blog/content-org/blog.org, which is where I store my blog. When I invoke the capture template using C-c c h, I am first prompted for a blog title and can then write my post:

** TODO Test post
:PROPERTIES:
:EXPORT_FILE_NAME: test-post
:END:
This is a test post.
`