Skip to content

Commit

Permalink
Make clerk editor runnable without custom sci build
Browse files Browse the repository at this point in the history
Co-authored-by: Michiel Borkent <[email protected]>
  • Loading branch information
mk and borkdude committed Oct 5, 2024
1 parent 07b8b08 commit ccdfa2b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 72 deletions.
2 changes: 1 addition & 1 deletion bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
dev
{:doc "Boots and watches both shadow browser test and sci builds"
:depends [yarn-install]
:task (clojure "-M:dev:test:nextjournal/clerk:sci watch sci browser-test")}
:task (clojure "-M:dev:test:nextjournal/clerk:sci watch browser-test")}

cljs:sci
{:doc "watches cljs build"
Expand Down
13 changes: 7 additions & 6 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
:aliases
{:nextjournal/clerk
{:extra-paths ["notebooks" "dev"]
:extra-deps {io.github.nextjournal/clerk {:git/sha "f7b47ebb639ea157a0f72d6a63e7f263179c72a9"
:extra-deps {io.github.nextjournal/clerk {:git/sha "80b3e0832d08b75e13e3b862bd46660cb1c66d5c"
:exclusions [io.github.nextjournal/markdown]}}
:jvm-opts ["-Dclojure.main.report=stderr"
"-Dclerk.resource_manifest={\"/js/viewer.js\" \"js/viewer.js\"}"] ;;
#_"-Dclerk.resource_manifest={\"/js/viewer.js\" \"js/viewer.js\"}"] ;;
:exec-fn nextjournal.clerk/build!
:exec-args {:git/url "https://github.com/nextjournal/markdown"
:paths ["README.md"
Expand Down Expand Up @@ -50,10 +50,11 @@
:sci
{:extra-paths ["sci"]
:main-opts ["-m" "shadow.cljs.devtools.cli"]
:jvm-opts ["-Dclerk.resource_manifest={\"/js/viewer.js\" \"http://localhost:8021/viewer.js\"}"]
:extra-deps {io.github.nextjournal/clerk.render {:git/url "https://github.com/nextjournal/clerk"
:git/sha "f7b47ebb639ea157a0f72d6a63e7f263179c72a9"
:deps/root "render"}}}
#_#_:jvm-opts ["-Dclerk.resource_manifest={\"/js/viewer.js\" \"http://localhost:8021/viewer.js\"}"]
:extra-deps {thheller/shadow-cljs {:mvn/version "2.18.0"}
#_#_io.github.nextjournal/clerk.render {:git/url "https://github.com/nextjournal/clerk"
:git/sha "80b3e0832d08b75e13e3b862bd46660cb1c66d5c"
:deps/root "render"}}}

:build
{:ns-default build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns nextjournal.clerk.sci-ext
(ns nextjournal.markdown.render
(:require ["katex" :as katex]
["react" :as react]
["@codemirror/view" :refer [EditorView highlightActiveLine highlightSpecialChars ViewPlugin keymap]]
Expand All @@ -10,14 +10,11 @@
[nextjournal.clerk.render :as render]
[nextjournal.clerk.render.hooks :as hooks]
[nextjournal.clerk.render.code :as code]
[nextjournal.clerk.sci-env]
[nextjournal.clerk.viewer :as v]
[nextjournal.clojure-mode :as clojure-mode]
[nextjournal.markdown :as md]
[nextjournal.markdown.transform :as md.transform]
[reagent.core :as r]
[sci.core :as sci]
[sci.ctx-store]))
[reagent.core :as r]))

(def theme (j/lit {"&.cm-editor.cm-focused" {:outline "none"}
".cm-activeLine" {:background-color "rgb(226 232 240)"}
Expand All @@ -31,6 +28,7 @@
;; syntax (an LRParser) + support (a set of extensions)
(def clojure-lang (LanguageSupport. (clojure-mode/syntax)
(.. clojure-mode/default-extensions (slice 1))))

(defn on-change-ext [f]
(.. EditorState -transactionExtender
(of (fn [^js tr]
Expand Down Expand Up @@ -63,7 +61,7 @@

(defn eval-string [source]
(when-some [code (not-empty (str/trim source))]
(try {:result (sci/eval-string* (sci.ctx-store/get-ctx) code)}
(try {:result (eval (read-string code))}
(catch js/Error e
{:error (str (.-message e))}))))

Expand All @@ -90,43 +88,33 @@
:block-formula (fn [_ctx node]
[:div {:dangerouslySetInnerHTML {:__html (.renderToString katex (md.transform/->text node) #js {:displayMode true})}}])))

(defn expand-all-by-default [store]
(reify
ILookup
(-lookup [_ k] (get store k true))
IAssociative
(-assoc [_ k v] (expand-all-by-default (assoc store k v)))
IMap
(-dissoc [_ k] (expand-all-by-default (dissoc store k)))))

(sci.ctx-store/swap-ctx! sci/merge-opts
{:namespaces {'md {'parse md/parse}
'md.transform {'->hiccup md.transform/->hiccup}
'md.demo {'editor editor
'renderers markdown-renderers
'inspect-expanded (fn [x]
(r/with-let [expanded-at (r/atom (expand-all-by-default {:hover-path [] :prompt-multi-expand? false}))]
(render/inspect-presented {:!expanded-at expanded-at}
(v/present x))))}}})

(comment
(js/console.log (new LanguageSupport
(j/obj :language (clojure-mode/syntax)
:extensions (.slice clojure-mode/default-extensions 1))))

(js/console.log (clojure-mode/syntax) )
(do foldGutter )
(js/console.log (markdown (j/obj :base markdownLanguage
:addKeymap false
:defaultCodeLanguage
(LanguageSupport. (clojure-mode/syntax)
(.. clojure-mode/default-extensions
(slice 1))))))


;; ["@codemirror/language-data" :refer [languages]]
;; Closure compilation failed with 1 errors
;; --- node_modules/@lezer/php/dist/index.cjs:49
;; Illegal redeclared variable: global

)
#_(defn expand-all-by-default [store]
(reify
ILookup
(-lookup [_ k] (get store k true))
IAssociative
(-assoc [_ k v] (expand-all-by-default (assoc store k v)))
IMap
(-dissoc [_ k] (expand-all-by-default (dissoc store k)))))


(defn inspect-expanded [x]
(r/with-let [expanded-at (r/atom {:hover-path [] :prompt-multi-expand? false})]
(render/inspect-presented {:!expanded-at expanded-at}
(v/present x))))

(defn try [init-text]
(let [text->state (fn [text]
(let [parsed (md/parse text)]
{:parsed parsed
:hiccup (md.transform/->hiccup markdown-renderers parsed)}))
!state (hooks/use-state (text->state init-text))]
[:div.grid.grid-cols-2.m-10
[:div.m-2.p-2.text-xl.border-2.overflow-y-scroll.bg-slate-100 {:style {:height "20rem"}}
[editor {:doc init-text :on-change #(reset! !state (text->state %)) :lang :markdown}]]
[:div.m-2.p-2.font-medium.overflow-y-scroll {:style {:height "20rem"}}
[inspect-expanded (:parsed @!state)]]
[:div.m-2.p-2.overflow-x-scroll
[inspect-expanded (:hiccup @!state)]]
[:div.m-2.p-2.bg-slate-50.viewer-markdown
[v/html (:hiccup @!state)]]]))
26 changes: 7 additions & 19 deletions notebooks/try.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,17 @@
(:require [nextjournal.clerk :as clerk]))
;; _Edit markdown text, see parsed AST and transformed hiccup live. Preview how Clerk renders it._
^{::clerk/width :full
::clerk/visibility {:code :fold}
::clerk/viewer {:render-fn '(fn [_]
(let [init-text "# 👋 Hello Markdown
::clerk/visibility {:code :fold}}
(clerk/with-viewer {:render-fn 'nextjournal.markdown.render/try
:require-cljs true}
"# 👋 Hello Markdown
```clojure id=xxyyzzww
(reduce + [1 2 3])
```
## Subsection
- [x] type **some**
- [x] ~~nasty~~
- [ ] _stuff_ here"
text->state (fn [text]
(let [parsed (md/parse text)]
{:parsed parsed
:hiccup (md.transform/->hiccup md.demo/renderers parsed)}))
!state (nextjournal.clerk.render.hooks/use-state (text->state init-text))]
[:div.grid.grid-cols-2.m-10
[:div.m-2.p-2.text-xl.border-2.overflow-y-scroll.bg-slate-100 {:style {:height "20rem"}}
[md.demo/editor {:doc init-text :on-change #(reset! !state (text->state %)) :lang :markdown}]]
[:div.m-2.p-2.font-medium.overflow-y-scroll {:style {:height "20rem"}}
[md.demo/inspect-expanded (:parsed @!state)]]
[:div.m-2.p-2.overflow-x-scroll
[md.demo/inspect-expanded (:hiccup @!state)]]
[:div.m-2.p-2.bg-slate-50.viewer-markdown
[nextjournal.clerk.viewer/html (:hiccup @!state)]]]))}}
(Object.)
- [ ] _stuff_ here")

#_(clerk/serve! {:port 8989 :browse true})

0 comments on commit ccdfa2b

Please sign in to comment.