enhance: add append command

Fixes https://test.logseq.com/#/page/689ca670-d93f-4fe0-a262-029d95f8450f
This commit is contained in:
Gabriel Horner
2025-08-26 09:21:56 -04:00
parent bc46cf7d14
commit 58270ff934
7 changed files with 44 additions and 10 deletions

View File

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

View File

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

11
deps/cli/README.md vendored
View File

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

View File

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

View File

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

View File

@@ -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"}})