Files
dotemacs/mostalive/journelly-convert-entry-to-org-roam.el
T

96 lines
4.1 KiB
EmacsLisp

;;; mostalive/convert-journelly-to-org-roam.el -*- lexical-binding: t; -*-
(provide 'journelly-convert-entry-to-org-roam)
(cl-defstruct journelly-entry
title
content
heading
url)
(defun journelly-parse-entry (entry-text)
"Parse a journelly entry and return a journelly-entry struct."
(let* ((lines (split-string entry-text "\n" t)) ; t omits null strings
(heading (car lines))
(url-line (cl-find-if (lambda (l) (string-match-p "https?://" l)) lines))
(url-pos (cl-position url-line lines :test 'equal))
;; Title is the non-empty line before the URL
(title-line (when url-pos
(cl-find-if (lambda (l) (not (string-match-p "^[[:space:]]*$" l)))
(reverse (cl-subseq lines 0 url-pos)))))
;; Content is everything except heading, title, and URL
(content-lines (cl-remove-if (lambda (l)
(or (equal l heading)
(equal l title-line)
(equal l url-line)))
lines)))
(make-journelly-entry
:title title-line
:content (string-trim (mapconcat 'identity content-lines "\n"))
:heading heading
:url url-line)))
(defun my/journelly-add-properties (url journelly-heading)
"Add ROAM_REFS and JOURNELLY_TITLE to the current node's property drawer."
(org-with-point-at 1
(when url
(org-roam-property-add "ROAM_REFS" url))
(when journelly-heading
(org-roam-property-add "JOURNELLY_TITLE" journelly-heading))))
(defun journelly-convert-entry-to-org-roam ()
"Interactive function to convert journelly entry to org-roam.
Opens the capture buffer, adds url as ROAM_REFS
and then lets you review before finalizing."
(interactive)
(let* ((entry-text (if (use-region-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(buffer-substring-no-properties (point-min) (point-max))))
(parsed (journelly-parse-entry entry-text))
(title (journelly-entry-title parsed))
(content (journelly-entry-content parsed))
(heading (journelly-entry-heading parsed))
(url (journelly-entry-url parsed))
(body (concat (or content "") "\n")))
(org-roam-capture-
:node (org-roam-node-create :title title)
:props (list :immediate-finish nil
:jump-to-captured t)
:templates `(("d" "default" plain
,body
:target (file+head "%<%Y%m%d%H%M%S>-${slug}.org"
"#+title: ${title}\n\n")
:unnarrowed t)))
(my/journelly-add-properties url heading)))
(ert-deftest test-journelly-parse-basic ()
(let* ((entry " * [2025-10-08 Wed 22:39] @ -
Paul is #XP
Do software engineering professionals still read? - survey results
https://paulgrenyer.blogspot.com/2025/10/do-software-engineering-professionals.html?m=1")
(parsed (journelly-parse-entry entry)))
(should (equal (journelly-entry-heading parsed)
" * [2025-10-08 Wed 22:39] @ -"))
(should (equal (journelly-entry-title parsed)
"Do software engineering professionals still read? - survey results"))
(should (equal (journelly-entry-url parsed)
"https://paulgrenyer.blogspot.com/2025/10/do-software-engineering-professionals.html?m=1"))
(should (equal (journelly-entry-content parsed)
"Paul is #XP"))))
(ert-deftest test-journelly-parse-with-extra-text-after-url ()
(let* ((entry " * [2025-10-08 Wed 22:39] @ -
Some intro text
Article Title
https://example.com
Some notes after the URL")
(parsed (journelly-parse-entry entry)))
(should (equal (journelly-entry-title parsed) "Article Title"))
(should (equal (journelly-entry-url parsed) "https://example.com"))
(should (string-match-p "Some intro text" (journelly-entry-content parsed)))
(should (string-match-p "Some notes after the URL" (journelly-entry-content parsed)))))
(ert "test-journelly-*")