masasam / emacs-easy-hugo

Emacs major mode for managing hugo
https://gohugo.io/tools/editors/#emacs
GNU General Public License v3.0
355 stars 26 forks source link

Feature request: title to slug #64

Closed max-arnold closed 2 years ago

max-arnold commented 2 years ago

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

max-arnold commented 2 years ago

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)
             )))))))
masasam commented 2 years ago

Hi @max-arnold . Thank you for comment. I've implemented easy-hugo-slugify at https://github.com/masasam/emacs-easy-hugo/commit/baead14d7f2fa86e108269932a94bf376de9c2e5. I hope you find it useful.