From 63a0b28bea0cd2829334a880bbdf930c78fd9866 Mon Sep 17 00:00:00 2001 From: sterni Date: Tue, 6 Apr 2021 14:59:32 +0200 Subject: refactor(panettone): remove code duplication in render-markdown Move the common part (encoding/decoding json and connecting to cheddar) into request-markdown-from-cheddar. The two render-markdown implementations are now only thin wrappers around that function. Change-Id: I81bb34b684af44228dcad02fca541082e6d060ce Reviewed-on: https://cl.tvl.fyi/c/depot/+/2868 Tested-by: BuildkiteCI Reviewed-by: sterni --- web/panettone/src/panettone.lisp | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to 'web/panettone') diff --git a/web/panettone/src/panettone.lisp b/web/panettone/src/panettone.lisp index 10d501d8aea9..52908e147835 100644 --- a/web/panettone/src/panettone.lisp +++ b/web/panettone/src/panettone.lisp @@ -8,11 +8,10 @@ "Render the argument, or the elements of the argument, as markdown, and return the same structure")) -(defmethod render-markdown ((markdown string)) - (cdr - (assoc :markdown - (cl-json:decode-json - (drakma:http-request +(defun request-markdown-from-cheddar (input) + "Send the CL value INPUT encoded as JSON to cheddar's + markdown endpoint and return the decoded response." + (let ((s (drakma:http-request (concatenate 'string *cheddar-url* "/markdown") @@ -21,24 +20,18 @@ :content-type "application/json" :external-format-out :utf-8 :external-format-in :utf-8 - :content (json:encode-json-to-string - `((markdown . ,markdown))) - :want-stream t))))) + :content (json:encode-json-to-string input) + :want-stream t))) + (cl-json:decode-json s))) + +(defmethod render-markdown ((markdown string)) + (cdr (assoc :markdown + (request-markdown-from-cheddar + `((markdown . ,markdown)))))) (defmethod render-markdown ((markdown hash-table)) (alist-hash-table - (cl-json:decode-json - (drakma:http-request - (concatenate 'string - *cheddar-url* - "/markdown") - :accept "application/json" - :method :post - :content-type "application/json" - :external-format-out :utf-8 - :external-format-in :utf-8 - :content (json:encode-json-to-string markdown) - :want-stream t)))) + (request-markdown-from-cheddar markdown))) (defun markdownify-comment-bodies (comments) "Convert the bodies of the given list of comments to markdown in-place using -- cgit 1.4.1