Skip to content

Commit

Permalink
Drop js-interop dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino committed Sep 27, 2024
1 parent 75f4200 commit 710c211
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
9 changes: 4 additions & 5 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -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"}
Expand Down Expand Up @@ -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"}}}

Expand Down
46 changes: 19 additions & 27 deletions src/nextjournal/markdown/impl.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand All @@ -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 ?
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 710c211

Please sign in to comment.