diff --git a/kagi.el b/kagi.el index e171a7b..6435cb1 100644 --- a/kagi.el +++ b/kagi.el @@ -136,40 +136,35 @@ https://help.kagi.com/kagi/api/summarizer.html." "--header" "Content-Type: application/json" "--data" "@-"))) -(defun kagi--html-bold-to-face (string) - "Convert HTML tags inside STRING to faces. +(defconst kagi--markup-to-face + '(("" "" 'bold) + ("```" "```" 'fixed-pitch)) + "Contains a mapping from markup elements to faces.") -Fun fact: initial version of this function was generated by +(defun kagi--convert-markup-to-faces (string) + "Convert markup elements inside STRING to faces. + +Fun fact: the initial version of this function was generated by FastGPT with the following prompt: write an Emacs Lisp function that accepts a string with html bold tags, and returns a string with bold face text properties - applied for the tags content atd the tags removed" + applied for the tags content and the tags removed" (with-temp-buffer (insert string) - (goto-char (point-min)) - (let ((bold-match (rx (seq "" (group (* (not "<"))) "")))) - (while (re-search-forward bold-match nil t) - (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))) + (dolist (entry kagi--markup-to-face) + (goto-char (point-min)) + (let ((start (nth 0 entry)) + (end (nth 1 entry)) + (face (nth 2 entry)) + (regexp (rx (seq (literal start) (group (* any)) (literal end))))) + (while (re-search-forward regexp nil t) + (replace-match (propertize (match-string 1) 'font-lock-face face) t nil)))) (buffer-string))) (defun kagi--format-output (output) "Format the OUTPUT by replacing markup elements to proper faces." - (thread-first output - kagi--html-bold-to-face - kagi--code-to-face)) + (kagi--convert-markup-to-faces output)) (defun kagi--format-reference-index (i) "Format the index of reference number I."