Skip to content

Commit

Permalink
Reimplement table cell alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino committed Sep 24, 2024
1 parent 459a73b commit 16b97a8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/nextjournal/markdown/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/nextjournal/markdown/impl.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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])))))
Expand Down
21 changes: 20 additions & 1 deletion test/nextjournal/markdown_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 16b97a8

Please sign in to comment.