use edn frontmatter for edit command

" hr "
" body)))
" header-lines))]
" (rest body-lines)))))
This commit is contained in:
Travis Shears 2026-03-09 15:40:59 +01:00
parent 314c480c41
commit 3274ffc440
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469
2 changed files with 22 additions and 26 deletions

View file

@ -23,7 +23,8 @@
(defn update-snippet [id patch]
(http/patch (str (:backend-host config) "/api/snippet")
{:query-params {:id id}
:headers {:content-type "application/edn"}
:headers {:accept "application/edn"
:content-type "application/edn"}
:body (pr-str patch)}))
(defn create-snippet [{:keys [title slug markdown tags]}]

View file

@ -1,36 +1,31 @@
(ns snippets-cms.tui-actions.edit
(:require
[clojure.edn :as edn]
[com.travisshears.gum-utils :as utils]
[clojure.pprint :as pprint]
[clojure.string :as str]
[snippets-cms.backend-connect :refer [fetch-snippet fetch-snippets
update-snippet]]
[snippets-cms.tui-actions.view :as view]))
(defn prompt-for-edit-type []
(utils/select (map #(hash-map :value (name %) :item %) '(:title
:markdown
:slug
:remove-tag
:add-tags
:done))))
(defn- pr-str-pretty [value]
(with-out-str (pprint/pprint value)))
(def hr "---")
(defn- gen-edit-pre-fill [snippet]
(let [header (pr-str-pretty {:title (:title snippet)
:slug (:slug snippet)
:tags (:tags snippet)})
body (:markdown snippet)]
(str header "\n" hr "\n" body)))
(defn edit
([snippet]
(edit snippet {}))
([snippet changes]
(case (prompt-for-edit-type)
:title (let [new-title (utils/prompt-for "title" :prefill (:title snippet))]
(edit (assoc snippet :title new-title) (assoc changes :title new-title)))
:slug (let [new-slug (utils/prompt-for "slug" :prefill (:slug 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- parse-update [text]
(let [lines (str/split-lines text)
[header-lines, body-lines] (split-with #(not= % hr) lines)
header (edn/read-string (str/join "\n" header-lines))]
(assoc header :markdown (str/join "\n" (rest body-lines)))))
(defn edit [snippet]
(parse-update (utils/prompt-for-long-form "body" :prefill (gen-edit-pre-fill snippet))))
(defn run []
(let [snippet-to-edit (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))]