chatgpt-shell icon indicating copy to clipboard operation
chatgpt-shell copied to clipboard

Is there way to remove Markdown?

Open gnusupport opened this issue 2 years ago • 3 comments

Is there any way to automatically remove Markdown markups?

gnusupport avatar Nov 26 '23 07:11 gnusupport

Could try asking not to reply with markdown in chatgpt-shell-system-prompt though in early tries it wasn't super effective (maybe better these days). Otherwise post processing via chatgpt-shell-after-command-functions hook.

xenodium avatar Nov 27 '23 06:11 xenodium

pandoc is good at converting markdown to org-mode or plaintext.

NightMachinery avatar Dec 24 '23 12:12 NightMachinery

Check https://github.com/xenodium/chatgpt-shell/issues/140 @agzam has a pandoc copy solution.

xenodium avatar Dec 26 '23 09:12 xenodium

Yup, yup.... here it is:

(defun yank-as-org ()
  "Convert region of markdown text to org while yanking."
  (interactive)
  (let* ((_ (unless (executable-find "pandoc")
              (user-error "pandoc not found")))
         (beg (if evil-mode
                  (marker-position evil-visual-beginning)
                (region-beginning)))
         (end (if evil-mode
                  (marker-position evil-visual-end)
                (region-end)))
         (region-content (buffer-substring-no-properties beg end))
         (_ (print region-content))
         (converted-content
          (with-temp-buffer
            (insert region-content)
            (shell-command-on-region
             (point-min)
             (point-max)
             "pandoc --wrap=none -f markdown -t org" nil t)
            (buffer-string))))
    (kill-new converted-content)
    (message "yanked Markdown as Org")))
    
(defun yank-as-markdown ()
  "Convert region of Org-mode to markdown while yanking."
  (interactive)
  (let* ((_ (unless (executable-find "pandoc")
              (user-error "pandoc not found")))
         (beg (if evil-mode
                  (marker-position evil-visual-beginning)
                (region-beginning)))
         (end (if evil-mode
                  (marker-position evil-visual-end)
                (region-end)))
         (region-content (buffer-substring-no-properties beg end))
         (_ (print region-content))
         (converted-content
          (with-temp-buffer
            (insert region-content)
            (shell-command-on-region
             (point-min)
             (point-max)
             "pandoc --wrap=none -f org -t markdown_strict" nil t)
            (buffer-string))))
    (kill-new converted-content)
    (message "yanked Org as Markdown")))


(defun maybe-yank-and-convert-a (orig-fun beg end &optional type register yank-handler)
  "Advice function to convert marked region to org before yanking."
  (let ((yank-fn (cond
                  ((derived-mode-p 'markdown-mode)
                   #'yank-as-org)
                  ((derived-mode-p 'org-mode)
                   #'yank-as-markdown))))
    (if (and current-prefix-arg (use-region-p)
             yank-fn)
        (funcall yank-fn)
      (funcall
       orig-fun
       beg end type register
       yank-handler))))
       
       
;; advise 'kill-ring-save or whatever if you're not using Evil
(advice-add 'evil-yank :around #'maybe-yank-and-convert-a)

I know there's some repetition there, it can use some DRYing, I just wrote it, saw that it works and pretty much forgot about it. Maybe one day I'll clean up and make it into a separate package. It basically works this way - if you copy normally, it would just do the normal copy, but if you do copy with a prefix (C-u) - in markdown mode it will copy it as org, and in the org-mode - it'll copy it as markdown.

agzam avatar Oct 04 '24 15:10 agzam