Skip to content

Commit 8952634

Browse files
authored
feat: Render result with markdown (#13)
* feat: Render result with markdown * Changelog
1 parent 4e76ec0 commit 8952634

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
99
> Released N/A
1010
1111
* Add support for ChatGPT (#12)
12+
* Render result with markdown (#13)
1213

1314
## 0.1.0
1415
> Released Feb 05, 2023

Eask

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
(depends-on "emacs" "26.1")
1717
(depends-on "openai")
18+
(depends-on "markdown-mode")
1819
(depends-on "spinner")
1920

2021
(setq network-security-level 'low) ; see https://github.com/jcs090218/setup-emacs-windows/issues/156#issuecomment-932956432

codegpt.el

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
;; Maintainer: Shen, Jen-Chieh <jcs090218@gmail.com>
77
;; URL: https://github.com/emacs-openai/codegpt
88
;; Version: 0.1.0
9-
;; Package-Requires: ((emacs "26.1") (openai "0.1.0") (spinner "1.7.4"))
9+
;; Package-Requires: ((emacs "26.1") (openai "0.1.0") (markdown-mode "2.1") (spinner "1.7.4"))
1010
;; Keywords: convenience codegpt
1111

1212
;; This file is not part of GNU Emacs.
@@ -36,6 +36,7 @@
3636
(require 'openai)
3737
(require 'openai-chat)
3838
(require 'openai-completion)
39+
(require 'markdown-mode)
3940
(require 'spinner)
4041

4142
(defgroup codegpt nil
@@ -138,16 +139,15 @@
138139
;;
139140
;;; Application
140141

141-
(defmacro codegpt--ask-in-buffer (instruction &rest body)
142-
"Insert INSTRUCTION then execute BODY form."
143-
(declare (indent 1))
144-
`(progn
145-
(openai--pop-to-buffer codegpt-buffer-name) ; create it
146-
(openai--with-buffer codegpt-buffer-name
147-
(codegpt-mode)
148-
(erase-buffer)
149-
(insert ,instruction "\n\n")
150-
,@body)))
142+
(defun codegpt--render-markdown (content)
143+
"Render CONTENT in markdown."
144+
(if (featurep 'markdown-mode)
145+
(with-temp-buffer
146+
(insert content)
147+
(delay-mode-hooks (markdown-mode))
148+
(ignore-errors (font-lock-ensure))
149+
(buffer-string))
150+
content))
151151

152152
(defun codegpt--fill-region (start end)
153153
"Like function `fill-region' (START to END), improve readability."
@@ -160,6 +160,17 @@
160160
(fill-region (line-beginning-position) (line-end-position)))
161161
(forward-line 1))))
162162

163+
(defmacro codegpt--ask-in-buffer (instruction &rest body)
164+
"Insert INSTRUCTION then execute BODY form."
165+
(declare (indent 1))
166+
`(progn
167+
(openai--pop-to-buffer codegpt-buffer-name) ; create it
168+
(openai--with-buffer codegpt-buffer-name
169+
(codegpt-mode)
170+
(erase-buffer)
171+
(insert ,instruction "\n\n")
172+
,@body)))
173+
163174
(defun codegpt--internal (instruction start end)
164175
"Do INSTRUCTION with partial code.
165176
@@ -187,8 +198,10 @@ boundaries of that region in buffer."
187198
(cl-case codegpt-tunnel
188199
(`completion
189200
(let* ((choices (openai--data-choices data))
190-
(result (openai--get-choice choices)))
191-
(insert (string-trim result) "\n")))
201+
(result (openai--get-choice choices))
202+
(result (string-trim result))
203+
(result (codegpt--render-markdown result)))
204+
(insert result "\n")))
192205
(`chat
193206
(let ((choices (let-alist data .choices))
194207
(result))
@@ -197,7 +210,9 @@ boundaries of that region in buffer."
197210
(let-alist .message
198211
(setq result (string-trim .content)))))
199212
choices)
200-
(insert (string-trim result) "\n"))))
213+
(setq result (string-trim result)
214+
result (codegpt--render-markdown result))
215+
(insert result "\n"))))
201216
(codegpt--fill-region original-point (point))))
202217
(unless codegpt-focus-p
203218
(select-window original-window)))

0 commit comments

Comments
 (0)