From a1afc15484c8b5d6925661bf48bff794b6982f32 Mon Sep 17 00:00:00 2001 From: Yuri Zhykin Date: Sat, 2 Apr 2016 17:00:13 +0300 Subject: [PATCH] Added support for image references --- README.md | 11 +++++++++++ src/cljc/markdown/links.cljc | 27 +++++++++++++++++++++----- src/cljc/markdown/transformers.cljc | 2 ++ test/files/img_references.html | 1 + test/files/img_references.md | 30 +++++++++++++++++++++++++++++ test/markdown/md_file_test.clj | 7 +++++++ 6 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 test/files/img_references.html create mode 100644 test/files/img_references.md diff --git a/README.md b/README.md index d5d629d..21afd94 100644 --- a/README.md +++ b/README.md @@ -501,6 +501,17 @@ note: to enable footnotes, the `:footnotes?` option must be set to true. ![Alt text](/path/to/img.jpg "Optional Title") ``` +##### Image Reference + +``` +This is ![an example][id] reference-style image descriptor. + +[id]: http://example.com/ "Optional Title Here" +``` + +note: reference links require the `:reference-links?` option to be set to true + + ### Image Link ``` [![Continuous Integration status](https://secure.travis-ci.org/yogthos/markdown-clj.png)](http://travis-ci.org/yogthos/markdown-clj) diff --git a/src/cljc/markdown/links.cljc b/src/cljc/markdown/links.cljc index 31088b2..3ae2e89 100644 --- a/src/cljc/markdown/links.cljc +++ b/src/cljc/markdown/links.cljc @@ -133,11 +133,10 @@ (if (nil? match) [text state] (let [next-text (string/replace-first text matcher (partial replace-footnote-link footnotes)) - next-state - (-> state - (update-in [:footnotes :next-fn-id] inc) - (assoc-in [:footnotes :processed (get-in state [:footnotes :next-fn-id])] - (get-in state [:footnotes :unprocessed match])))] + next-state (-> state + (update-in [:footnotes :next-fn-id] inc) + (assoc-in [:footnotes :processed (get-in state [:footnotes :next-fn-id])] + (get-in state [:footnotes :unprocessed match])))] (recur next-text next-state))))) (defn footnote-link [text {:keys [code codeblock footnotes] :as state}] @@ -151,3 +150,21 @@ :else (let [[text state] (replace-all-footnote-links text state)] [text state]))) + +(defn make-image-reference [src alt title] + (let [title-text (str (if title (str "\" title=" (string/join title) "") "\""))] + (str "\"""))) + +(defn image-reference-link [text {:keys [references] :as state}] + (if (or (not (:reference-links? state)) (empty? references)) + [text state] + (let [matcher #"!\[([^\]]+)\]\s*(\[[a-zA-Z0-9 ]+\])" + matches (distinct (re-seq matcher text))] + (loop [ms matches + new-text text] + (if (seq ms) + (let [[m alt ref] (first ms) + refval (get references ref) + im (make-image-reference (first refval) alt (second refval))] + (recur (rest ms) (string/replace new-text m im))) + [new-text state]))))) diff --git a/src/cljc/markdown/transformers.cljc b/src/cljc/markdown/transformers.cljc index 5d13a86..0372f4f 100644 --- a/src/cljc/markdown/transformers.cljc +++ b/src/cljc/markdown/transformers.cljc @@ -4,6 +4,7 @@ :refer [link image reference-link + image-reference-link implicit-reference-link footnote-link]] [markdown.lists :refer [li]] @@ -289,6 +290,7 @@ autoemail-transformer autourl-transformer image + image-reference-link link implicit-reference-link reference-link diff --git a/test/files/img_references.html b/test/files/img_references.html new file mode 100644 index 0000000..23a393a --- /dev/null +++ b/test/files/img_references.html @@ -0,0 +1 @@ +

You can optionally use a space to separate the sets of brackets:

This is an example reference-style image descriptor.

This is an example reference-style image descriptor.

This is an example reference-style image descriptor.

This is an/example reference-style image descriptor.

This is an'example reference-style image descriptor.

Then, anywhere in the document, you define your image descriptor label like this, on a line by itself:

This is an example reference-style image descriptor.

This is an example reference-style image descriptor.

A image descriptor with an underscore.

diff --git a/test/files/img_references.md b/test/files/img_references.md new file mode 100644 index 0000000..0f8af88 --- /dev/null +++ b/test/files/img_references.md @@ -0,0 +1,30 @@ +[id]: http://example.com/ "Optional Title Here" + +You can optionally use a space to separate the sets of brackets: + +This is ![an example] [id] reference-style image descriptor. + +This is ![an example][id] reference-style image descriptor. + +This is ![an example][id1] reference-style image descriptor. + +This is ![an/example][id1] reference-style image descriptor. + +This is ![an'example][id1] reference-style image descriptor. + +Then, anywhere in the document, you define your image descriptor label like this, on a line by itself: + +This is ![an example][id2] reference-style image descriptor. + +This is ![an example][id3] reference-style image descriptor. + +A image descriptor with ![an underscore][underscore]. + +[id1]: http://example.com/ 'Optional Title Here' + +[id2]: http://example.com/ (Optional Title Here) + +[id3]: http://example.com/ + +[underscore]: http://example.com/#_a_b + diff --git a/test/markdown/md_file_test.clj b/test/markdown/md_file_test.clj index 4c15c46..38ae88e 100644 --- a/test/markdown/md_file_test.clj +++ b/test/markdown/md_file_test.clj @@ -9,6 +9,13 @@ (markdown/md-to-html (str "test/files" java.io.File/separator "references.md") wrt :reference-links? true) (is (= (slurp (str "test/files" java.io.File/separator "references.html")) (.toString wrt))))) +(deftest img-references + (let [wrt (java.io.StringWriter.)] + (markdown/md-to-html (str "test/files" java.io.File/separator "img_references.md") wrt :reference-links? true) + (is (= (clojure.string/trim-newline + (slurp (str "test/files" java.io.File/separator "img_references.html"))) + (.toString wrt))))) + (deftest footnotes (let [wrt (java.io.StringWriter.)] (markdown/md-to-html (str "test/files" java.io.File/separator "footnotes.md") wrt :footnotes? true)