Browse Source

enhance: add append command

Fixes https://test.logseq.com/#/page/689ca670-d93f-4fe0-a262-029d95f8450f
Gabriel Horner 1 tháng trước cách đây
mục cha
commit
58270ff934

+ 1 - 0
deps/cli/.carve/ignore

@@ -5,3 +5,4 @@ logseq.cli.commands.graph/list-graphs
 logseq.cli.commands.query/query
 logseq.cli.commands.search/search
 logseq.cli.commands.export/export
+logseq.cli.commands.append/append

+ 1 - 0
deps/cli/CHANGELOG.md

@@ -1,5 +1,6 @@
 ## 0.2.0
 * Add export command to export graph as markdown
+* Add append command to add text to current page
 * Change export-edn command to default to writing to a file, like the export command
 * Rename --api-query-token options to --api-server-token
 * Add descriptions for most commands to explain in-depth usage

+ 8 - 3
deps/cli/README.md

@@ -12,7 +12,7 @@ This section assumes you have installed the CLI from npm or via the [dev
 setup](#setup). If you haven't, substitute `node cli.mjs` for `logseq` e.g.
 `node.cli.mjs -h`.
 
-All commands can be used offline or on CI. The `search` command and any command that has an api-server-token option require the [HTTP API Server](https://docs.logseq.com/#/page/local%20http%20server) to be turned on.
+All commands except for `append` can be used offline or on CI. The `search` command and any command that has an api-server-token option require the [HTTP API Server](https://docs.logseq.com/#/page/local%20http%20server) to be turned on.
 
 Now let's use it!
 
@@ -26,12 +26,13 @@ Options:
 Commands:
 list                 List graphs
 show                 Show DB graph(s) info
-search [options]     Search current DB graph
+search [options]     Search DB graph
 query [options]      Query DB graph(s)
+export [options]     Export DB graph as Markdown
 export-edn [options] Export DB graph as EDN
+append [options]     Appends text to current page
 help                 Print a command's help
 
-
 $ logseq list
 DB Graphs:
 db-test
@@ -117,6 +118,10 @@ Exported 41 pages to yep_markdown_1756128259.zip
 # Export DB graph as EDN
 $ logseq export-edn woot -f woot.edn
 Exported 16 properties, 16 classes and 36 pages
+
+# Append text to current page
+$ logseq append add this text -a my-token
+Success!
 ```
 
 ## API

+ 4 - 0
deps/cli/src/logseq/cli.cljs

@@ -95,6 +95,10 @@
     :fn (lazy-load-fn 'logseq.cli.commands.export-edn/export)
     :args->opts [:graph] :require [:graph]
     :spec cli-spec/export-edn}
+   {:cmds ["append"] :desc "Appends text to current page"
+    :fn (lazy-load-fn 'logseq.cli.commands.append/append)
+    :args->opts [:args] :require [:args] :coerce {:args []}
+    :spec cli-spec/append}
    {:cmds ["help"] :fn help-command :desc "Print a command's help"
     :args->opts [:command] :require [:command]}
    {:cmds []

+ 19 - 0
deps/cli/src/logseq/cli/commands/append.cljs

@@ -0,0 +1,19 @@
+(ns logseq.cli.commands.append
+  "Command to append to a page"
+  (:require [clojure.string :as string]
+            [logseq.cli.util :as cli-util]
+            [promesa.core :as p]))
+
+(defn append
+  [{{:keys [api-server-token args]} :opts}]
+  (let [text (string/join " " args)]
+    (-> (p/let [resp (cli-util/api-fetch api-server-token "logseq.app.append_block_in_page" [text nil nil])]
+          (if (= 200 (.-status resp))
+            (p/let [body (.json resp)]
+              (if (.-error body)
+                (cli-util/error (str "Failed to append. Ensure the app is in a specific page.\n"
+                                     "API Response: "
+                                     (string/replace (.-error body) "\n" "\\\\n")))
+                (println "Success!")))
+            (cli-util/api-handle-error-response resp)))
+        (p/catch cli-util/command-catch-handler))))

+ 7 - 3
deps/cli/src/logseq/cli/spec.cljs

@@ -35,13 +35,17 @@
    :title-query {:alias :t
                  :desc "Invoke local query on :block/title"}
    :api-server-token {:alias :a
-                     :desc "API server token to query current graph"}})
+                      :desc "API server token to query current graph"}})
 
 (def search
   {:api-server-token {:alias :a
-                     :desc "API server token to search current graph"}
+                      :desc "API server token to search current graph"}
    :raw {:alias :r
          :desc "Print raw response"}
    :limit {:alias :l
            :default 100
-           :desc "Limit max number of results"}})
+           :desc "Limit max number of results"}})
+
+(def append
+  {:api-server-token {:alias :a
+                      :desc "API server token to modify current graph"}})

+ 4 - 4
src/main/logseq/api.cljs

@@ -1079,17 +1079,17 @@
 (defn ^:export append_block_in_page
   [uuid-or-page-name content ^js opts]
   (let [current-page? (or (and (nil? content) (nil? opts))
-                        (and (nil? opts) (some->> content (instance? js/Object))))
+                          (and (nil? opts) (some->> content (instance? js/Object))))
         opts (if current-page? content opts)
         content (if current-page? uuid-or-page-name content)
         uuid-or-page-name (if current-page? (state/get-current-page)
-                            uuid-or-page-name)]
+                              uuid-or-page-name)]
     (p/let [_ (<ensure-page-loaded uuid-or-page-name)
             page? (not (util/uuid-string? uuid-or-page-name))
             page-not-exist? (and page? (nil? (db-model/get-page uuid-or-page-name)))
             _ (and page-not-exist? (page-handler/<create! uuid-or-page-name
-                                     {:redirect? false
-                                      :format (state/get-preferred-format)}))]
+                                                          {:redirect? false
+                                                           :format (state/get-preferred-format)}))]
       (when-let [block (db-model/get-page uuid-or-page-name)]
         (let [target (str (:block/uuid block))]
           (insert_block target content opts))))))