Skip to content

Commit

Permalink
feat(images): add support for image's implicit reference syntax
Browse files Browse the repository at this point in the history
Closes #366
  • Loading branch information
tivie committed Mar 30, 2017
1 parent f1eab2a commit 0c6c07b
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 12 deletions.
11 changes: 8 additions & 3 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/subParsers/images.js
Expand Up @@ -6,8 +6,9 @@ showdown.subParser('images', function (text, options, globals) {

text = globals.converter._dispatch('images.before', text, options, globals);

var inlineRegExp = /!\[(.*?)]\s?\([ \t]*()<?(\S+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(['"])(.*?)\6[ \t]*)?\)/g,
referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[(.*?)]()()()()()/g;
var inlineRegExp = /!\[(.*?)]\s?\([ \t]*()<?(\S+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(['"])(.*?)\6[ \t]*)?\)/g,
referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[(.*?)]()()()()()/g,
refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g;

function writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title) {

Expand Down Expand Up @@ -77,6 +78,9 @@ showdown.subParser('images', function (text, options, globals) {
// Next, handle inline images: ![alt text](url =<width>x<height> "optional title")
text = text.replace(inlineRegExp, writeImageTag);

// handle reference-style shortcuts: |[img text]
text = text.replace(refShortcutRegExp, writeImageTag);

text = globals.converter._dispatch('images.after', text, options, globals);
return text;
});
5 changes: 4 additions & 1 deletion test/cases/images.html
@@ -1,3 +1,6 @@
<p><img src="/path/to/img.jpg" alt="Alt text" /></p>
<p><img src="/path/to/img.jpg" alt="Alt text" title="Optional title" /></p>
<p><img src="url/to/image" alt="Alt text" title="Optional title attribute" /></p>
<p><img src="url/to/image.jpg" alt="Alt text" title="Optional title attribute" /></p>
<p><img src="url/to/image2.jpg" alt="My Image" title="Optional title attribute" /></p>
<p>![leave me alone]</p>
<p>![leave me alone][]</p>
9 changes: 8 additions & 1 deletion test/cases/images.md
Expand Up @@ -5,4 +5,11 @@

![Alt text][id]

[id]: url/to/image "Optional title attribute"
![My Image]

![leave me alone]

![leave me alone][]

[id]: url/to/image.jpg "Optional title attribute"
[My Image]: url/to/image2.jpg "Optional title attribute"
1 change: 1 addition & 0 deletions test/cases/implicit-anchors.html
@@ -1 +1,2 @@
<p>Search the web at <a href="http://google.com/">Google</a> or <a href="http://daringfireball.net/">Daring Fireball</a>.</p>
<p>Search the web at <a href="http://google.com/">Google</a> or <a href="http://daringfireball.net/">Daring Fireball</a>.</p>
3 changes: 3 additions & 0 deletions test/cases/implicit-anchors.md
@@ -1,5 +1,8 @@

Search the web at [Google][] or [Daring Fireball][].

Search the web at [Google] or [Daring Fireball].


[Google]: http://google.com/
[Daring Fireball]: http://daringfireball.net/

0 comments on commit 0c6c07b

Please sign in to comment.