Skip to content

Commit 5546421

Browse files
authored
Enable usage of keyword arguments from JS (#198)
In javascript, there are no keywords, so we simply convert string to keywords first. This feature is added to the clojure version as well, mostly because I don't know how to run a make a test only for the JS version. Fixes #197
1 parent 7a45199 commit 5546421

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ a string followed by the options as its input, and returns the resulting HTML st
268268

269269
```javascript
270270
console.log(markdown.core.mdToHtml("##This is a heading\nwith a paragraph following it"));
271+
272+
// With keyword arguments
273+
console.log(markdown.core.mdToHtml("##This is a heading\nwith a paragraph following it", "heading-anchors", true));
274+
271275
```
272276

273277
## Supported syntax

src/cljs/markdown/core.cljs

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
[text params]
4545
(binding [markdown.common/*substring* (fn [s n] (apply str (drop n s)))
4646
markdown.transformers/*formatter* format]
47-
(let [params (when params (apply (partial assoc {}) params))
47+
(let [params (some->> params
48+
(partition 2)
49+
(reduce (fn [m [k v]] (assoc m (keyword k) v)) {}))
4850
lines (.split (str text "\n") "\n")
4951
html (goog.string.StringBuffer. "")
5052
references (when (:reference-links? params) (parse-references lines))

test/markdown/md_test.cljc

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
(is (=
3131
"<h3 id=\"foo_bar_baz\">foo bar BAz</h3><p>some text</p>"
3232
(entry-function "###foo bar BAz\nsome text" :heading-anchors true)))
33-
(is (=
34-
"<h3 id=\"foo_bar_baz\">foo bar BAz</h3><p>some text</p>"
35-
(entry-function "###foo bar BAz##\nsome text" :heading-anchors true))))
33+
#?(:cljs
34+
; Testing that keywords args can be passed as strings, for javascript compatibility
35+
(is (= "<h3 id=\"foo_bar_baz\">foo bar BAz</h3><p>some text</p>"
36+
(entry-function "###foo bar BAz##\nsome text" "heading-anchors" true)))))
3637

3738
(deftest br
3839
(is (= "<p>foo<br /></p>" (entry-function "foo "))))

0 commit comments

Comments
 (0)