nextjournal / clerk

⚡️ Moldable Live Programming for Clojure
https://clerk.vision
ISC License
1.74k stars 76 forks source link

Using clerk as a standalone command line tool only displays default home.clj only #581

Closed egri-nagy closed 7 months ago

egri-nagy commented 7 months ago

I tried to create a command line tool to render notebooks in the browser. The whole app can fit here:

(ns clerk-viewer
  (:require [nextjournal.clerk :as clerk])
  (:gen-class))

(defn -main [& args]
  (let [filename (first args)]
    (clerk/serve! {:browse? true :watch-paths []}) 
    (clerk/show! filename)))

but when I create a jar file for this and call

java -jar clerk-viewer.jar notebook.clj the log says the notebook.clj is evaluated, but then /nextjournal/clerk/home.clj is also evaluated and only that one is displayed. So I can only see the default page. What am I doing wrong?

Here is the whole project in case something with the build is wrong: https://codeberg.org/egri-nagy/clerk-viewer

mk commented 7 months ago

Hi Attila,

I think you're running into an issue where the browser request triggered by {:browse? true} causes Clerk to show the homepage (which happens on requests to /). I do see the right thing when navigating to http://localhost:7777/src/clerk_viewer in the browser.

Note that we've recently added the ability to disable the welcome page in #561 by passing a :paths option. I'd recommend trying that with the lastet main e8f275b5cf077ec9441e404c1885ff0b6ee0aef9. Please let me know if that helps!

mk commented 7 months ago

Also worth mentioning: Clerk should is useable standalone using clj -X:

I'm working on some smaller fixes and improvements for this in https://github.com/nextjournal/clerk/compare/standalone-fixes

$ clj -Sdeps '{:deps {io.github.nextjournal/clerk {:git/sha "e5484675ad21a4d79ff1cbc699324b742a25e986"}}}}' -X nextjournal.clerk/serve! :paths '["Hello.md"]' :browse true

Clerk also ships with metadata to be https://github.com/babashka/cli compatible:

clj -Sdeps '{:deps {org.babashka/cli {:mvn/version "0.7.53"} io.github.nextjournal/clerk {:git/sha "e5484675ad21a4d79ff1cbc699324b742a25e986"}}}}' -M -m babashka.cli.exec nextjournal.clerk/serve! --browse

Need to add the missing :paths in the serve for it, though. Have a busy week so I can't promise I'll get to it very soon. PR welcome in the meantime.

mk commented 7 months ago

I've documented the missing options and exposed them to babashka.cli in 333727ad3aa6b5c7d8244bb0b7703b757e9904c4. I'm considering to add a dependency on babashka.cli and expose a main function directly (please let me know if you'd find that helpful!). For now it works by declaring an extra dep like so:

$ clojure -Sdeps '{:deps {io.github.nextjournal/clerk {:git/sha "333727ad3aa6b5c7d8244bb0b7703b757e9904c4"} org.babashka/cli {:mvn/version "0.7.53"}}}' -M -m babashka.cli.exec nextjournal.clerk/serve! --help
Start the Clerk webserver with an optional a file watcher.

Options: 
  --host           Host or ip for the webserver to listen on, defaults to "locahost".
  --port           Port number for the webserver to listen on, defaults to 7777.
  --browse         Opens the browser on boot when set.
  --watch-paths    Paths on which to watch for changes and show a changed document.
  --show-filter-fn Symbol resolving to a fn to restrict when to show a notebook as a result of file system event.
  --paths          Restricts serving to the given paths, supports glob patterns. Will disable Clerk's homepage when set.
  --paths-fn       Symbol resolving to a 0-arity function returning computed paths.
  --index          Override the name of the index file (default "index.clj|md", will be added to paths.