nextjournal / markdown

A cross-platform clojure/script parser for Markdown
ISC License
46 stars 6 forks source link

empty cells in tables are skipped #12

Closed daslu closed 1 year ago

daslu commented 1 year ago

Thanks for this beautiful and handy library.

The following example shows that empty cells of tables are skipped, thus resulting in misplacement of values from other cells at the resulting HTML.

(nextjournal.markdown/->hiccup
"
| :x | :y | :z |
|---:|---:|---:|
|  1 |  2 |  3 |
|    |  4 |  5 |
")

;; =>

[:div
 [:table
  [:thead
   [:tr
    [:th {:style {:text-align "right"}} ":x"]
    [:th {:style {:text-align "right"}} ":y"]
    [:th {:style {:text-align "right"}} ":z"]]]
  [:tbody
   [:tr
    [:td {:style {:text-align "right"}} "1"]
    [:td {:style {:text-align "right"}} "2"]
    [:td {:style {:text-align "right"}} "3"]]
   [:tr
    [:td {:style {:text-align "right"}} "4"]
    [:td {:style {:text-align "right"}} "5"]]]]]

The parsing stage works fine:

(nextjournal.markdown/parse
 "
| :x | :y | :z |
|---:|---:|---:|
|  1 |  2 |  3 |
|    |  4 |  5 |

;; => 

{:type :doc,
 :content
 [{:type :table,
   :content
   [{:type :table-head,
     :content
     [{:type :table-row,
       :content
       [{:type :table-header,
         :content [{:type :text, :text ":x"}],
         :attrs {:style "text-align:right"}}
        {:type :table-header,
         :content [{:type :text, :text ":y"}],
         :attrs {:style "text-align:right"}}
        {:type :table-header,
         :content [{:type :text, :text ":z"}],
         :attrs {:style "text-align:right"}}]}]}
    {:type :table-body,
     :content
     [{:type :table-row,
       :content
       [{:type :table-data,
         :content [{:type :text, :text "1"}],
         :attrs {:style "text-align:right"}}
        {:type :table-data,
         :content [{:type :text, :text "2"}],
         :attrs {:style "text-align:right"}}
        {:type :table-data,
         :content [{:type :text, :text "3"}],
         :attrs {:style "text-align:right"}}]}
      {:type :table-row,
       :content
       [{:type :table-data, :content [], :attrs {:style "text-align:right"}}
        {:type :table-data,
         :content [{:type :text, :text "4"}],
         :attrs {:style "text-align:right"}}
        {:type :table-data,
         :content [{:type :text, :text "5"}],
         :attrs {:style "text-align:right"}}]}]}]}],
 :toc {:type :toc}}
")

But on conversion to Hiccup, the resulting :table-data element with empty :content is skipped, probably due to the following condition in the code: https://github.com/nextjournal/markdown/blob/a84ab81bcd586b4360ba6b74a177aed9a9569499/src/nextjournal/markdown/transform.cljc#L31

zampino commented 1 year ago

Thanks for the report @daslu, I'll look into it.

zampino commented 1 year ago

probably due to the following condition

And thank you for spotting the cause of the issue.

daslu commented 1 year ago

Thanks for the super-quick help!