(internal): speed up extraction of global props, headlines, and title (#1061)
Some checks failed
Docs / build (push) Has been cancelled
CI / build (snapshot) (push) Has been cancelled

Avoids usage of org-element-parse-buffer, preferring simple regex searches.
This commit is contained in:
Kisaragi Hiu 2020-08-24 23:14:15 +09:00 committed by GitHub
parent f6bf9d9401
commit 82ac6b6b9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 26 deletions

View file

@ -10,6 +10,7 @@ In this release we support fuzzy links of the form `[[Title]]`, `[[*Headline]]`
### Features
- [#1061](https://github.com/org-roam/org-roam/pull/1061) Speed up the extraction of file properties, headlines, and titles
- [#1046](https://github.com/org-roam/org-roam/pull/1046) New user option to exclude files from Org-roam
- [#1032](https://github.com/org-roam/org-roam/pull/1032) File changes are now propagated to the database on idle timer. This prevents large wait times during file saves.
- [#974](https://github.com/org-roam/org-roam/pull/974) Protect region targeted by `org-roam-insert`

View file

@ -495,6 +495,10 @@ Use external shell commands if defined in `org-roam-list-files-commands'."
(defun org-roam--extract-global-props (props)
"Extract PROPS from the current org buffer.
The search terminates when the first property is encountered."
(if (functionp 'org-collect-keywords)
(->> (org-collect-keywords props)
;; convert (("TITLE" "my title")) to (("TITLE" . "my title"))
(mapcar (pcase-lambda (`(,k ,v)) (cons k v))))
(let ((buf (org-element-parse-buffer))
res)
(dolist (prop props)
@ -504,7 +508,7 @@ The search terminates when the first property is encountered."
(org-element-property :value kw)))
:first-match t)))
(push (cons prop p) res)))
res))
res)))
(defun org-roam--expand-links (content path)
"Crawl CONTENT for relative links and expand them.
@ -631,15 +635,15 @@ it as FILE-PATH."
If FILE-PATH is nil, use the current file."
(let ((file-path (or file-path
(file-truename (buffer-file-name)))))
(org-element-map (org-element-parse-buffer) 'node-property
(lambda (node-property)
(let ((key (org-element-property :key node-property))
(value (org-element-property :value node-property)))
(when (string= key "ID")
(let* ((id value)
(data (vector id
file-path)))
data)))))))
;; Use `org-map-region' instead of `org-map-entries' as the latter
;; would require another step to remove all nil values.
(let ((result nil))
(org-map-region
(lambda ()
(when-let ((id (org-entry-get nil "ID")))
(push (vector id file-path) result)))
(point-min) (point-max))
result)))
(defun org-roam--extract-titles-title ()
"Return title from \"#+title\" of the current buffer."
@ -665,12 +669,15 @@ Reads from the \"roam_alias\" property."
(defun org-roam--extract-titles-headline ()
"Return the first headline of the current buffer."
(let ((headline (org-element-map
(org-element-parse-buffer)
'headline
(lambda (h)
(org-no-properties (org-element-property :raw-value h)))
:first-match t)))
(let ((headline (save-excursion
(goto-char (point-min))
;; "What happens if a heading star was quoted
;; before the first heading?"
;; - `org-map-region' also does this
;; - Org already breaks badly when you do that;
;; precede the heading star with a ",".
(re-search-forward org-outline-regexp-bol nil t)
(org-entry-get nil "ITEM"))))
(when headline
(list headline))))

View file

@ -255,7 +255,7 @@
(it "extracts headlines"
(expect (test #'org-roam--extract-headlines
"headlines/headline.org")
:to-equal
:to-have-same-items-as
`(["e84d0630-efad-4017-9059-5ef917908823" ,(test-org-roam--abs-path "headlines/headline.org")]
["801b58eb-97e2-435f-a33e-ff59a2f0c213" ,(test-org-roam--abs-path "headlines/headline.org")])))))