Creating a new blog post in Emacs

Now that my blog is based on markdown text files, some new tooling options have opened!

I just wrote this function to create a new posts file and open a buffer for editing it. In fact, it’s how I’m editing this post right now!

(defun new-blog-post ()
  (interactive)
  (let ((post-title (read-string "Enter new post title: ")))
    (let* ((posts-dir "/ssh:vps:~/projects/blog/_posts/")
           (clean-title (replace-regexp-in-string
                         "[^[:alpha:][:digit:]_-]"
                         ""
                         (s-replace " " "-" (downcase post-title))))
           (new-post-filename (concat
                               (format-time-string "%Y-%m-%d")
                               "-"
                               clean-title
                               ".md"))
           (frontmatter-template "---\nlayout: post\ntitle: {title}\ndate: {date}\n---\n\n")
           (frontmatter (s-replace "{date}"
                                   (format-time-string "%Y-%m-%d %H:%m %z")
                                   (s-replace "{title}"
                                              post-title
                                              frontmatter-template)))
           (new-post-file (expand-file-name new-post-filename posts-dir)))
      (if (file-exists-p new-post-file)
          (message "A post with that name already exists.")
        (write-region frontmatter nil new-post-file)
        (find-file new-post-file)))))

This was also the first elisp function I wrote :)