|
4 | 4 | (:require #?@(:clj [[clojure.tools.reader :as tools.reader]
|
5 | 5 | [taoensso.nippy :as nippy]
|
6 | 6 | [multiformats.base.b58 :as b58]
|
7 |
| - [multiformats.hash :as hash]]) |
| 7 | + [multiformats.hash :as hash]] |
| 8 | + :cljs [[goog.crypt] |
| 9 | + [goog.crypt.Sha1]]) |
8 | 10 | [clojure.set :as set]
|
9 | 11 | [clojure.string :as str]
|
10 | 12 | [clojure.zip]
|
|
333 | 335 | (defn sha1-base58 [s]
|
334 | 336 | (->> s hash/sha1 hash/encode b58/format-btc)))
|
335 | 337 |
|
| 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 | + |
336 | 344 | (defn guess-var
|
337 | 345 | "An best guess to say if the given `form` defines a var without running
|
338 | 346 | macroexpansion. Will be refined during analysis."
|
|
347 | 355 | (guess-var '(def my-range (range 500)))
|
348 | 356 | (guess-var '(defonce !state (atom {}))))
|
349 | 357 |
|
| 358 | +(defn supports-meta? [x] |
| 359 | + #?(:clj (instance? clojure.lang.IObj x) |
| 360 | + :cljs (satisfies? IMeta x))) |
| 361 | + |
350 | 362 | (defn get-block-id [!id->count {:as block :keys [form type doc]}]
|
351 | 363 | (let [id->count @!id->count
|
352 | 364 | id (if-let [var (if (contains? block :vars)
|
|
355 | 367 | var
|
356 | 368 | (let [hash-fn (fn [x]
|
357 | 369 | #?(: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)))] |
359 | 371 | (symbol (str *ns*)
|
360 | 372 | (case type
|
361 | 373 | :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) |
364 | 375 | (with-meta {}))))
|
365 | 376 | :markdown (str "markdown-" (hash-fn doc))))))]
|
366 | 377 | (swap! !id->count update id (fnil inc 0))
|
|
385 | 396 | #_(extract-file (clojure.java.io/resource "clojure/core.clj"))
|
386 | 397 | #_(extract-file (clojure.java.io/resource "nextjournal/clerk.clj"))
|
387 | 398 |
|
388 |
| - |
389 | 399 | (defn add-loc [{:as opts :keys [file]} loc form]
|
390 | 400 | (cond-> form
|
391 |
| - #?(:clj (instance? clojure.lang.IObj form) |
392 |
| - :cljs (satisfies? cljs.core.IMeta form)) |
| 401 | + (supports-meta? form) |
393 | 402 | (vary-meta merge (cond-> loc
|
394 | 403 | (:file opts) (assoc :clojure.core/eval-file
|
395 | 404 | (str #?(:clj (cond-> (:file opts)
|
|
0 commit comments