diff --git a/src/interface/emacs/khoj.el b/src/interface/emacs/khoj.el index 109cdfc2..6db962c8 100644 --- a/src/interface/emacs/khoj.el +++ b/src/interface/emacs/khoj.el @@ -169,74 +169,80 @@ Use `which-key` if available, else display simple message in echo area" (defun khoj--extract-entries-as-markdown (json-response query) "Convert JSON-RESPONSE, QUERY from API to markdown entries." - ;; remove leading (, ) or SPC from extracted entries string - (replace-regexp-in-string - "^[\(\) ]" "" - ;; extract entries from response as single string and convert to entries - (format "# %s\n%s" - query - (mapcar - (lambda (args) - (replace-regexp-in-string - "^\#+" "##" - (format "%s" (cdr (assoc 'entry args))))) - json-response)))) + (thread-last + json-response + ;; Extract and render each markdown entry from response + (mapcar (lambda (json-response-item) + (thread-last + ;; Extract markdown entry from each item in json response + (cdr (assoc 'entry json-response-item)) + ;; Format markdown entry as a string + (format "%s") + ;; Standardize results to 2nd level heading for consistent rendering + (replace-regexp-in-string "^\#+" "##")))) + ;; Render entries into markdown formatted string with query set as as top level heading + (format "# %s\n%s" query) + ;; remove leading (, ) or SPC from extracted entries string + (replace-regexp-in-string "^[\(\) ]" ""))) (defun khoj--extract-entries-as-org (json-response query) "Convert JSON-RESPONSE, QUERY from API to `org-mode' entries." - ;; remove leading (, ) or SPC from extracted entries string - (replace-regexp-in-string - "^[\(\) ]" "" - ;; extract entries from response as single string and convert to entries - (format "* %s\n%s\n#+STARTUP: showall hidestars inlineimages" - query - (mapcar - (lambda (args) - (replace-regexp-in-string - "^\*+" "**" - (format "%s" (cdr (assoc 'entry args))))) - json-response)))) + (let ((org-results-buffer-format-str "* %s\n%s\n#+STARTUP: showall hidestars inlineimages")) + (thread-last + json-response + ;; Extract and render each org-mode entry from response + (mapcar (lambda (json-response-item) + (thread-last + ;; Extract org entry from each item in json response + (cdr (assoc 'entry json-response-item)) + ;; Format org entry as a string + (format "%s") + ;; Standardize results to 2nd level heading for consistent rendering + (replace-regexp-in-string "^\*+" "**")))) + ;; Render entries into org formatted string with query set as as top level heading + (format org-results-buffer-format-str query) + ;; remove leading (, ) or SPC from extracted entries string + (replace-regexp-in-string "^[\(\) ]" "")))) (defun khoj--extract-entries-as-images (json-response query) "Convert JSON-RESPONSE, QUERY from API to html with images." - ;; remove leading (, ) or SPC from extracted entries string - (replace-regexp-in-string - "[\(\) ]$" "" - ;; remove leading (, ) or SPC from extracted entries string - (replace-regexp-in-string - "^[\(\) ]" "" - ;; extract entries from response as single string and convert to entries - (format "\n
\n