Function: markdown-ts--toc-collect-candidates

markdown-ts--toc-collect-candidates is a byte-compiled function defined in markdown-ts-mode-x.el.gz.

Signature

(markdown-ts--toc-collect-candidates TOC-START-NODE TOC-END-NODE HANDLES FROM)

Documentation

Return an alist of toc entry candidates from the current buffer.

HANDLES is a list of base handles; see markdown-ts--toc-handles.

TOC-START-NODE is the "markdown-ts-toc-start" node. TOC-END-NODE is the "markdown-ts-toc-end" node.

If FROM is the symbol above, source candidates from the beginning of the buffer until the toc start node. If below, source candidates from toc start node until the end of the buffer. If all, use the whole buffer.

Remove ignored candidates containing "<!-- markdown-ts-ignore: -->".

Sort the list by the starting character positions of nodes.

The alist is of the form:
  '((handle . node))

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode-x.el.gz
(defun markdown-ts--toc-collect-candidates (toc-start-node
                                            toc-end-node handles from)
  "Return an alist of toc entry candidates from the current buffer.
HANDLES is a list of base handles; see `markdown-ts--toc-handles'.

TOC-START-NODE is the \"markdown-ts-toc-start\" node.
TOC-END-NODE is the \"markdown-ts-toc-end\" node.

If FROM is the symbol `above', source candidates from the beginning of
the buffer until the toc start node.  If `below', source candidates from
toc start node until the end of the buffer.  If `all', use the whole
buffer.

Remove ignored candidates containing \"<!-- markdown-ts-ignore: -->\".

Sort the list by the starting character positions of nodes.

The alist is of the form:
  \\='((handle . node))"
  (let (candidates beg end)
    (pcase from
      ('above
       (setq beg (point-min))
       (setq end (treesit-node-start toc-start-node)))
      ('below
       (setq beg (treesit-node-end toc-end-node))
       (setq end (point-max)))
      ('all
       (setq beg (point-min))
       (setq end (point-max))))

    ;; It looks like we cannot get them in one fell swoop.
    (dolist (elt handles)
      (setq
       candidates
       (append
        candidates
        (pcase elt
          ('h1 (treesit-query-capture
                'markdown '((atx_heading (atx_h1_marker) (inline) @h1)) beg end))
          ('h2 (treesit-query-capture
                'markdown '((atx_heading (atx_h2_marker) (inline) @h2)) beg end))
          ('h3 (treesit-query-capture
                'markdown '((atx_heading (atx_h3_marker) (inline) @h3)) beg end))
          ('h4 (treesit-query-capture
                'markdown '((atx_heading (atx_h4_marker) (inline) @h4)) beg end))
          ('h5 (treesit-query-capture
                'markdown '((atx_heading (atx_h5_marker) (inline) @h5)) beg end))
          ('h6 (treesit-query-capture
                'markdown '((atx_heading (atx_h6_marker) (inline) @h6)) beg end))
          ('sh1 (treesit-query-capture
                 'markdown '((setext_heading heading_content: (_) @sh1 (setext_h1_underline))) beg end))
          ('sh2 (treesit-query-capture
                 'markdown '((setext_heading heading_content: (_) @sh2 (setext_h2_underline))) beg end))
          ('minus (treesit-query-capture
                   'markdown '((list_item (list_marker_minus) (paragraph (inline) @minus))) beg end))
          ('plus (treesit-query-capture
                  'markdown '((list_item (list_marker_plus) (paragraph (inline) @plus))) beg end))
          ('star (treesit-query-capture
                  'markdown '((list_item (list_marker_star) (paragraph (inline) @star))) beg end))
          ('dot (treesit-query-capture
                 'markdown '((list_item (list_marker_dot) (paragraph (inline) @dot))) beg end))
          ('paren (treesit-query-capture
                   'markdown '((list_item (list_marker_parenthesis) (paragraph (inline) @paren))) beg end))
          ('code (treesit-query-capture
                  'markdown '((fenced_code_block (info_string (language)) @code)) beg end))))))
    (sort
     (seq-remove
      (lambda (elt)
        (string-match-p "<!--[[:blank:]]*markdown-ts-ignore:[[:blank:]]*-->"
                        (treesit-node-text (cdr elt) t)))
      candidates)
     :key (lambda (elt)
            (treesit-node-start (cdr elt)))
     :lessp #'<
     :in-place t)))