I'm parsing some Hiccup in CLJS, with the goal of taking :h2
and :h3
elements and converting them to a tree of nested :ul
and :li
.
My starting point is a flat vector like:
[[:h2 {} "Foo"] [:h2 {} "Bar"] [:h3 {} "Child1"] [:h2 {} "Baz"]]
If I just map over these and replace (first el)
with [:li]
, I have a flat list. But I'd like to get something like:
[[:li "Foo"] [:li "Bar"] [:ul [:li "Child1"]] [:li "Baz"]]
If I call (partition-by #(= :h2 (first %)) my-vec)
, I get something almost useful:
(([:h2 {} "Foo"] [:h2 {} "Bar"]) ([:h3 {} "Child1"]) ([:h2 {} "Baz"]))
The partition happens when the predicate #(= :h2 (first %))
changes, (which is what the documentation says it does).
How can I get the behavior I'm looking for?
Here's an answer that does the job, but is horribly inelegant, since it essentially mutates the last element in the
reduce
call when necessary:Produces: