Skip to content

Commit 2f51040

Browse files
committed
Implement block id for cljs
1 parent 3ea2c7a commit 2f51040

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/nextjournal/clerk.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@
648648
(show! "notebooks/onwards.md")
649649
(show! "notebooks/pagination.clj")
650650
(show! "notebooks/how_clerk_works.clj")
651-
(show! "notebooks/hello_clojurescript.cljs")
651+
(show! "notebooks/hello.cljs")
652652
(show! "notebooks/conditional_read.cljc")
653653
(show! "src/nextjournal/clerk/analyzer.clj")
654654
(show! "src/nextjournal/clerk.clj")

src/nextjournal/clerk/parser.cljc

+16-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
(:require #?@(:clj [[clojure.tools.reader :as tools.reader]
55
[taoensso.nippy :as nippy]
66
[multiformats.base.b58 :as b58]
7-
[multiformats.hash :as hash]])
7+
[multiformats.hash :as hash]]
8+
:cljs [[goog.crypt]
9+
[goog.crypt.Sha1]])
810
[clojure.set :as set]
911
[clojure.string :as str]
1012
[clojure.zip]
@@ -333,6 +335,12 @@
333335
(defn sha1-base58 [s]
334336
(->> s hash/sha1 hash/encode b58/format-btc)))
335337

338+
#?(:cljs
339+
(defn hash-sha1 [x]
340+
(let [hasher (goog.crypt.Sha1.)]
341+
(.update hasher (goog.crypt/stringToUtf8ByteArray (pr-str x)))
342+
(.digest hasher))))
343+
336344
(defn guess-var
337345
"An best guess to say if the given `form` defines a var without running
338346
macroexpansion. Will be refined during analysis."
@@ -347,6 +355,10 @@
347355
(guess-var '(def my-range (range 500)))
348356
(guess-var '(defonce !state (atom {}))))
349357

358+
(defn supports-meta? [x]
359+
#?(:clj (instance? clojure.lang.IObj x)
360+
:cljs (satisfies? IMeta x)))
361+
350362
(defn get-block-id [!id->count {:as block :keys [form type doc]}]
351363
(let [id->count @!id->count
352364
id (if-let [var (if (contains? block :vars)
@@ -355,12 +367,11 @@
355367
var
356368
(let [hash-fn (fn [x]
357369
#?(:clj (-> x nippy/fast-freeze sha1-base58)
358-
:cljs (throw (ex-info "hash-fn cljs not implemented for cljs yet" {}))))]
370+
:cljs (hash-sha1 x)))]
359371
(symbol (str *ns*)
360372
(case type
361373
:code (str "anon-expr-" (hash-fn (cond-> form
362-
#?(:clj (instance? clojure.lang.IObj form)
363-
:cljs (throw (ex-info "hash-fn cljs not implemented for cljs yet" {})))
374+
(supports-meta? form)
364375
(with-meta {}))))
365376
:markdown (str "markdown-" (hash-fn doc))))))]
366377
(swap! !id->count update id (fnil inc 0))
@@ -385,11 +396,9 @@
385396
#_(extract-file (clojure.java.io/resource "clojure/core.clj"))
386397
#_(extract-file (clojure.java.io/resource "nextjournal/clerk.clj"))
387398

388-
389399
(defn add-loc [{:as opts :keys [file]} loc form]
390400
(cond-> form
391-
#?(:clj (instance? clojure.lang.IObj form)
392-
:cljs (satisfies? cljs.core.IMeta form))
401+
(supports-meta? form)
393402
(vary-meta merge (cond-> loc
394403
(:file opts) (assoc :clojure.core/eval-file
395404
(str #?(:clj (cond-> (:file opts)

0 commit comments

Comments
 (0)