From 8a5a59f39fdbcbeb53e6041d3726b6e6e4ed3711 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Wed, 27 Dec 2023 10:08:53 +0100 Subject: [PATCH] Format code snippets in output --- kagi.el | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/kagi.el b/kagi.el index 2e0537b..e171a7b 100644 --- a/kagi.el +++ b/kagi.el @@ -153,10 +153,23 @@ FastGPT with the following prompt: (replace-match (propertize (match-string 1) 'font-lock-face 'bold) t nil))) (buffer-string))) +(defun kagi--code-to-face (string) + "Convert code inside STRING to faces. + +In the FastGPT output, code is surrounded by three backticks (```)." + (with-temp-buffer + (insert string) + (goto-char (point-min)) + (let ((code-match (rx (seq "```" (group (* (not "`"))) "```")))) + (while (re-search-forward code-match nil t) + (replace-match (propertize (match-string 1) 'font-lock-face 'fixed-pitch) t nil))) + (buffer-string))) + (defun kagi--format-output (output) "Format the OUTPUT by replacing markup elements to proper faces." - (kagi--html-bold-to-face output)) - + (thread-first output + kagi--html-bold-to-face + kagi--code-to-face)) (defun kagi--format-reference-index (i) "Format the index of reference number I." @@ -165,7 +178,6 @@ FastGPT with the following prompt: (defun kagi--format-references (references) "Format the REFRENCES as a string. - The REFERENCES is a part of the JSON response, see https://help.kagi.com/kagi/api/fastgpt.html for more information." (string-join