Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for image references (issue #74) #95

Merged
merged 1 commit into from
Apr 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 22 additions & 5 deletions src/cljc/markdown/links.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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}]
Expand All @@ -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 "<img src=\"" src "\" alt=\"" alt title-text " />")))

(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])))))
2 changes: 2 additions & 0 deletions src/cljc/markdown/transformers.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
:refer [link
image
reference-link
image-reference-link
implicit-reference-link
footnote-link]]
[markdown.lists :refer [li]]
Expand Down Expand Up @@ -289,6 +290,7 @@
autoemail-transformer
autourl-transformer
image
image-reference-link
link
implicit-reference-link
reference-link
Expand Down
1 change: 1 addition & 0 deletions test/files/img_references.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>You can optionally use a space to separate the sets of brackets:</p><p>This is <img src="http://example.com/" alt="an example" title="Optional Title Here" /> reference-style image descriptor.</p><p>This is <img src="http://example.com/" alt="an example" title="Optional Title Here" /> reference-style image descriptor.</p><p>This is <img src="http://example.com/" alt="an example" title='Optional Title Here' /> reference-style image descriptor.</p><p>This is <img src="http://example.com/" alt="an/example" title='Optional Title Here' /> reference-style image descriptor.</p><p>This is <img src="http://example.com/" alt="an'example" title='Optional Title Here' /> reference-style image descriptor.</p><p>Then, anywhere in the document, you define your image descriptor label like this, on a line by itself:</p><p>This is <img src="http://example.com/" alt="an example" title=(Optional Title Here) /> reference-style image descriptor.</p><p>This is <img src="http://example.com/" alt="an example" /> reference-style image descriptor.</p><p>A image descriptor with <img src="http://example.com/#<i>a</i>b" alt="an underscore" />.</p>
30 changes: 30 additions & 0 deletions test/files/img_references.md
Original file line number Diff line number Diff line change
@@ -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

7 changes: 7 additions & 0 deletions test/markdown/md_file_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down