Skip to content

Commit

Permalink
Merge pull request #82 from LA-Toth/daily-notes-template
Browse files Browse the repository at this point in the history
Add support for templates - daily notes template
  • Loading branch information
licht1stein committed Dec 11, 2023
2 parents 57168c8 + 7f589f9 commit bd8bebc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
34 changes: 31 additions & 3 deletions README.org
Expand Up @@ -21,6 +21,7 @@ Emacs front-end for [[https://obsidian.md/][Obsidian Notes]].
- [[#searching-notes][Searching notes]]
- [[#finding-all-notes-with-a-tag][Finding all notes with a tag]]
- [[#move-note-to-another-folder][Move note to another folder]]
- [[#templates][Templates]]
- [[#why-obsidianel-and-not][Why obsidian.el and not...]]
- [[#obsidian-app-itself-athens-research-or-any-other-great-app][Obsidian App itself, Athens Research or any other great app?]]
- [[#org-roam-or-any-other-great-emacs-libraries][Org-roam or any other great Emacs libraries?]]
Expand All @@ -42,6 +43,17 @@ Put this in your ~init.el~:
(obsidian-specify-path "~/MY_OBSIDIAN_FOLDER")
;; If you want a different directory of `obsidian-capture':
(setq obsidian-inbox-directory "Inbox")
;; Clicking on a wiki link referring a non-existing file the file can be
;; created in the inbox (t) or next to the file with the link (nil).
;; Default: t - creating in the inbox
;(setq obsidian-wiki-link-create-file-in-inbox nil)
;; You may want to define a folder for daily notes. By default it is the inbox.
;(setq obsidian-daily-notes-directory "Daily Notes")
;; Directory of note templates, unset (nil) by default
;(setq obsidian-templates-directory "Templates")
;; Daily Note template name - requires a template directory. Default: Daily Note Template.md
;(setq obsidian-daily-note-template "Daily Note Template.md")


;; Define obsidian-mode bindings
(add-hook
Expand All @@ -56,10 +68,11 @@ Put this in your ~init.el~:
;; Following backlinks
(local-set-key (kbd "C-c C-b") 'obsidian-backlink-jump)))

;; Optionally you can also bind `obsidian-jump' and `obsidian-capture'
;; Optionally you can also bind a few functions:
;; replace "YOUR_BINDING" with the key of your choice:
(global-set-key (kbd "YOUR_BINDING") 'obsidian-jump)
(global-set-key (kbd "YOUR_BINDING") 'obsidian-capture)
(global-set-key (kbd "YOUR_BINDING") 'obsidian-jump) ;; Opening a note
(global-set-key (kbd "YOUR_BINDING") 'obsidian-capture) ;; Capturing a new note in the inbox
(global-set-key (kbd "YOUR_BINDING") 'obsidian-daily-note) ;; Creating daily note

;; Activate detection of Obsidian vault
(global-obsidian-mode t)
Expand All @@ -77,6 +90,16 @@ Put this in your ~init.el~:
:custom
;; This directory will be used for `obsidian-capture' if set.
(obsidian-inbox-directory "Inbox")
;; Create missing files in inbox? - when clicking on a wiki link
;; t: in inbox, nil: next to the file with the link
;; default: t
;(obsidian-wiki-link-create-file-in-inbox nil)
;; The directory for daily notes (file name is YYYY-MM-DD.md)
(obsidian-daily-notes-directory "Daily Notes")
;; Directory of note templates, unset (nil) by default
;(obsidian-templates-directory "Templates")
;; 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
;; 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)
Expand Down Expand Up @@ -234,6 +257,11 @@ Use ~obsidian-move-file~ to move current note to another folder:
M-x obsidian-move-file RET
#+end_src

** Templates

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.

*** Development tasks
- [X] Specify Obsidian folder and save it in variables
- [X] Enumerate files in the Obsidian folder and save a list
Expand Down
32 changes: 32 additions & 0 deletions obsidian.el
Expand Up @@ -86,6 +86,16 @@
"Subdir to create daily notes with `obsidian-daily-note'. Default: the inbox directory"
:type 'directory)

(defcustom obsidian-templates-directory nil
"Subdir containing templates"
:type 'directory
)

(defcustom obsidian-daily-note-template "Daily Note Template.md"
"Daily notes' template filename in templates directory"
:type 'file
)

(eval-when-compile (defvar local-minor-modes))

(defun obsidian--directory-files-pre28 (orig-func dir &optional full match nosort ignored)
Expand Down Expand Up @@ -492,6 +502,10 @@ in `obsidian-directory' root.
(clean-filename (s-replace "//" "/" filename)))
(find-file (expand-file-name clean-filename) t)
(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))
(save-buffer)))
(add-to-list 'obsidian-files-cache clean-filename)))

;;;###autoload
Expand Down Expand Up @@ -686,6 +700,24 @@ 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)
"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"))
(time (format-time-string "%H:%M:%S"))
(m (point))
(template-content (with-temp-buffer
(insert-file-contents template-filename)
(buffer-string)))
(output-content (replace-regexp-in-string "{{title}}" title template-content))
(output-content (replace-regexp-in-string "{{date}}" date output-content))
(output-content (replace-regexp-in-string "{{time}}" time output-content))
(output-size (length output-content)))
(goto-char (point-min))
(insert output-content)
(message "Template variables replaced and inserted to the buffer")
(goto-char m)))

;;;###autoload
(defun obsidian-backlink-jump ()
"Select a backlink to this file and follow it."
Expand Down

0 comments on commit bd8bebc

Please sign in to comment.