Skip to content
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

Drop js-interop dependency #29

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{:paths ["src" "resources"]
:deps {applied-science/js-interop {:mvn/version "0.3.3"}
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 @@ -30,7 +29,8 @@

: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
45 changes: 18 additions & 27 deletions src/nextjournal/markdown/impl.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
(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]
[nextjournal.markdown.utils :as u]))

(defn get* [o k]
(unchecked-get o (cond-> k (keyword? k) name)))

(defn get-token-attr [token key]
(cond-> (get* 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 +127,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 +148,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 +167,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 +244,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
Loading