masasam/emacs-easy-hugo

Feature request: title to slug

max-arnold opened this issue · 2 comments

Sometimes it is necessary to convert a title to slug - it could be a blog post title, or a section title. It would be nice to have a helper function in easy-hugo that converts selected text to a slug in the same way as hugo (goldmark?) does: https://discourse.gohugo.io/t/difference-in-auto-generated-heading-anchor-names-between-previous-versions-and-v0-60-0-or-higher/22076

I wrote an initial version, feel free to improve it:

(defun hugo-slugify (start end)
  (interactive "r")
  (if (use-region-p)
      (let ((regionp (buffer-substring start end)))
        (save-excursion
          (delete-region start end)
          (insert
           (replace-regexp-in-string
            "[^a-z0-9-]" ""
            (replace-regexp-in-string
             "\s+" "-"
             (downcase regionp)
             )))))))

Hi @max-arnold .
Thank you for comment.
I've implemented easy-hugo-slugify at baead14.
I hope you find it useful.