diff --git a/lisp/ox.el b/lisp/ox.el index 66d68acac..f656af5ad 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -4050,7 +4050,8 @@ significant." (let ((foundp (funcall find-headline path parent))) (when foundp (throw 'exit foundp)))) (let ((parent-hl (org-export-get-parent-headline link))) - (cons parent-hl (org-export-get-genealogy parent-hl)))) + (if (not parent-hl) (list (plist-get info :parse-tree)) + (cons parent-hl (org-export-get-genealogy parent-hl))))) ;; No destination found: return nil. (and (not match-title-p) (puthash path nil link-cache)))))))) diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el index c85d1ddcd..cbae08a82 100644 --- a/testing/lisp/test-ox.el +++ b/testing/lisp/test-ox.el @@ -1642,7 +1642,14 @@ Another text. (ref:text) (should-not (org-test-with-parsed-data "[[target]]" (org-export-resolve-fuzzy-link - (org-element-map tree 'link 'identity info t) info)))) + (org-element-map tree 'link 'identity info t) info))) + ;; Match fuzzy link even when before first headline. + (should + (eq 'headline + (org-test-with-parsed-data "[[hl]]\n* hl" + (org-element-type + (org-export-resolve-fuzzy-link + (org-element-map tree 'link 'identity info t) info)))))) (ert-deftest test-org-export/resolve-id-link () "Test `org-export-resolve-id-link' specifications."