(internal): use regexp search for link-extraction (#1066)

Replace call to org-element-parse-buffer with simple regexp search.
This commit is contained in:
Jethro Kuan
2020-08-25 18:24:29 +08:00
committed by GitHub
parent 0a64a5def4
commit 4f0b1b8d43

View File

@ -579,55 +579,51 @@ FILE-FROM is typically the buffer file path, but this may not exist, for example
in temp buffers. In cases where this occurs, we do know the file path, and pass in temp buffers. In cases where this occurs, we do know the file path, and pass
it as FILE-PATH." it as FILE-PATH."
(require 'org-ref nil t) (require 'org-ref nil t)
(let ((file-path (or file-path (unless file-path
(file-truename (buffer-file-name)))) (setq file-path (file-truename (buffer-file-name))))
links) (let (links)
(org-element-map (org-element-parse-buffer) 'link (save-excursion
(lambda (link) (goto-char (point-min))
(let* ((type (org-element-property :type link)) (while (re-search-forward org-link-any-re nil t)
(path (org-element-property :path link)) (save-excursion
(start (org-element-property :begin link))) (goto-char (match-beginning 0))
(goto-char start) (let* ((link (org-element-link-parser))
(let* ((element (org-element-at-point)) (type (org-element-property :type link))
(path (org-element-property :path link))
(element (org-element-at-point))
(begin (or (org-element-property :content-begin element) (begin (or (org-element-property :content-begin element)
(org-element-property :begin element))) (org-element-property :begin element)))
(content (or (org-element-property :raw-value element) (content (or (org-element-property :raw-value element)
(buffer-substring-no-properties (buffer-substring-no-properties
begin begin
(or (org-element-property :content-end element) (or (org-element-property :content-end element)
(org-element-property :end element))))) (org-element-property :end element)))))
(content (string-trim content)) (content (string-trim content))
;; Expand all relative links to absolute links (content (org-roam--expand-links content file-path))
(content (org-roam--expand-links content file-path))) (properties (list :outline (mapcar (lambda (path)
(let ((properties (list :outline (mapcar (lambda (path) (org-roam--expand-links path file-path))
(org-roam--expand-links path file-path)) (org-roam--get-outline-path))
(org-roam--get-outline-path)) :content content
:content content :point begin))
:point begin)) (names (pcase type
(names (pcase type ("id"
("id" (list (car (org-roam-id-find path))))
(list (car (org-roam-id-find path)))) ((pred (lambda (typ)
((pred (lambda (typ) (and (boundp 'org-ref-cite-types)
(and (boundp 'org-ref-cite-types) (-contains? org-ref-cite-types typ))))
(-contains? org-ref-cite-types typ)))) (setq type "cite")
(setq type "cite") (org-ref-split-and-strip-string path))
(org-ref-split-and-strip-string path)) ("fuzzy" (list path))
("fuzzy" (list path)) (_ (if (file-remote-p path)
(_ (if (file-remote-p path) (list path)
(list path) (let ((file-maybe (file-truename
(let ((file-maybe (file-truename (expand-file-name path (file-name-directory file-path)))))
(expand-file-name path (file-name-directory file-path))))) (if (f-exists? file-maybe)
(if (f-exists? file-maybe) (list file-maybe)
(list file-maybe) (list path))))))))
(list path)))))))) (dolist (name names)
(seq-do (lambda (name) (when name
(when name (push (vector file-path name type properties) links)))))))
(push (vector file-path
name
type
properties)
links)))
names))))))
links)) links))
(defun org-roam--extract-headlines (&optional file-path) (defun org-roam--extract-headlines (&optional file-path)