summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsko Nõmm <asko@nmm.ee>2026-02-10 23:53:56 +0000
committerAsko Nõmm <asko@nmm.ee>2026-02-10 23:53:56 +0000
commit92e81516d217586e961228a0380fc8bfae3ea378 (patch)
treec1a1d97710f896d519ed5679353c4955251604a5
parentd23410f547608deae53ed8e028b46f8572552ef3 (diff)
parentba9005587b045d417b460f59145032f569c95cba (diff)
Merge pull request 'Allow docstrings in `defhtml` (closes #2)' (#5) from allow-docstrings-in-defhtml into main
Reviewed-on: https://git.nmm.ee/asko/dompa/pulls/5
-rw-r--r--src/dompa/nodes.cljc13
-rw-r--r--test/dompa/nodes_test.cljc18
2 files changed, 28 insertions, 3 deletions
diff --git a/src/dompa/nodes.cljc b/src/dompa/nodes.cljc
index 58ff045..7e8f769 100644
--- a/src/dompa/nodes.cljc
+++ b/src/dompa/nodes.cljc
@@ -95,10 +95,14 @@
(defmacro defhtml
"Creates a new function with `name` that outputs HTML.
+ Optionally accepts a docstring between the name and the argument vector.
+
Example usage:
```clojure
- (defhtml about-page [who]
+ (defhtml about-page
+ \"Renders the about page for the given person.\"
+ [who]
($ :div
($ \"hello \" who)))
@@ -106,8 +110,11 @@
```
"
[name & args-and-elements]
- (let [[args & elements] args-and-elements]
- `(defn ~name ~args
+ (let [[maybe-doc & rest] args-and-elements
+ [docstring args elements] (if (string? maybe-doc)
+ [maybe-doc (first rest) (next rest)]
+ [nil maybe-doc rest])]
+ `(defn ~name ~@(when docstring [docstring]) ~args
(->html (vector ~@elements)))))
(defn- list-of-one?
diff --git a/test/dompa/nodes_test.cljc b/test/dompa/nodes_test.cljc
index 0340abb..e7c6616 100644
--- a/test/dompa/nodes_test.cljc
+++ b/test/dompa/nodes_test.cljc
@@ -16,10 +16,28 @@
($ :div
($ "hello " who)))
+(defhtml hello-with-doc
+ "Greets the given person with a div."
+ [who]
+ ($ :div
+ ($ "hello " who)))
+
(deftest defhtml-test
(is (= "<div>hello world</div>"
(hello "world"))))
+(deftest defhtml-docstring-test
+ (testing "defhtml with docstring produces correct HTML"
+ (is (= "<div>hello world</div>"
+ (hello-with-doc "world"))))
+ #?(:clj
+ (testing "docstring is attached to the var metadata"
+ (is (= "Greets the given person with a div."
+ (:doc (meta #'hello-with-doc))))))
+ #?(:clj
+ (testing "defhtml without docstring has no :doc metadata"
+ (is (nil? (:doc (meta #'hello)))))))
+
(defhtml list-items [items]
($ :ul
(->> items