showdown/src/subParsers/images.js

100 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-01-16 05:21:33 +08:00
/**
* Turn Markdown image shortcuts into <img> tags.
*/
showdown.subParser('images', function (text, options, globals) {
2015-01-19 19:37:21 +08:00
'use strict';
2015-01-16 05:21:33 +08:00
2015-01-19 19:37:21 +08:00
var writeImageTag = function (wholeMatch, m1, m2, m3, m4, m5, m6, m7) {
2015-01-16 05:21:33 +08:00
2015-01-19 19:37:21 +08:00
wholeMatch = m1;
var altText = m2,
linkId = m3.toLowerCase(),
url = m4,
title = m7,
gUrls = globals.gUrls,
gTitles = globals.gTitles;
2015-01-16 05:21:33 +08:00
2015-01-19 19:37:21 +08:00
if (!title) {
title = '';
}
2015-01-16 05:21:33 +08:00
2015-01-19 19:37:21 +08:00
if (url === '' || url === null) {
if (linkId === '' || linkId === null) {
// lower-case and turn embedded newlines into spaces
linkId = altText.toLowerCase().replace(/ ?\n/g, ' ');
}
url = '#' + linkId;
2015-01-16 05:21:33 +08:00
2015-01-19 19:37:21 +08:00
if (typeof gUrls[linkId] !== 'undefined') {
url = gUrls[linkId];
if (typeof gTitles[linkId] !== 'undefined') {
title = gTitles[linkId];
}
} else {
return wholeMatch;
}
}
altText = altText.replace(/"/g, '&quot;');
2015-01-19 22:57:43 +08:00
url = showdown.helper.escapeCharacters(url, '*_', false);
2015-01-19 19:37:21 +08:00
var result = '<img src="' + url + '" alt="' + altText + '"';
if (title) {
title = title.replace(/"/g, '&quot;');
title = showdown.helper.escapeCharacters(title, '*_', false);
result += ' title="' + title + '"';
}
2015-01-19 19:37:21 +08:00
result += ' />';
return result;
};
// First, handle reference-style labeled images: ![alt text][id]
/*
text = text.replace(/
( // wrap whole match in $1
!\[
(.*?) // alt text = $2
\]
[ ]? // one optional space
(?:\n[ ]*)? // one optional newline followed by spaces
\[
(.*?) // id = $3
\]
)()()()() // pad rest of backreferences
/g,writeImageTag);
*/
text = text.replace(/(!\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeImageTag);
// Next, handle inline images: ![alt text](url "optional title")
// Don't forget: encode * and _
/*
text = text.replace(/
( // wrap whole match in $1
!\[
(.*?) // alt text = $2
\]
\s? // One optional whitespace character
\( // literal paren
[ \t]*
() // no id, so leave $3 empty
<?(\S+?)>? // src url = $4
[ \t]*
( // $5
(['"]) // quote char = $6
(.*?) // title = $7
\6 // matching quote
[ \t]*
)? // title is optional
\)
)
/g,writeImageTag);
*/
text = text.replace(/(!\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, writeImageTag);
return text;
2015-01-16 05:21:33 +08:00
});