use edn frontmatter for edit command
" hr " " body))) " header-lines))] " (rest body-lines)))))
This commit is contained in:
parent
314c480c41
commit
3274ffc440
2 changed files with 22 additions and 26 deletions
|
|
@ -23,7 +23,8 @@
|
||||||
(defn update-snippet [id patch]
|
(defn update-snippet [id patch]
|
||||||
(http/patch (str (:backend-host config) "/api/snippet")
|
(http/patch (str (:backend-host config) "/api/snippet")
|
||||||
{:query-params {:id id}
|
{:query-params {:id id}
|
||||||
:headers {:content-type "application/edn"}
|
:headers {:accept "application/edn"
|
||||||
|
:content-type "application/edn"}
|
||||||
:body (pr-str patch)}))
|
:body (pr-str patch)}))
|
||||||
|
|
||||||
(defn create-snippet [{:keys [title slug markdown tags]}]
|
(defn create-snippet [{:keys [title slug markdown tags]}]
|
||||||
|
|
|
||||||
|
|
@ -1,36 +1,31 @@
|
||||||
(ns snippets-cms.tui-actions.edit
|
(ns snippets-cms.tui-actions.edit
|
||||||
(:require
|
(:require
|
||||||
|
[clojure.edn :as edn]
|
||||||
[com.travisshears.gum-utils :as utils]
|
[com.travisshears.gum-utils :as utils]
|
||||||
|
[clojure.pprint :as pprint]
|
||||||
|
[clojure.string :as str]
|
||||||
[snippets-cms.backend-connect :refer [fetch-snippet fetch-snippets
|
[snippets-cms.backend-connect :refer [fetch-snippet fetch-snippets
|
||||||
update-snippet]]
|
update-snippet]]
|
||||||
[snippets-cms.tui-actions.view :as view]))
|
[snippets-cms.tui-actions.view :as view]))
|
||||||
|
|
||||||
(defn prompt-for-edit-type []
|
(defn- pr-str-pretty [value]
|
||||||
(utils/select (map #(hash-map :value (name %) :item %) '(:title
|
(with-out-str (pprint/pprint value)))
|
||||||
:markdown
|
(def hr "---")
|
||||||
:slug
|
(defn- gen-edit-pre-fill [snippet]
|
||||||
:remove-tag
|
(let [header (pr-str-pretty {:title (:title snippet)
|
||||||
:add-tags
|
:slug (:slug snippet)
|
||||||
:done))))
|
:tags (:tags snippet)})
|
||||||
|
body (:markdown snippet)]
|
||||||
|
(str header "\n" hr "\n" body)))
|
||||||
|
|
||||||
(defn edit
|
(defn- parse-update [text]
|
||||||
([snippet]
|
(let [lines (str/split-lines text)
|
||||||
(edit snippet {}))
|
[header-lines, body-lines] (split-with #(not= % hr) lines)
|
||||||
([snippet changes]
|
header (edn/read-string (str/join "\n" header-lines))]
|
||||||
(case (prompt-for-edit-type)
|
(assoc header :markdown (str/join "\n" (rest body-lines)))))
|
||||||
:title (let [new-title (utils/prompt-for "title" :prefill (:title snippet))]
|
|
||||||
(edit (assoc snippet :title new-title) (assoc changes :title new-title)))
|
(defn edit [snippet]
|
||||||
:slug (let [new-slug (utils/prompt-for "slug" :prefill (:slug snippet))]
|
(parse-update (utils/prompt-for-long-form "body" :prefill (gen-edit-pre-fill snippet))))
|
||||||
(edit (assoc snippet :slug new-slug) (assoc changes :slug new-slug)))
|
|
||||||
:remove-tag (let [tag-to-remove (utils/select (map #(hash-map :value % :item %) (:tags snippet)))
|
|
||||||
tags (remove #(= % tag-to-remove) (:tags snippet))]
|
|
||||||
(edit (assoc snippet :tags tags) (assoc changes :tags tags)))
|
|
||||||
:add-tags (let [new-tags (utils/prompt-for-many "tags")
|
|
||||||
tags (concat new-tags (:tags snippet))]
|
|
||||||
(edit (assoc snippet :tags tags) (assoc changes :tags tags)))
|
|
||||||
:markdown (let [new-markdown (utils/prompt-for-long-form "markdown" :prefill (:markdown snippet))]
|
|
||||||
(edit (assoc snippet :markdown new-markdown) (assoc changes :markdown new-markdown)))
|
|
||||||
:done changes)))
|
|
||||||
|
|
||||||
(defn run []
|
(defn run []
|
||||||
(let [snippet-to-edit (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))]
|
(let [snippet-to-edit (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue