From 16b97a88204568a9102bcf9f24b9a9538118b4d8 Mon Sep 17 00:00:00 2001 From: Andrea Amantini Date: Tue, 24 Sep 2024 12:53:30 +0200 Subject: [PATCH] Reimplement table cell alignment --- src/nextjournal/markdown/impl.clj | 19 ++++++++++++++++--- src/nextjournal/markdown/impl.cljs | 1 - test/nextjournal/markdown_test.cljc | 21 ++++++++++++++++++++- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/nextjournal/markdown/impl.clj b/src/nextjournal/markdown/impl.clj index c84edfc..670d51a 100644 --- a/src/nextjournal/markdown/impl.clj +++ b/src/nextjournal/markdown/impl.clj @@ -7,7 +7,7 @@ (:import (org.commonmark.ext.autolink AutolinkExtension) (org.commonmark.ext.footnotes FootnotesExtension FootnoteReference FootnoteDefinition InlineFootnote) (org.commonmark.ext.gfm.strikethrough Strikethrough StrikethroughExtension) - (org.commonmark.ext.gfm.tables TableBlock TableBody TableRow TableHead TableCell TablesExtension) + (org.commonmark.ext.gfm.tables TableBlock TableBody TableRow TableHead TableCell TablesExtension TableCell$Alignment) (org.commonmark.ext.task.list.items TaskListItemsExtension TaskListItemMarker) (org.commonmark.node Node AbstractVisitor Document @@ -158,9 +158,22 @@ (u/update-current-loc ctx (fn [loc] (u/zopen-node loc {:type :table-body})))) (defmethod open-node TableRow [ctx ^TableRow _node] (u/update-current-loc ctx (fn [loc] (u/zopen-node loc {:type :table-row})))) + +(defn alignment->keyword [enum] + (condp = enum + TableCell$Alignment/LEFT :left + TableCell$Alignment/CENTER :center + TableCell$Alignment/RIGHT :right)) + (defmethod open-node TableCell [ctx ^TableCell node] - (u/update-current-loc ctx (fn [loc] (u/zopen-node loc {:type (if (.isHeader node) :table-header :table-data) - :content []})))) + (u/update-current-loc ctx (fn [loc] + (let [alignment (some-> (.getAlignment node) alignment->keyword)] + (u/zopen-node loc (cond-> {:type (if (.isHeader node) :table-header :table-data) + :content []} + alignment + (assoc :alignment alignment + ;; TODO: drop/deprecate this, compute in transform + :attrs {:style (str "text-align:" (name alignment))}))))))) (defmethod open-node FootnoteDefinition [ctx ^FootnoteDefinition node] (-> ctx diff --git a/src/nextjournal/markdown/impl.cljs b/src/nextjournal/markdown/impl.cljs index 8aa1513..9ddf5a6 100644 --- a/src/nextjournal/markdown/impl.cljs +++ b/src/nextjournal/markdown/impl.cljs @@ -120,7 +120,6 @@ (defn footnote-label [{:as _ctx ::keys [footnote-offset]} token] ;; TODO: consider initial offset in case we're parsing multiple inputs - (prn :label/offset footnote-offset) (or (j/get-in token [:meta :label]) ;; inline labels won't have a label (str "inline-note-" (+ footnote-offset (j/get-in token [:meta :id]))))) diff --git a/test/nextjournal/markdown_test.cljc b/test/nextjournal/markdown_test.cljc index 1e984e0..ff9ceb8 100644 --- a/test/nextjournal/markdown_test.cljc +++ b/test/nextjournal/markdown_test.cljc @@ -233,6 +233,25 @@ $$\\int_a^bf(t)dt$$ | | 4 | ")))) +(deftest table-alignment + (is (match? [:div [:table + [:thead + [:tr + [:th {:style {:text-align "left"}} "x"] + [:th {:style {:text-align "center"}} "y"] + [:th {:style {:text-align "right"}} "z"]]] + [:tbody + [:tr + [:td {:style {:text-align "left"}} "1"] + [:td {:style {:text-align "center"}} "2"] + [:td {:style {:text-align "right"}} "3"]]]]] + (md/->hiccup " +| x | y | z | +|:--|:--:|--:| +| 1 | 2 | 3 | +")))) + + (deftest hard-breaks (is (= [:div [:p "Please don't inter" [:br] "rupt me when I'm writing."]] (md/->hiccup "Please don't inter \nrupt me when I'm writing.") @@ -991,7 +1010,7 @@ back to text") :content second))) (comment (clojure.test/run-test-var #'formulas) - + (shadow.cljs.devtools.api/repl :browser-test) (doseq [[n v] (ns-publics *ns*)] (ns-unmap *ns* n)) (clojure.test/run-tests) (run-tests 'nextjournal.markdown-test)