Skip to content

Clerk in Babashka #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 81 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
ff2ef75
First sketch of Clerk in Babashka
zampino Oct 18, 2022
36ba59c
Fix bb task
zampino Oct 18, 2022
066161f
Require latest babashka
zampino Oct 18, 2022
ed9b560
clean bb deps
zampino Oct 19, 2022
baa9660
Rely on latest bb for namespace instance check
zampino Oct 19, 2022
4fc3526
Move stubs to bb files
zampino Oct 19, 2022
1493d7b
Pass cli args to bb task
zampino Oct 19, 2022
8406591
Fix add-viewers! and auto-resolve of ::alias keywords
zampino Oct 19, 2022
e5ce7ab
Extract var from def
zampino Oct 19, 2022
a4b3738
Fix static builder in bb runtime
zampino Oct 19, 2022
a642887
Fix markdown viewer for cljs consumption
zampino Oct 19, 2022
b6bd4c4
Add build bb task
zampino Oct 19, 2022
7c95f89
Implement no-cache?
zampino Oct 20, 2022
995e264
Parse markdown via quickjs
zampino Oct 20, 2022
865c088
Check quickjs is available
zampino Oct 20, 2022
38997c8
Lazily copy markdown js module
zampino Oct 20, 2022
10e95c4
More escapes
zampino Oct 20, 2022
a5312a1
Merge branch 'main' into bb-clerk
borkdude May 6, 2025
ef77ecc
bypass editscript
borkdude May 6, 2025
b39c34b
dejavu
borkdude May 6, 2025
6ce1ddb
bypass nippy
borkdude May 6, 2025
9b711cd
wip [skip ci]
borkdude May 6, 2025
b219f09
more stuff
borkdude May 6, 2025
86e0d5d
more
borkdude May 6, 2025
e34c6bd
more
borkdude May 6, 2025
1fe803b
wip
borkdude May 6, 2025
6cc60fc
always-array-map is challenge
borkdude May 6, 2025
48dd946
wip
borkdude May 6, 2025
92c6676
kondo
borkdude May 6, 2025
e1f15d2
wip [skip ci]
borkdude May 12, 2025
0a907a0
wip
borkdude May 12, 2025
142f873
web server starts
borkdude May 29, 2025
e180a18
wip
borkdude May 29, 2025
9587c6d
fix macro
borkdude May 29, 2025
83d4d7f
wip
borkdude May 29, 2025
329c08f
fix build
borkdude May 29, 2025
9c6ee05
kondo
borkdude May 29, 2025
8bdc2b0
wip
borkdude May 29, 2025
4d29767
wip
borkdude May 29, 2025
e7f26d6
caching works
borkdude May 30, 2025
aeee8cd
wip
borkdude May 30, 2025
876b963
debug [skip ci]
borkdude May 30, 2025
61ba530
remove dbg
borkdude Jun 2, 2025
3fc5c6b
parse bb reader conditionals
borkdude Jun 2, 2025
69b5b8d
wip
borkdude Jun 2, 2025
673be31
reduce diff
borkdude Jun 3, 2025
2383705
wip
borkdude Jun 3, 2025
33f1481
fix tests
borkdude Jun 3, 2025
285eff7
fix lint warning
borkdude Jun 3, 2025
9d6a91f
Remove redundant clj-kondo warnings
mk Jun 3, 2025
fb5d2c4
Revert "Remove redundant clj-kondo warnings"
borkdude Jun 3, 2025
2e5f437
wip
borkdude Jun 3, 2025
aec2fc9
wip
borkdude Jun 16, 2025
77fccb7
multiformats upgrade
borkdude Jun 16, 2025
eeb7878
wip
borkdude Jun 16, 2025
1a58c86
wip
borkdude Jun 16, 2025
920baff
wip
borkdude Jun 16, 2025
c933c96
tests
borkdude Jun 17, 2025
c9c9e84
web server tests
borkdude Jun 23, 2025
1aa02cd
dep upgrades
borkdude Jun 23, 2025
447709b
make analyzer tests work in bb (WIP)
borkdude Jun 23, 2025
5bc316b
wip
borkdude Jun 23, 2025
0c613d0
always array map
borkdude Jun 23, 2025
c10cc0f
yes
borkdude Jun 23, 2025
33f64b1
fix clj
borkdude Jun 23, 2025
498e0bd
builder tests
borkdude Jun 23, 2025
8ee2d06
fix more tests
borkdude Jun 23, 2025
66b56bb
more tests
borkdude Jun 23, 2025
6c80884
remove print-simple workaround
borkdude Jun 23, 2025
bd3874b
viewer tests
borkdude Jun 23, 2025
8789194
bb tests
borkdude Jun 23, 2025
7497c9a
temp bb
borkdude Jun 23, 2025
6d94f0d
debug
borkdude Jun 23, 2025
00f010e
Add timings
borkdude Jun 24, 2025
1ae07a8
delete bb-runtime
borkdude Jun 24, 2025
844e616
Address PR review
borkdude Jun 24, 2025
dffaf02
Drop help-doc
mk Jun 24, 2025
1f94bf0
indent
mk Jun 24, 2025
238f3f4
lint
mk Jun 24, 2025
1cb3855
Changelog
mk Jun 24, 2025
75ac056
fix md link
mk Jun 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .clj-kondo/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
taoensso.encore/defonce clojure.core/defonce
taoensso.encore/defalias clojure.core/def
promesa.core/let clojure.core/let
shadow.cljs.modern/defclass clojure.core/defprotocol}
shadow.cljs.modern/defclass clojure.core/defprotocol
nextjournal.clerk.utils/if-bb clojure.core/if}
:linters {:clojure-lsp/unused-public-var {:level :off}
:consistent-alias {:aliases {datomic.api datomic
clojure.string str
Expand Down
49 changes: 48 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
- { os: windows-latest, shell: powershell }
clojure:
- 1.10.3
- 1.12.0
- 1.12.1
defaults:
run:
shell: ${{matrix.sys.shell}}
Expand Down Expand Up @@ -105,6 +105,53 @@ jobs:
bb test:clj :kaocha/reporter '[kaocha.report/documentation]' \
:clojure '"${{ matrix.clojure }}"'

test-bb:
runs-on: ${{matrix.sys.os}}

strategy:
matrix:
sys:
- { os: macos-latest, shell: bash }
- { os: ubuntu-latest, shell: bash }
- { os: windows-latest, shell: powershell }
defaults:
run:
shell: ${{matrix.sys.shell}}

steps:
- uses: actions/checkout@v2

- name: 🔧 Install java
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'

- name: 🔧 Install clojure
uses: DeLaGuardo/setup-clojure@master
with:
bb: latest

- name: 🗝 maven cache
uses: actions/cache@v3
with:
path: |
~/.m2
~/.gitlibs
~/.deps.clj
key: ${{ runner.os }}-maven-test-${{ hashFiles('deps.edn') }}-${{ hashFiles('bb.edn') }}
- name: 🔧 Install bb from dev build (until next release)
shell: bash
run: |
curl -sLO https://raw.githubusercontent.com/babashka/babashka/master/install
chmod +x ./install
./install --dev-build --dir /tmp
cp /tmp/bb "$(which bb)"
- name: 🧪 Run tests
shell: bash
run: |
bb test:bb

static-build:
runs-on: ubuntu-latest
needs: [build-and-upload-viewer-resources]
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Changes can be:

## Unreleased

* 🌟 Add compatibility with Babashka

Currently requires a dev build of Babashka ([6b89b78](https://github.com/babashka/babashka/commit/6b89b78c38e72d296f211e024d4079bf1504f3e4)
or newer).

* 🕵🏻 New dependency analyzer implementation

Based on the analyzer from [hyperfiddle/rfc](https://github.com/hyperfiddle/rcf). This replaces `tools.analyzer` and drops the dependency.
Expand Down
24 changes: 19 additions & 5 deletions bb.edn
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{:min-bb-version "0.9.159"
{:min-bb-version "1.12.202"
:paths ["bb"]
:deps {io.github.nextjournal/dejavu {:git/sha "4980e0cc18c9b09fb220874ace94ba6b57a749ca"}
io.github.nextjournal/cas-client {:git/sha "d9f838937ebc8b645fe5764949e72a6df8e344de"}
mvxcvi/multiformats {:git/url "https://github.com/greglook/clj-multiformats"
:git/sha "1189f1fb26db180cd8dcfd50518cdf553c0ff9e1"}}
:deps {io.github.nextjournal/dejavu {:git/sha "7276cd9cec1bad001d595b52cee9e83a60d43bf0"}
io.github.nextjournal/cas-client {:git/sha "114d3d88d38a2068ec844b0e6c808eaaa6aa64ef"}}
:tasks
{:requires
([tasks :as t]
Expand Down Expand Up @@ -56,6 +54,22 @@
clojure (str ":" clojure-version))
*command-line-args*))}

test:bb {:doc "Run babashka tests"
:extra-paths ["test"]
:extra-deps {io.github.nextjournal/clerk {:local/root "."}
nubank/matcher-combinators {:mvn/version "3.5.1"}
org.clojure/test.check {:mvn/version "1.1.1"}
io.github.cognitect-labs/test-runner {:git/tag "v0.5.1" :git/sha "dfb30dd"}}
:task cognitect.test-runner/-main}

dev:bb {:extra-deps {io.github.nextjournal/clerk {:local/root "."}}
:extra-paths ["notebooks" "resources" "test"]
:task (exec 'tasks/bb-nrepl)}

build:bb {:extra-deps {io.github.nextjournal/clerk {:local/root "."}}
:extra-paths ["notebooks" "resources" "test"]
:task (exec 'nextjournal.clerk/build!)}

playwright:version {:doc "Print used playwright version from ui_tests/yarn.json"
:task (print (->> (babashka.process/shell {:out :string} "grep -E 'playwright-core \"(.*)\"' ui_tests/yarn.lock")
:out
Expand Down
11 changes: 11 additions & 0 deletions bb/tasks.clj
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,14 @@

(defn version []
(shared/version))

(defn bb-nrepl [args]
(System/setProperty "clerk.resource_manifest" (str {"/js/viewer.js" "/js/viewer.js"}))
((requiring-resolve 'babashka.nrepl.server/start-server!) {:host "localhost" :port 1339})
(spit ".nrepl-port" "1339")
((requiring-resolve 'nextjournal.clerk/serve!) args)
(-> (Runtime/getRuntime)
(.addShutdownHook (Thread. (fn []
((requiring-resolve 'nextjournal.clerk/halt!))
(fs/delete ".nrepl-port")))))
(deref (promise)))
13 changes: 7 additions & 6 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{:paths ["src" "resources" "bb"]
:deps {org.clojure/clojure {:mvn/version "1.10.3"}
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/java.classpath {:mvn/version "1.0.0"}
babashka/fs {:mvn/version "0.5.22"}
borkdude/edamame {:mvn/version "1.4.28"}
weavejester/dependency {:mvn/version "0.2.1"}
com.nextjournal/beholder {:mvn/version "1.0.2"}
org.flatland/ordered {:mvn/version "1.15.12"}
io.github.nextjournal/markdown {:mvn/version "0.7.184"}
io.github.nextjournal/markdown {:mvn/version "0.7.186"}
babashka/process {:mvn/version "0.4.16"}
io.github.nextjournal/dejavu {:git/sha "4980e0cc18c9b09fb220874ace94ba6b57a749ca"}
io.github.nextjournal/dejavu {:git/sha "7276cd9cec1bad001d595b52cee9e83a60d43bf0"}
io.github.babashka/sci.nrepl {:mvn/version "0.0.2"}

com.taoensso/nippy {:mvn/version "3.4.2"}

mvxcvi/multiformats {:mvn/version "0.3.107"}
mvxcvi/multiformats {:mvn/version "1.0.125"}
mvxcvi/alphabase {:mvn/version "3.0.185"}
com.pngencoder/pngencoder {:mvn/version "0.13.1"}

http-kit/http-kit {:mvn/version "2.8.0"}
Expand Down Expand Up @@ -41,7 +42,7 @@
cider/cider-nrepl {:mvn/version "0.55.7"}
nrepl/nrepl {:mvn/version "1.3.1"}
com.clojure-goes-fast/clj-async-profiler {:mvn/version "1.3.0"}
io.github.nextjournal/cas-client {:git/sha "d9f838937ebc8b645fe5764949e72a6df8e344de"}
io.github.nextjournal/cas-client {:git/sha "114d3d88d38a2068ec844b0e6c808eaaa6aa64ef"}
org.slf4j/slf4j-nop {:mvn/version "2.0.7"}
org.babashka/cli {:mvn/version "0.5.40"}
org.clojure/data.int-map {:mvn/version "1.3.0"}}
Expand Down Expand Up @@ -93,7 +94,7 @@
:nextjournal.garden/aliases [:demo]}

:build {:deps {io.github.nextjournal/clerk {:local/root "."}
io.github.nextjournal/cas-client {:git/sha "d9f838937ebc8b645fe5764949e72a6df8e344de"}
io.github.nextjournal/cas-client {:git/sha "114d3d88d38a2068ec844b0e6c808eaaa6aa64ef"}
io.github.clojure/tools.build {:git/tag "v0.10.3" :git/sha "15ead66"}
io.github.slipset/deps-deploy {:git/sha "b4359c5d67ca002d9ed0c4b41b710d7e5a82e3bf"}}
:extra-paths ["bb" "src" "resources"] ;; for loading lookup-url in build
Expand Down
31 changes: 31 additions & 0 deletions notebooks/babashka.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(ns babashka
; {:nextjournal.clerk/no-cache true}
(:require [babashka.fs :as fs]
[nextjournal.clerk :as clerk]))

(System/getProperty "babashka.version")

*file*

(fs/exists? *file*)

;;;;

;;;; dude

(+ 1 2 3)

(prn :dude)

java.io.File

(def x 2)

(prn x)

(clerk/with-viewer
{
:render-fn '(fn [_]
[:div {:style {:color "green"}}
"Hello"])}
nil)
5 changes: 5 additions & 0 deletions src/nextjournal/beholder.bb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns nextjournal.beholder
"Babashka runtime no-op stubs")

(defn watch [cb & args] nil)
(defn stop [w] nil)
4 changes: 4 additions & 0 deletions src/nextjournal/clerk.clj
Original file line number Diff line number Diff line change
Expand Up @@ -662,3 +662,7 @@
(clear-cache!)
(halt!)
)

(comment
(with-cache 1)
)
149 changes: 89 additions & 60 deletions src/nextjournal/clerk/always_array_map.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns nextjournal.clerk.always-array-map
"A persistent data structure that is based on array-map, but doesn't turn into a hash-map by using assoc etc.
Prints like a normal Clojure map in the order of insertion.")
Prints like a normal Clojure map in the order of insertion."
(:require [nextjournal.clerk.utils :as utils]))

(set! *warn-on-reflection* true)

Expand All @@ -9,72 +10,100 @@

(declare ->AlwaysArrayMap)

(deftype AlwaysArrayMap [^clojure.lang.PersistentArrayMap the-map]
clojure.lang.ILookup
(valAt [_ k]
(get the-map k))

clojure.lang.Seqable
(seq [_]
(seq the-map))

clojure.lang.IPersistentMap
(assoc [_ k v]
(if (< (count the-map) 8)
(->AlwaysArrayMap (assoc the-map k v))
(->AlwaysArrayMap (assoc-after the-map k v))))

(assocEx [_ _k _v]
(throw (ex-info "Not implemented" {})))

(without [_ k]
(->AlwaysArrayMap (dissoc the-map k)))

clojure.lang.Associative
(containsKey [_ k]
(contains? the-map k))

clojure.lang.IPersistentCollection
(equiv [_ other]
(= the-map other))
(count [_]
(count the-map))

java.lang.Iterable
(iterator [_]
(.iterator the-map))

clojure.lang.IMeta
(meta [_]
(meta the-map))

clojure.lang.IObj
(withMeta [_ meta]
(->AlwaysArrayMap (with-meta the-map meta)))

Object
(toString [_]
"<always-array-map>"))
(utils/if-bb
(defn ->AlwaysArrayMap [m]
(proxy [clojure.lang.APersistentMap clojure.lang.IMeta clojure.lang.IObj]
[]
(valAt
([k]
(get m k))
([k default-value]
(get m k default-value)))
(iterator []
(.iterator ^java.lang.Iterable m))

(containsKey [k] (contains? m k))
(entryAt [k] (when (contains? m k)
(get this k)))
(equiv [other] (= m other))
(empty [] (empty m))
(count [] (count m))
(assoc [k v] (if (< (count m) 8)
(->AlwaysArrayMap (assoc m k v))
(->AlwaysArrayMap (assoc-after m k v))))
(without [k] (->AlwaysArrayMap (dissoc m k)))
(seq [] (seq m))
; a lot of map users expect meta to work
(meta [] (meta m))
(withMeta [meta] (->AlwaysArrayMap (with-meta m meta)))))

(deftype AlwaysArrayMap [^clojure.lang.PersistentArrayMap the-map]
clojure.lang.ILookup
(valAt [_ k]
(get the-map k))

clojure.lang.Seqable
(seq [_]
(seq the-map))

clojure.lang.IPersistentMap
(assoc [_ k v]
(if (< (count the-map) 8)
(->AlwaysArrayMap (assoc the-map k v))
(->AlwaysArrayMap (assoc-after the-map k v))))

(assocEx [_ _k _v]
(throw (ex-info "Not implemented" {})))

(without [_ k]
(->AlwaysArrayMap (dissoc the-map k)))

clojure.lang.Associative
(containsKey [_ k]
(contains? the-map k))

clojure.lang.IPersistentCollection
(equiv [_ other]
(= the-map other))
(count [_]
(count the-map))

java.lang.Iterable
(iterator [_]
(.iterator the-map))

clojure.lang.IMeta
(meta [_]
(meta the-map))

clojure.lang.IObj
(withMeta [_ meta]
(->AlwaysArrayMap (with-meta the-map meta)))

Object
(toString [_]
"<always-array-map>")))

(defn assoc-before [aam k v]
(->AlwaysArrayMap (apply array-map (list* k v (interleave (keys aam) (vals aam))))))

(defn always-array-map [& kvs]
(->AlwaysArrayMap (apply array-map kvs)))

(defmethod print-method AlwaysArrayMap
[v ^java.io.Writer writer]
(.write writer "{")
(let [write-kv! (fn [k v]
(.write writer (pr-str k))
(.write writer " ")
(.write writer (pr-str v)))]
(doseq [[k v] (butlast v)]
(write-kv! k v)
(.write writer ", "))
(let [[k v] (last v)]
(write-kv! k v)))
(.write writer "}"))
(utils/when-not-bb
(defmethod print-method AlwaysArrayMap
[v ^java.io.Writer writer]
(.write writer "{")
(let [write-kv! (fn [k v]
(.write writer (pr-str k))
(.write writer " ")
(.write writer (pr-str v)))]
(doseq [[k v] (butlast v)]
(write-kv! k v)
(.write writer ", "))
(let [[k v] (last v)]
(write-kv! k v)))
(.write writer "}")))

(comment
(pr-str (always-array-map 1 2))
Expand Down
Loading
Loading