June 24th 2026 - Things I Worked On

Added pasting blog post as html link

After I write a blog post I need to link it in my index page. Normally I copy the org file name, run (org-insert-link), manipulate the link to have a ./ prefix, and change the .org suffix to .html.

It was getting annoying to do this every day for every post so I wrote a command that pastes the link with the correct prefix and suffix.

(defun blog--paste-link-in-region (link)
  "Paste the LINK into the selected region."
  (if (use-region-p)
      (let* ((beg (region-beginning))
             (end (region-end))
             (text (buffer-substring-no-properties beg end)))
        (delete-region beg end)
        (insert (format "[[%s][%s]]" link text)))))

(defun blog--paste-link-in-word (link)
  "Paste the LINK in the word at point"
  (let ((word (thing-at-point 'word t))
        (word-bounds (bounds-of-thing-at-point 'word)))
    (when (and word-bounds link)
      (delete-region (car word-bounds) (cdr word-bounds))
      (insert (format "[[%s][%s]]" link word)))))

(defun blog--is-org-file (filename)
  "Return t if FILENAME is an org file, otherwise return nil."
  (s-ends-with-p ".org" filename))

(defun blog--convert-org-filename-to-html (filename)
  "Convert the given FILENAME to a valid html file name."
  (let ((html-link (s-replace ".org" ".html" filename)))
    (format "./%s" html-link)))

(defun blog-paste-article-link ()
  "Paste an article link into the word or region at point."
  (interactive)
  (let ((raw-link (current-kill 0)))
    (if (not (blog--is-org-file raw-link))
        (user-error "String in kill ring is not an org file: %s" raw-link)
      (let ((formatted-link (blog--convert-org-filename-to-html raw-link)))
        (when formatted-link
          (if (use-region-p)
              (blog--paste-link-in-region formatted-link)
            (blog--paste-link-in-word formatted-link))
          (message "Pasted %s" formatted-link))))))