Skip to content

Commit

Permalink
Add obsidian-apply-template interactive command
Browse files Browse the repository at this point in the history
Previously the daily notes command used `obsidian-apply-template`,
but it is internal, so it gets the `obsidian--apply-template`.

For the interactive version, the original `obsidian-apply-tempalte`
is used. It list the template files from the templates directory
without extension, like `Daily Note Template`.
If an existing file name is chosen, it is applied, inserted
to the beginning of the current buffer.
  • Loading branch information
LA-Toth committed Dec 12, 2023
1 parent 56f6861 commit ee64884
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ Put this in your ~init.el~:
(add-hook
'obsidian-mode-hook
(lambda ()
;; apply a template from the directory specified by obsidian-tempaltes-directory
;("C-c C-t" . obsidian-apply-template)

;; Replace standard command with Obsidian.el's in obsidian vault:
(local-set-key (kbd "C-c C-o") 'obsidian-follow-link-at-point)

Expand Down Expand Up @@ -101,6 +104,8 @@ Put this in your ~init.el~:
;; Daily Note template name - requires a template directory. Default: Daily Note Template.md
;(setq obsidian-daily-note-template "Daily Note Template.md")
:bind (:map obsidian-mode-map
;; apply a template from the directory specified by obsidian-tempaltes-directory
;("C-c C-t" . obsidian-apply-template)
;; Replace C-c C-o with Obsidian.el's implementation. It's ok to use another key binding.
("C-c C-o" . obsidian-follow-link-at-point)
;; Jump to backlinks
Expand Down Expand Up @@ -262,6 +267,9 @@ Use ~obsidian-move-file~ to move current note to another folder:
Obsidian.el has a basic template support, where the Obsidian app's template placeholders can be used,
without customization. {{title}}, {{date}}, and {{time}} can be used. {{title}} is the name of the file without the extension.

While the daily notes can have an automatic template (see the config examples), for every other file
templates must be applied manually, by calling `obsidian-apply-template`.

*** Development tasks
- [X] Specify Obsidian folder and save it in variables
- [X] Enumerate files in the Obsidian folder and save a list
Expand Down
34 changes: 31 additions & 3 deletions obsidian.el
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,12 @@ In the `obsidian-inbox-directory' if set otherwise in `obsidian-directory' root.
(find-file (expand-file-name clean-filename) t)
(save-buffer)
(add-to-list 'obsidian-files-cache clean-filename)))



(defun obsidian--template-path (filename)
"Returns the full path for the filename"
(s-concat obsidian-directory "/" obsidian-templates-directory "/"filename))

;;;###autoload
(defun obsidian-daily-note ()
"Create new obsidian daily note.
Expand All @@ -504,7 +509,7 @@ in `obsidian-directory' root.
(save-buffer)
(if (and obsidian-templates-directory obsidian-daily-note-template (eq (buffer-size) 0))
(progn
(obsidian-apply-template (s-concat obsidian-directory "/" obsidian-templates-directory "/" obsidian-daily-note-template))
(obsidian--apply-template (obsidian--template-path obsidian-daily-note-template))
(save-buffer)))
(add-to-list 'obsidian-files-cache clean-filename)))

Expand Down Expand Up @@ -700,7 +705,7 @@ See `markdown-follow-link-at-point' and
(_ (-map (lambda (f) (puthash f (obsidian--expand-file-name f) dict)) coll)))
dict))

(defun obsidian-apply-template (template-filename)
(defun obsidian--apply-template (template-filename)
"Apply the template for the current buffer. Template vars: {{title}}, {{date}}, and {{time}}"
(let* ((title (file-name-sans-extension (file-name-nondirectory buffer-file-name)))
(date (format-time-string "%Y-%m-%d"))
Expand Down Expand Up @@ -731,6 +736,29 @@ See `markdown-follow-link-at-point' and
(find-file target))
(message "No backlinks found."))))

(defun obsidian--find-template-files ()
"Finds (returns) all .md files from the template directory"
(if obsidian-templates-directory
(let* ((templates-directory (s-concat obsidian-directory "/" obsidian-templates-directory))
(all-files (directory-files templates-directory))
(markdown-files (-filter '(lambda (filename) (not (s-match-strings-all filename ".*\.md"))) all-files)))
markdown-files)
nil))

;;;###autoload
(defun obsidian-apply-template ()
"Select a template and apply it for the current buffer"
(interactive)
(let* ((templates (obsidian--find-template-files))
(choices (-map 'file-name-sans-extension (-sort #'string< templates))))
(if choices
(let* ((choice (completing-read "Template: " choices))
(template (obsidian--template-path (s-concat choice ".md"))))
(if (f-exists-p template)
(obsidian--apply-template template)
(message "Template file not found")))
(message "No template found."))))

;;;###autoload
(defun obsidian-search ()
"Search Obsidian vault for input."
Expand Down

0 comments on commit ee64884

Please sign in to comment.