(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 ### 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 - [#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. - [#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` - [#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) (defun org-roam--extract-global-props (props)
"Extract PROPS from the current org buffer. "Extract PROPS from the current org buffer.
The search terminates when the first property is encountered." 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)) (let ((buf (org-element-parse-buffer))
res) res)
(dolist (prop props) (dolist (prop props)
@ -504,7 +508,7 @@ The search terminates when the first property is encountered."
(org-element-property :value kw))) (org-element-property :value kw)))
:first-match t))) :first-match t)))
(push (cons prop p) res))) (push (cons prop p) res)))
res)) res)))
(defun org-roam--expand-links (content path) (defun org-roam--expand-links (content path)
"Crawl CONTENT for relative links and expand them. "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." If FILE-PATH is nil, use the current file."
(let ((file-path (or file-path (let ((file-path (or file-path
(file-truename (buffer-file-name))))) (file-truename (buffer-file-name)))))
(org-element-map (org-element-parse-buffer) 'node-property ;; Use `org-map-region' instead of `org-map-entries' as the latter
(lambda (node-property) ;; would require another step to remove all nil values.
(let ((key (org-element-property :key node-property)) (let ((result nil))
(value (org-element-property :value node-property))) (org-map-region
(when (string= key "ID") (lambda ()
(let* ((id value) (when-let ((id (org-entry-get nil "ID")))
(data (vector id (push (vector id file-path) result)))
file-path))) (point-min) (point-max))
data))))))) result)))
(defun org-roam--extract-titles-title () (defun org-roam--extract-titles-title ()
"Return title from \"#+title\" of the current buffer." "Return title from \"#+title\" of the current buffer."
@ -665,12 +669,15 @@ Reads from the \"roam_alias\" property."
(defun org-roam--extract-titles-headline () (defun org-roam--extract-titles-headline ()
"Return the first headline of the current buffer." "Return the first headline of the current buffer."
(let ((headline (org-element-map (let ((headline (save-excursion
(org-element-parse-buffer) (goto-char (point-min))
'headline ;; "What happens if a heading star was quoted
(lambda (h) ;; before the first heading?"
(org-no-properties (org-element-property :raw-value h))) ;; - `org-map-region' also does this
:first-match t))) ;; - 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 (when headline
(list headline)))) (list headline))))

View file

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