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 <sternenseemann@systemli.org>
This commit is contained in:
sterni 2021-04-06 14:59:32 +02:00
parent c28d9710d6
commit 63a0b28bea

View file

@ -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