feat(images): add support for image's implicit reference syntax

Closes #366
This commit is contained in:
Estevao Soares dos Santos 2017-03-30 23:35:28 +01:00
parent f1eab2a7f2
commit 0c6c07b341
9 changed files with 22 additions and 4 deletions

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

View File

@ -6,8 +6,9 @@ showdown.subParser('images', function (text, options, globals) {
text = globals.converter._dispatch('images.before', 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, 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; referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[(.*?)]()()()()()/g,
refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g;
function writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title) { function writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title) {
@ -77,6 +78,9 @@ showdown.subParser('images', function (text, options, globals) {
// Next, handle inline images: ![alt text](url =<width>x<height> "optional title") // Next, handle inline images: ![alt text](url =<width>x<height> "optional title")
text = text.replace(inlineRegExp, writeImageTag); 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); text = globals.converter._dispatch('images.after', text, options, globals);
return text; return text;
}); });

View File

@ -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" /></p>
<p><img src="/path/to/img.jpg" alt="Alt text" title="Optional title" /></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>

View File

@ -5,4 +5,11 @@
![Alt text][id] ![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"

View File

@ -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>
<p>Search the web at <a href="http://google.com/">Google</a> or <a href="http://daringfireball.net/">Daring Fireball</a>.</p>

View File

@ -1,5 +1,8 @@
Search the web at [Google][] or [Daring Fireball][]. Search the web at [Google][] or [Daring Fireball][].
Search the web at [Google] or [Daring Fireball].
[Google]: http://google.com/ [Google]: http://google.com/
[Daring Fireball]: http://daringfireball.net/ [Daring Fireball]: http://daringfireball.net/