Emacs Functions for Blogging with Hugo

I’ve been thinking a bit about moving my blog yet again. This time to hugo for a variety of reasons:

  1. I don’t use more than 10% of the features offered by WordPress.
  2. I have very mixed feelings about blog-hosted discussions these days.
  3. WordPress seems to be an easy target for hacks these days.

Most of my personal writing gets composed in emacs as markdown first anyway, so the first set of functions are intended to ease conversion by generating yaml front matter (title and date) for a post from the filename and the first heading. My filenames tend to start with a date stamp YYYYMMDD for ease of searching.

;;functions for compiling blog posts with hugo. 
;;convert dates from YYYYMMDD to YYYY-MM-DD
(defun kjs/date-convert (str)
  (let ((year (substring str 0 4))
    (month (substring str 4 6))
    (day (substring str 6 8)))
    (concat year "-" month "-" day)))

;;get the title from the first header.
(defun kjs/get-title ()
  ;;(interactive)
  (save-excursion
    (re-search-forward "^#")
    (substring (thing-at-point 'line) 2)))

;;add yaml front matter. 
(defun kjs/yaml-add ()
  (interactive)
  (goto-char (point-min))
  (let ((date (kjs/date-convert (buffer-name)))
    (title (kjs/get-title)))
    (insert (concat "---
title: \"" title "\"
description: \"" title "\"
date: \"" date "\"
---" )))
  (re-search-forward "^#")
  (move-beginning-of-line nil)
  (kill-line))

Next, a skeleton and abbreviation for creating new posts:

(defvar current-date-time-format "%F"
  "Format of date to insert with `insert-current-date-time' func
See help of `format-time-string' for possible replacements")

(define-skeleton kjs/yaml-skeleton
  ""
  ""
  "---
title: \"\"
description: \"\"
date: \""
(format-time-string current-date-time-format (current-time))
"\"
tags: []
---

")

(use-package markdown-mode 
  :config
  (define-abbrev markdown-mode-abbrev-table 
    "yamhead" "" 'kjs/yaml-skeleton))

And, a function for using ido completion to insert images:

(defun kjs/insert-image ()
  ""
  (interactive)
  (let ((f (ido-read-file-name "Filename: "
               "~/Dropbox/hugo-blog-test/static/images/")))
    (insert (concat
         "![]("
         (replace-regexp-in-string ".*images" "/images" f)
         ")"))))
 
Advertisements