Skip to content

Commit

Permalink
Markdown title extraction - first headline
Browse files Browse the repository at this point in the history
  • Loading branch information
kisaragi-hiu committed Sep 17, 2021
1 parent 24502b7 commit 5bc696a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
17 changes: 15 additions & 2 deletions org-roam-extract.el
Expand Up @@ -280,8 +280,21 @@ Reads from the \"roam_alias\" property."
(buffer-file-name)))
nil))))

(defun org-roam--extract-titles-headline ()
"Return the first headline of the current buffer."
(cl-defgeneric org-roam--extract-titles-headline ()
"Extract the first headline as the document title."
nil)

;; Function body from md-roam's `org-roam--extract-titles-mdheadline'
(cl-defmethod org-roam--extract-titles-headline (&context (major-mode markdown-mode))
"Extract the first headline as a title in Markdown mode."
(save-excursion
(goto-char (point-min))
(when (re-search-forward md-roam-regex-headline nil t 1)
(list (or (match-string-no-properties 1)
(match-string-no-properties 4))))))

(cl-defmethod org-roam--extract-titles-headline (&context (major-mode org-mode))
"Extract the first headline as a title in Org mode."
(let ((headline (save-excursion
(goto-char (point-min))
;; "What happens if a heading star was quoted
Expand Down
1 change: 1 addition & 0 deletions tests/roam-files/titles/headline.md
@@ -0,0 +1 @@
# Headline
4 changes: 4 additions & 0 deletions tests/test-org-roam.el
Expand Up @@ -191,6 +191,10 @@
"titles/headline.org")
:to-equal
'("Headline"))
(expect (test #'org-roam--extract-titles-headline
"titles/headline.md")
:to-equal
'("Headline"))
(expect (test #'org-roam--extract-titles-headline
"titles/combination.org")
:to-equal
Expand Down

0 comments on commit 5bc696a

Please sign in to comment.