From 291e0f055b42d9803f8a2095896125b495d1c549 Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Sat, 21 Sep 2024 16:07:15 +0200 Subject: [PATCH] feat: add new command conlang-store-heading-vuepress --- conlanging.el | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/conlanging.el b/conlanging.el index a3fc06e..3e90b98 100644 --- a/conlanging.el +++ b/conlanging.el @@ -48,14 +48,9 @@ ; Vuepress headings ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defun conlanging--get-heading-slug () - "Select and return a heading and its slug." - (let* ((settings (cdr (assq major-mode counsel-outline-settings))) - (heading (substring-no-properties (ivy-read "Heading: " (counsel-outline-candidates settings) - :preselect (max (1- counsel-outline--preselect) 0)))) - ;; keep only lowest heading - (heading (replace-regexp-in-string "\\`.*/" "" heading)) - (slug (downcase heading)) +(defun conlanging--slugify (str) + "Slugify STR." + (let* ((slug (downcase str)) ;; only keep alphanumeric characters (slug (replace-regexp-in-string "[^[:alnum:]]+" " " slug)) (slug (string-trim slug)) @@ -68,14 +63,33 @@ (slug (replace-regexp-in-string "[óòōôö]" "o" slug)) (slug (replace-regexp-in-string "[ńǹñ]" "n" slug)) (slug (replace-regexp-in-string "[úùûü]" "u" slug)) - (slug (replace-regexp-in-string "ý" "y" slug)) - (slug (replace-regexp-in-string " " "-" slug))) + (slug (replace-regexp-in-string "ý" "y" slug))) + (replace-regexp-in-string " " "-" slug))) + +(defun conlanging--get-heading-slug () + "Select and return a heading and its slug." + (let* ((settings (cdr (assq major-mode counsel-outline-settings))) + (heading (substring-no-properties (ivy-read "Heading: " (counsel-outline-candidates settings) + :preselect (max (1- counsel-outline--preselect) 0)))) + ;; keep only lowest heading + (heading (replace-regexp-in-string "\\`.*/" "" heading)) + (slug (conlanging--slugify heading))) `(,heading . ,slug))) (defun conlanging--get-filename-no-ext () "Get file name of current buffer without its extension." (file-name-sans-extension (file-name-nondirectory (buffer-file-name (buffer-base-buffer))))) +;;;###autoload +(defun conlanging-store-heading-vuepress () + "Store in kill ring a Vuepress link to current heading." + (interactive) + (let* ((filename (conlanging--get-filename-no-ext)) + (heading (nth 4 (org-heading-components))) + (slug (conlanging--slugify heading))) + (org-link--add-to-stored-links (format "file:./%s.md#%s" filename slug) + heading))) + ;;;###autoload (defun conlanging-insert-heading-vuepress () "Insert vuepress-compatible link to heading."