From 710c2117fc64d79cca5f65c5ebb764aaf80b8686 Mon Sep 17 00:00:00 2001
From: Andrea Amantini <lo.zampino@gmail.com>
Date: Fri, 27 Sep 2024 13:27:18 +0200
Subject: [PATCH 1/3] Drop js-interop dependency

---
 deps.edn                           |  9 +++---
 src/nextjournal/markdown/impl.cljs | 46 ++++++++++++------------------
 2 files changed, 23 insertions(+), 32 deletions(-)

diff --git a/deps.edn b/deps.edn
index 960fc66..d16191d 100644
--- a/deps.edn
+++ b/deps.edn
@@ -1,8 +1,5 @@
 {:paths ["src" "resources" "classes"]
- :mvn/repos {"jitpack.io" {:url "https://jitpack.io"}}
- :deps {applied-science/js-interop {:mvn/version "0.3.3"}
-        org.clojure/data.json {:mvn/version "2.4.0"}
-        org.commonmark/commonmark {:mvn/version "0.23.0"}
+ :deps {org.commonmark/commonmark {:mvn/version "0.23.0"}
         org.commonmark/commonmark-ext-autolink {:mvn/version "0.23.0"}
         org.commonmark/commonmark-ext-footnotes {:mvn/version "0.23.0"}
         org.commonmark/commonmark-ext-task-list-items {:mvn/version "0.23.0"}
@@ -32,7 +29,9 @@
 
   :dev
   {:extra-paths ["dev"]
-   :extra-deps {org.babashka/http-client {:mvn/version "0.3.11"}
+   :extra-deps {applied-science/js-interop {:mvn/version "0.3.3"}
+                org.babashka/http-client {:mvn/version "0.3.11"}
+                org.clojure/data.json {:mvn/version "2.4.0"}
                 org.clojure/test.check {:mvn/version "1.1.1"}
                 org.graalvm.js/js {:mvn/version "21.3.2.1"}}}
 
diff --git a/src/nextjournal/markdown/impl.cljs b/src/nextjournal/markdown/impl.cljs
index 9ddf5a6..3e67b7c 100644
--- a/src/nextjournal/markdown/impl.cljs
+++ b/src/nextjournal/markdown/impl.cljs
@@ -2,17 +2,25 @@
 (ns nextjournal.markdown.impl
   (:require ["/js/markdown" :as md]
             ["markdown-it/lib/token" :as Token]
-            [applied-science.js-interop :as j]
             [clojure.zip :as z]
+            [goog.object]
             [nextjournal.markdown.utils :as u]))
 
+(defn goget [o k]
+  (goog.object/get o (cond-> k (keyword? k) name)))
+
+(defn get-token-attr [token key]
+  (cond-> (goget token key)
+    (= :attrs key)
+    (->> (into {} (map (juxt (comp keyword first) second))))
+    (= :meta key)
+    (js->clj :keywordize-keys true)))
+
 (extend-type Token
   ILookup
   (-lookup
-    ([this key] (j/get this key))
-    ([this key not-found]
-     (js/console.log :ilookup/not-found this key not-found)
-     (j/get this key not-found))))
+    ([this key] (get-token-attr this key))
+    ([this key not-found] (or (get-token-attr this key) not-found))))
 
 (defn hlevel [{:as _token hn :tag}] (when (string? hn) (some-> (re-matches #"h([\d])" hn) second js/parseInt)))
 
@@ -120,15 +128,15 @@
 
 (defn footnote-label [{:as _ctx ::keys [footnote-offset]} token]
   ;; TODO: consider initial offset in case we're parsing multiple inputs
-  (or (j/get-in token [:meta :label])
+  (or (get-in token [:meta :label])
       ;; inline labels won't have a label
-      (str "inline-note-" (+ footnote-offset (j/get-in token [:meta :id])))))
+      (str "inline-note-" (+ footnote-offset (get-in token [:meta :id])))))
 
 ;; footnotes
 (defmethod apply-token "footnote_ref" [{:as ctx ::keys [label->footnote-ref]} token]
   (let [label (footnote-label ctx token)
         footnote-ref (or (get label->footnote-ref label)
-                         {:type :footnote-ref :inline? (not (j/get-in token [:meta :label]))
+                         {:type :footnote-ref :inline? (not (get-in token [:meta :label]))
                           :ref (count label->footnote-ref)  ;; was (+ (count footnotes) (j/get-in token [:meta :id])) ???
                           :label label})]
     (-> ctx
@@ -141,7 +149,7 @@
     (-> ctx
         (u/update-current-loc (fn [loc]
                                 (u/zopen-node loc {:type :footnote
-                                                   :inline? (not (j/get-in token [:meta :label]))
+                                                   :inline? (not (get-in token [:meta :label]))
                                                    :label label}))))))
 
 ;; inline footnotes^[like this one]
@@ -160,20 +168,7 @@
 (defmethod apply-token "footnote_anchor" [doc _token] doc)
 
 (comment
-  (-> "some text^[inline note]
-"
-      md/tokenize flatten-tokens
-      #_ parse
-      #_ u/insert-sidenote-containers)
-
-  (-> empty-doc
-      (update :text-tokenizers (partial map u/normalize-tokenizer))
-      (apply-tokens (nextjournal.markdown/tokenize "what^[the heck]"))
-      insert-sidenote-columns
-      (apply-tokens (nextjournal.markdown/tokenize "# Hello"))
-      insert-sidenote-columns
-      (apply-tokens (nextjournal.markdown/tokenize "is^[this thing]"))
-      insert-sidenote-columns))
+  (parse "some text^[inline note]"))
 
 ;; tables
 ;; table data tokens might have {:style "text-align:right|left"} attrs, maybe better nested node > :attrs > :style ?
@@ -250,10 +245,7 @@ _this #should be a tag_, but this [_actually #foo shouldnt_](/bar/) is not."
 ;; endregion
 
 ;; region data builder api
-(defn pairs->kmap [pairs] (into {} (map (juxt (comp keyword first) second)) pairs))
-(defn apply-tokens [doc tokens]
-  (let [mapify-attrs-xf (map (fn [x] (j/update! x :attrs pairs->kmap)))]
-    (reduce (mapify-attrs-xf apply-token) doc tokens)))
+(defn apply-tokens [doc tokens] (reduce apply-token doc tokens))
 
 (defn parse
   ([markdown] (parse u/empty-doc markdown))

From 8fb69b4f1faf94add096f68b43834ae857b7b4cc Mon Sep 17 00:00:00 2001
From: Andrea Amantini <lo.zampino@gmail.com>
Date: Mon, 30 Sep 2024 18:18:07 +0200
Subject: [PATCH 2/3] Drop goog.object in favour of unchecked-get

---
 src/nextjournal/markdown/impl.cljs | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/src/nextjournal/markdown/impl.cljs b/src/nextjournal/markdown/impl.cljs
index 3e67b7c..d41a0ee 100644
--- a/src/nextjournal/markdown/impl.cljs
+++ b/src/nextjournal/markdown/impl.cljs
@@ -3,14 +3,13 @@
   (:require ["/js/markdown" :as md]
             ["markdown-it/lib/token" :as Token]
             [clojure.zip :as z]
-            [goog.object]
             [nextjournal.markdown.utils :as u]))
 
-(defn goget [o k]
-  (goog.object/get o (cond-> k (keyword? k) name)))
+(defn get* [o k]
+  (unchecked-get o (cond-> k (keyword? k) name)))
 
 (defn get-token-attr [token key]
-  (cond-> (goget token key)
+  (cond-> (get* token key)
     (= :attrs key)
     (->> (into {} (map (juxt (comp keyword first) second))))
     (= :meta key)

From 93bec390e19a07ac6f1c2ca9f0d1583a89d0c1c5 Mon Sep 17 00:00:00 2001
From: Andrea Amantini <lo.zampino@gmail.com>
Date: Tue, 1 Oct 2024 17:28:48 +0200
Subject: [PATCH 3/3] Fix deps.edn after merge

---
 deps.edn | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/deps.edn b/deps.edn
index d16191d..da008c2 100644
--- a/deps.edn
+++ b/deps.edn
@@ -1,4 +1,4 @@
-{:paths ["src" "resources" "classes"]
+{:paths ["src" "resources"]
  :deps {org.commonmark/commonmark {:mvn/version "0.23.0"}
         org.commonmark/commonmark-ext-autolink {:mvn/version "0.23.0"}
         org.commonmark/commonmark-ext-footnotes {:mvn/version "0.23.0"}
@@ -57,5 +57,6 @@
 
   :build
   {:ns-default build
-   :deps {io.github.clojure/tools.build {:git/tag "v0.6.1" :git/sha "515b334"}
+   :jvm-opts ["-Dclojure.main.report=stderr"]
+   :deps {io.github.clojure/tools.build {:git/tag "v0.10.3" :git/sha "15ead66"}
           io.github.slipset/deps-deploy {:git/sha "b4359c5d67ca002d9ed0c4b41b710d7e5a82e3bf"}}}}}