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
Changes from 1 commit
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
Next Next commit
Drop js-interop dependency
  • Loading branch information
zampino committed Sep 27, 2024
commit 710c2117fc64d79cca5f65c5ebb764aaf80b8686
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"}
@@ -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"}}}

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