Skip to content

Commit

Permalink
Prepare Release of Markdown Module (#31)
Browse files Browse the repository at this point in the history
* Adds build tooling namespace and alias. This is needed for releasing Clerk with markdown-first support.

* Drop `nextjournal.log dependency` of markdown module

* Bump js scriptengine to latest version 21.3.0

* Use clerk via public git url inferred from lib name (dropping `:git/url` key)

* Fixes building classpath by uniforming dep names for nextjournal/log lib

* Fix Graal Polyglot Context when building source from JAR by dropping 
the evaluation of an 'import' statement in favour of using
context option 'js.esm-eval-returns-exports' which allows returning 
exports as a Polyglot value when we evaluate an esm source file.

* Also remove a separate polyglot engine as it doesn't fix the "interpreter 
only" warning when graal libraries are loaded by a stock JVM.

Co-authored-by: Martin Kavalar <[email protected]>
  • Loading branch information
zampino and mk authored Dec 7, 2021
1 parent 71c76cd commit e06e36d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
3 changes: 1 addition & 2 deletions modules/devdocs/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
io.github.nextjournal/cljs-extensions {:local/root "../cljs-extensions"}
io.github.nextjournal/markdown {:local/root "../markdown"}

com.nextjournal/clerk {:git/url "[email protected]:nextjournal/clerk.git"
:git/sha "d2cacf6d3d27768f34b9092c100be9083c6dd1fc"}
io.github.nextjournal/clerk {:git/sha "ebc8f998c761f1b0e2c9fc4750ae87d8f1766faa"}

;; we use shadow-resource directly
thheller/shadow-cljs {:mvn/version "2.15.12"}
Expand Down
31 changes: 31 additions & 0 deletions modules/markdown/build.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(ns build
(:require [clojure.tools.build.api :as b]
[deps-deploy.deps-deploy :as dd]))

(def lib 'io.github.nextjournal/markdown)
(def version (format "0.1.%s" (b/git-count-revs nil)))
(def class-dir "target/classes")
(def basis (b/create-basis {:project "deps.edn"}))
(def jar-file (format "target/%s-%s.jar" (name lib) version))

(defn clean [_]
(b/delete {:path "target"}))

(defn jar [_]
(b/write-pom {:class-dir class-dir
:lib lib
:version version
:basis basis
:src-dirs ["src"]})
(b/copy-dir {:src-dirs ["src" "resources"]
:target-dir class-dir})
(b/jar {:class-dir class-dir
:jar-file jar-file}))

(defn deploy [opts]
(println "Deploying version" jar-file "to Clojars.")
(jar {})
(dd/deploy (merge {:installer :remote
:artifact jar-file
:pom-file (b/pom-path {:lib lib :class-dir class-dir})}
opts)))
9 changes: 6 additions & 3 deletions modules/markdown/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
:deps {applied-science/js-interop {:mvn/version "0.2.6"}
lambdaisland/uri {:mvn/version "1.10.79"}
org.clojure/data.json {:mvn/version "0.2.6"}
org.graalvm.js/js {:mvn/version "21.2.0"}
org.graalvm.js/js-scriptengine {:mvn/version "21.2.0"}
nextjournal/log {:local/root "../log"}}}
org.graalvm.js/js {:mvn/version "21.3.0"}
org.graalvm.js/js-scriptengine {:mvn/version "21.3.0"}}

:aliases {:build {:deps {io.github.clojure/tools.build {:git/tag "v0.6.1" :git/sha "515b334"}
io.github.slipset/deps-deploy {:git/sha "b4359c5d67ca002d9ed0c4b41b710d7e5a82e3bf"}}
:ns-default build}}}
16 changes: 8 additions & 8 deletions modules/markdown/src/nextjournal/markdown.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
[clojure.data.json :as json]
[nextjournal.markdown.parser :as markdown.parser]
[nextjournal.markdown.transform :as markdown.transform])
(:import [org.graalvm.polyglot Context Context$Builder Engine Source Value]))
(:import [org.graalvm.polyglot Context Context$Builder Source Value]))

(set! *warn-on-reflection* true)

(def ^Engine engine (Engine/create))

(def ^Context$Builder context-builder
(doto (Context/newBuilder (into-array String ["js"]))
(.engine engine)
(.option "js.timer-resolution" "1")
(.option "js.java-package-globals" "false")
(.option "js.esm-eval-returns-exports", "true") ;; returns module exports when evaling an esm module file
(.out System/out)
(.err System/err)
(.allowIO true)
(.allowExperimentalOptions true)
(.allowAllAccess true)
(.allowNativeAccess true)))
(.allowNativeAccess true)
(.option "engine.WarnInterpreterOnly" "false")))

(def ^Context ctx (.build context-builder))

(def ^Value MD-imports
(.eval ctx (.build (Source/newBuilder "js"
(str "import MD from '" (.getPath (io/resource "js/markdown.mjs")) "'; MD")
"source.mjs"))))
(.. ctx
(eval (.build (Source/newBuilder "js" (io/resource "js/markdown.mjs"))))
(getMember "default")))

(defn make-js-fn [fn-name]
(let [f (.getMember MD-imports fn-name)]
Expand Down
8 changes: 4 additions & 4 deletions modules/markdown/src/nextjournal/markdown/parser.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"
(:require [clojure.string :as str]
[nextjournal.markdown.transform :as md.transform]
[nextjournal.log :as log]
#?(:cljs [applied-science.js-interop :as j])))
#?@(:cljs [[applied-science.js-interop :as j]
[cljs.reader :as reader]])))

;; clj common accessors
(def get-in* #?(:clj get-in :cljs j/get-in))
Expand All @@ -31,7 +31,7 @@
;; region node operations
;; helpers
(defn inc-last [path] (update path (dec (count path)) inc))
(defn hlevel [{:as _token hn :tag}] (when (string? hn) (some-> (re-matches #"h([\d])" hn) second #?(:clj read-string :cljs cljs.reader/read-string))))
(defn hlevel [{:as _token hn :tag}] (when (string? hn) (some-> (re-matches #"h([\d])" hn) second #?(:clj read-string :cljs reader/read-string))))
(defn parse-fence-info
"Ingests nextjournal, GFM, Pandoc and RMarkdown fenced code block info, returns a map
Expand Down Expand Up @@ -208,7 +208,7 @@ end"
(declare apply-tokens)
(defmulti apply-token (fn [_doc token] (:type token)))
(defmethod apply-token :default [doc token]
(log/warn :apply-token/unknown-type {:token token})
(prn :apply-token/unknown-type {:token token})
doc)

;; blocks
Expand Down
2 changes: 1 addition & 1 deletion modules/view/deps.edn
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{:deps {nextjournal/cljs-extensions {:local/root "../cljs-extensions"}
nextjournal/log {:local/root "../log"}}}
io.github.nextjournal/log {:local/root "../log"}}}

0 comments on commit e06e36d

Please sign in to comment.