Written by
Matt Bilyeu
on
on
Creating a new blog post in Emacs
Now that my blog is based on markdown text files, some new tooling options have opened!
- entire blog is under source control (done)
make
targets for common actions (e.g. create new post, deploy, serve dev version) (done)- git hooks for publishing (need to think about this more)
- blogging via emacs! (done!)
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 :)