diff --git a/lisp/ox.el b/lisp/ox.el index 840645b33..4eb00edb5 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -4829,14 +4829,14 @@ information. Return a list of all exportable headlines as parsed elements. Footnote sections, if any, will be ignored." - (unless (wholenump n) (setq n (plist-get info :headline-levels))) - (org-element-map (plist-get info :parse-tree) 'headline - (lambda (headline) - (unless (org-element-property :footnote-section-p headline) - ;; Strip contents from HEADLINE. - (let ((relative-level (org-export-get-relative-level headline info))) - (unless (> relative-level n) headline)))) - info)) + (let ((limit (plist-get info :headline-levels))) + (setq n (if (wholenump n) (min n limit) limit)) + (org-element-map (plist-get info :parse-tree) 'headline + #'(lambda (headline) + (unless (org-element-property :footnote-section-p headline) + (let ((level (org-export-get-relative-level headline info))) + (and (<= level n) headline)))) + info))) (defun org-export-collect-elements (type info &optional predicate) "Collect referenceable elements of a determined type. diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el index cbae08a82..36ee02625 100644 --- a/testing/lisp/test-ox.el +++ b/testing/lisp/test-ox.el @@ -2485,6 +2485,36 @@ Another text. (ref:text) info)))) + +;;; Tables of Contents + +(ert-deftest test-org-export/collect-headlines () + "Test `org-export-collect-headlines' specifications." + ;; Standard test. + (should + (= 2 + (length + (org-test-with-parsed-data "* H1\n** H2" + (org-export-collect-headlines info))))) + ;; Do not collect headlines below optional argument. + (should + (= 1 + (length + (org-test-with-parsed-data "* H1\n** H2" + (org-export-collect-headlines info 1))))) + ;; Never collect headlines below maximum headline level. + (should + (= 1 + (length + (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2" + (org-export-collect-headlines info))))) + (should + (= 1 + (length + (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2" + (org-export-collect-headlines info 2)))))) + + ;;; Templates