Skip to content

Commit

Permalink
Add an API function for parsing metadata by itself, without rendering…
Browse files Browse the repository at this point in the history
… the output HTML. (#162)
  • Loading branch information
mschaef committed Apr 11, 2020
1 parent 30565ea commit 709da99
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/clj/markdown/core.clj
Expand Up @@ -115,6 +115,13 @@
html (.toString output)]
{:metadata metadata :html html})))

(defn md-to-meta
"converts a markdown formatted string to a metadata map"
[text]
(when text
(let [input (new StringReader text)]
(parse-metadata input))))

(defn md-to-html-string [text & params]
(:html (md-to-html-string* text params)))

Expand Down
14 changes: 14 additions & 0 deletions test/markdown/md_file_test.clj
Expand Up @@ -44,6 +44,20 @@
:base_url ["http://example.com"]}
metadata)))))

(deftest md-metadata-only
(testing "Finds all metadata, without parsing the rest of the file."
(let [md (slurp (str "test/files/metadata.md"))
metadata (markdown/md-to-meta md)]
(is (= {:title ["My Document"]
:summary ["A brief description of my document."]
:authors ["Justin May"
"Spooky Mulder"
"End Line At End\n"]
:date ["October 31, 2015"]
:blank-value []
:base_url ["http://example.com"]}
metadata)))))

(deftest md-yaml-metadata
(testing "Finds all yaml metadata and correctly parses rest of file."
(let [md (slurp (str "test/files/metadata-yaml.md"))
Expand Down

0 comments on commit 709da99

Please sign in to comment.