Function: markdown-ts-remove-emphasis

markdown-ts-remove-emphasis is an interactive and byte-compiled function defined in markdown-ts-mode.el.gz.

Signature

(markdown-ts-remove-emphasis)

Documentation

Remove emphasis around point or region.

With an active region, strip the outermost emphasis markers from the selected text. Without a region, find the emphasis node at point using tree-sitter and remove its markers.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
(defun markdown-ts-remove-emphasis ()
  "Remove emphasis around point or region.
With an active region, strip the outermost emphasis markers from
the selected text.  Without a region, find the emphasis node at
point using tree-sitter and remove its markers."
  (interactive)
  (if (use-region-p)
      (let* ((beg (region-beginning))
             (end (region-end))
             (text (buffer-substring beg end))
             (stripped nil))
        (cl-loop for (_key . m) in (sort (copy-sequence
                                          markdown-ts-emphasis-alist)
                                         (lambda (a b)
                                           (> (length (cdr a))
                                              (length (cdr b)))))
                 for re = (concat "\\`" (regexp-quote m)
                                  "\\(\\(?:.\\|\n\\)*\\)"
                                  (regexp-quote m) "\\'")
                 when (string-match re text)
                 do (setq stripped (match-string 1 text))
                 and return nil)
        (when stripped
          (delete-region beg end)
          (insert stripped)))
    ;; Find the outermost emphasis node at point.
    (when-let* ((inner (markdown-ts--emphasis-node-at-point))
                (node (let ((n inner))
                        (while (let ((parent (treesit-node-parent n)))
                                 (when (and parent
                                            (member (treesit-node-type parent)
                                                    '("emphasis"
                                                      "strong_emphasis"
                                                      "strikethrough"
                                                      "code_span")))
                                   (setq n parent))))
                        n))
                (start (treesit-node-start node))
                (end (treesit-node-end node))
                (text (treesit-node-text node t))
                (type (treesit-node-type node))
                (mlen (pcase type
                        ("strong_emphasis"
                         (if (string-prefix-p "_" text) 2 2))
                        ("emphasis"
                         (if (string-prefix-p "_" text) 1 1))
                        ("strikethrough" 2)
                        ("code_span" 1)))
                (inner-text (substring text mlen (- (length text) mlen))))
      (let ((offset (- (point) start mlen)))
        (delete-region start end)
        (goto-char start)
        (insert inner-text)
        (goto-char (+ start (max 0 offset)))))))