Function: markdown-ts--fontify-atx-heading

markdown-ts--fontify-atx-heading is a byte-compiled function defined in markdown-ts-mode.el.gz.

Signature

(markdown-ts--fontify-atx-heading NODE OVERRIDE START END &rest _)

Documentation

Apply the heading face across an atx_heading NODE.

Layer the face on top of child sub-nodes (e.g. an inline link) so their own faces are preserved. Strip any prior copy of the face first so it does not accumulate when the heading is refontified or its level/type changes during editing. Do not fontify the header's trailing newline. Elide trailing whitespace when hiding markup. Fontify any optional trailing closing-# sequence as a delimiter. The tree-sitter grammar does not produce a separate node for these; per CommonMark they are decorative and must be preceded by a space or tab.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
(defun markdown-ts--fontify-atx-heading (node _override _start _end &rest _)
  "Apply the heading face across an atx_heading NODE.
Layer the face on top of child sub-nodes (e.g. an inline link) so
their own faces are preserved.  Strip any prior copy of the face
first so it does not accumulate when the heading is refontified or
its level/type changes during editing.
Do not fontify the header's trailing newline.
Elide trailing whitespace when hiding markup.
Fontify any optional trailing closing-`#' sequence as a delimiter.  The
tree-sitter grammar does not produce a separate node for these; per
CommonMark they are decorative and must be preceded by a space or tab."
  (let* ((n-start (treesit-node-start node))
         (n-end   (treesit-node-end node))
         (face (let ((marker (treesit-node-child node 0)))
                 (intern
                  (format "markdown-ts-heading-%d"
                          (progn
                            (string-match "[[:blank:]]*\\([#]+\\)"
                                          (treesit-node-text marker t))
                            (- (match-end 1) (match-beginning 1))))))))
    (font-lock--remove-face-from-text-property n-start n-end 'face face)
    (font-lock-append-text-property n-start (1- n-end) 'face face)
    (save-excursion
      (goto-char n-end)
      (skip-chars-backward "[:space:]" n-start)
      (let ((trailing-end (point)))
        (skip-chars-backward "#" n-start)
        (let ((trailing-start (point)))
          (cond ((and (< trailing-start trailing-end)
                      (> trailing-start n-start)
                      (memq (char-before trailing-start) '(?\s ?\t)))
                 ;; Identify the optional trailing closing-# sequence,
                 ;; fontify it as a delimiter, and remove whitespace
                 ;; between the heading text and the delimiter.  The
                 ;; grammar omits a node for this run despite CommonMark.
                 (font-lock--remove-face-from-text-property
                  trailing-start trailing-end
                  'face 'markdown-ts-delimiter)
                 (font-lock-prepend-text-property
                  trailing-start trailing-end
                  'face 'markdown-ts-delimiter)
                 (when markdown-ts-hide-markup
                   (let ((hide-start (save-excursion
                                       (goto-char trailing-start)
                                       (skip-chars-backward "[:space:]" n-start)
                                       (point))))
                     (put-text-property hide-start (pos-eol)
                                        'invisible 'markdown-ts--markup))))
                (markdown-ts-hide-markup
                 ;; Hide trailing whitespace in the nominal case.
                 (put-text-property trailing-end (pos-eol)
                                    'invisible 'markdown-ts--markup))))))))