org-make-toc
org-make-toc copied to clipboard
org-make-toc error in 'wrong-type-argument stringp nil' with headline that has no text
The org buffer to reproduce:
* TOC
:PROPERTIES:
:TOC: :include all :ignore (this)
:END:
:CONTENTS:
:END:
* one
*
Note the last headline has no text. This causes the string-match-p
call in line 332 to fail. The following is the top few stack frames of the debugger:
Debugger entered--Lisp error: (wrong-type-argument stringp nil)
string-match("\\`COMMENT\\(?:[[:blank:]]\\|\\'\\)" nil nil)
string-match-p("\\`COMMENT\\(?:[[:blank:]]\\|\\'\\)" nil)
(or (and (not (funcall --cl-arg-has-- force 'ignore)) (funcall --cl-entry-match-- :ignore 'this)) (seq-intersection org-make-toc-exclude-tags (org-get-tags)) (string-match-p "\\`COMMENT\\(?:[[:blank:]]\\|\\'\\)" (nth 4 (org-heading-components))))
Applying the following patch can work around the error:
modified org-make-toc.el
@@ -330,7 +330,7 @@ with the destination of the published file."
;; NOTE: The "COMMENT" keyword is not returned as the to-do keyword
;; by `org-heading-components', so it can't be tested as a keyword.
(string-match-p (rx bos "COMMENT" (or blank eos))
- (nth 4 (org-heading-components))))
+ (or (nth 4 (org-heading-components)) "")))
(funcall org-make-toc-link-type-fn)))
(entry-match (property value)
(when-let* ((found-value (entry-property property)))
but it can also produce incorrect TOC with the org buffers like below:
* TOC
:PROPERTIES:
:TOC: :include all :ignore (this)
:END:
:CONTENTS:
- [[#four][four]]
:END:
*
** four
Thanks. This should probably be fixed to, at least, give a useful error. What do you think should happen for "anonymous" headings?
If links can be generated to the "anonymous" headings without too much effort for you, perhaps we can have "placeholder" entries in place of such anonymous headings in the TOC?
An error message would be good enough otherwise. An org doc with such headings is unfinished and thoese headings must to be fixed anyway.
I don't know what links GitHub's renderer generates for anonymous headings. Maybe the best approach would be to show a warning at TOC-generation time and omit that entry from the TOC.