Skip to content

Commit

Permalink
Todo lists
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino committed Apr 3, 2024
1 parent 591f4ce commit f42219a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
5 changes: 4 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
:deps {applied-science/js-interop {:mvn/version "0.3.3"}
org.clojure/data.json {:mvn/version "2.4.0"}
org.graalvm.js/js {:mvn/version "21.3.2.1"}
org.commonmark/commonmark {:mvn/version "0.22.0"}}

;; commonmark
org.commonmark/commonmark {:mvn/version "0.22.0"}
org.commonmark/commonmark-ext-task-list-items {:mvn/version "0.22.0"}}

:aliases
{:nextjournal/clerk
Expand Down
52 changes: 38 additions & 14 deletions src/nextjournal/markdown/parser2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
(:require [clojure.zip :as z]
[nextjournal.markdown.parser :as parser])
(:import (org.commonmark.parser Parser)
(org.commonmark.ext.task.list.items TaskListItemsExtension
TaskListItemMarker)
(org.commonmark.node Node AbstractVisitor
;;;;;;;;;; node types ;;;;;;;;;;;;;;;;;;
Document
Expand All @@ -18,19 +20,22 @@

ListItem
Link
ThematicBreak
SoftLineBreak
HardLineBreak
Image)))

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

(def ^Parser parser (.. Parser builder build))
(def ^Parser parser (.. Parser
builder
(extensions [(TaskListItemsExtension/create)])
build))

(defmulti open-node (fn [_ctx node] (type node)))
(defmulti close-node (fn [_ctx node] (type node)))
(defmethod open-node :default [loc node] (prn :unknown-node node) loc)
(defmethod close-node :default [loc _node]
(or (z/up loc) loc))

(defmethod close-node :default [loc _node] (z/up loc))

(defmethod open-node Document [loc _node] loc)
(defmethod close-node Document [loc _node] loc)
Expand Down Expand Up @@ -66,32 +71,51 @@
:attrs {:src (.getDestination node) :title (.getTitle node)}
:content []}) z/down z/rightmost))

(defn insert-text [loc ^Text node]
(z/append-child loc {:type :text :text (.getLiteral node)}))
(defn handle-todo-list [loc ^TaskListItemMarker node]
(prn :todo node)
(-> loc
(z/edit assoc :type :todo-item :attrs {:checked (.isChecked node)})
z/up (z/edit assoc :type :todo-list)
z/down z/rightmost))

(defn node->data [^Node node]
(let [!loc (atom (parser/->zip {:type :doc :content []}))]
(.accept node
(proxy [AbstractVisitor] []
(visit [^Node node]
(prn :visit (str node) (z/node @!loc))
#_ (prn :visit (str node) (z/node @!loc))
(assert @!loc (format "Can't add node: '%s' to an empty location" node))
(condp instance? node
Text (swap! !loc insert-text node)
(do
(swap! !loc open-node node)
(.visitChildren this node)
(swap! !loc close-node node))))))
Text (swap! !loc z/append-child {:type :text :text (.getLiteral ^Text node)})
ThematicBreak (swap! !loc z/append-child {:type :ruler})
SoftLineBreak (swap! !loc z/append-child {:type :softbreak})
HardLineBreak (swap! !loc z/append-child {:type :softbreak})
TaskListItemMarker (swap! !loc handle-todo-list node)
(if (get-method open-node (class node))
(do
(swap! !loc open-node node)
(.visitChildren this node)
(swap! !loc close-node node))
(prn :open-node/not-implemented node))))))

(some-> @!loc z/root)))

(defn parse [md]
(node->data (.parse parser md)))

(comment
(parse "# Ahoi
(do
(parse "# Ahoi
par
broken
* a **strong** item
* what [a nice link](/to/some 'with a title')
* just _emphatic_ two
![img](/some/src 'title')"))
- [ ] one
- [x] two
---
![img](/some/src 'title')")
nil))

0 comments on commit f42219a

Please sign in to comment.