From c91bd7be9275a67dd775c715f6f829f2131e5499 Mon Sep 17 00:00:00 2001 From: Jethro Kuan Date: Wed, 12 Feb 2020 21:16:29 +0800 Subject: [PATCH] add org-roam--title-to-id --- org-roam.el | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/org-roam.el b/org-roam.el index 6403e81..e0e3c8a 100644 --- a/org-roam.el +++ b/org-roam.el @@ -181,6 +181,14 @@ If `ABSOLUTE', return the absolute file-path. Else, return the relative file-pat (or (org-roam--get-title file-path) (org-roam--get-id file-path))) +(defun org-roam--title-to-id (title) + "Convert TITLE to id." + (let* ((s (s-downcase title)) + (s (replace-regexp-in-string "[^a-zA-Z0-9_ ]" "" s)) + (s (s-split " " s)) + (s (s-join "_" s))) + s)) + ;;; Creating org-roam files (defun org-roam--populate-title (file &optional title) "Populate title line for FILE using TITLE, if provided. @@ -237,7 +245,7 @@ If not provided, derive the title from the file name." (org-roam--find-all-files))) (title (completing-read "File: " completions)) (id (or (cadr (assoc title completions)) - (read-string "Enter new file id: ")))) + (read-string "Enter new file id: " (org-roam--title-to-id title))))) (let ((file-path (org-roam--get-file-path id))) (unless (file-exists-p file-path) (org-roam--make-file file-path title)) @@ -265,7 +273,8 @@ If not provided, derive the title from the file name." (title-or-id (completing-read "File: " completions)) (file-path (cadr (assoc title-or-id completions)))) (unless file-path - (let ((id (read-string "Enter new file id: "))) + (let ((id (read-string "Enter new file id: " + (org-roam--title-to-id title-or-id)))) (setq file-path (org-roam--get-file-path id t)))) (unless (file-exists-p file-path) (org-roam--make-file file-path title-or-id))