Browse Source

Add set_file_content api to update built-in files like custom.js/css

Tienson Qin 1 week ago
parent
commit
6c1b9fe77b
2 changed files with 20 additions and 0 deletions
  1. 1 0
      src/main/logseq/api.cljs
  2. 19 0
      src/main/logseq/api/db.cljs

+ 1 - 0
src/main/logseq/api.cljs

@@ -88,6 +88,7 @@
 (def ^:export q api-db/q)
 (def ^:export datascript_query api-db/datascript_query)
 (def ^:export custom_query api-db/custom_query)
+(def ^:export set_file_content api-db/set_file_content)
 
 ;; editor
 (def ^:export prepend_block_in_page api-editor/prepend_block_in_page)

+ 19 - 0
src/main/logseq/api/db.cljs

@@ -48,3 +48,22 @@
                                                :disable-reactive? true
                                                :return-promise? true}))]
     (bean/->js (sdk-utils/normalize-keyword-for-json (flatten result)))))
+
+(defn set_file_content
+  [path content]
+  (let [built-in-paths #{"logseq/custom.js"
+                         "logseq/custom.css"
+                         "logseq/publish.js"
+                         "logseq/publish.css"}]
+    (cond
+      (not (string? content))
+      (throw (ex-info "content should be a string"
+                      {:content content}))
+      (not (contains? built-in-paths path))
+      (throw (ex-info "Invalid path"
+                      {:supported-paths built-in-paths}))
+      :else
+      (p/do!
+       (db/transact! [{:file/path path
+                       :file/content content}])
+       true))))