mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
78 lines
2.3 KiB
JavaScript
78 lines
2.3 KiB
JavaScript
/**
|
|
* Turn Markdown image shortcuts into <img> tags.
|
|
*/
|
|
showdown.subParser('images', function (text, options, globals) {
|
|
'use strict';
|
|
|
|
text = globals.converter._dispatch('images.before', text, options);
|
|
|
|
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;
|
|
|
|
function writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title) {
|
|
|
|
var gUrls = globals.gUrls,
|
|
gTitles = globals.gTitles,
|
|
gDims = globals.gDimensions;
|
|
|
|
linkId = linkId.toLowerCase();
|
|
|
|
if (!title) {
|
|
title = '';
|
|
}
|
|
|
|
if (url === '' || url === null) {
|
|
if (linkId === '' || linkId === null) {
|
|
// lower-case and turn embedded newlines into spaces
|
|
linkId = altText.toLowerCase().replace(/ ?\n/g, ' ');
|
|
}
|
|
url = '#' + linkId;
|
|
|
|
if (!showdown.helper.isUndefined(gUrls[linkId])) {
|
|
url = gUrls[linkId];
|
|
if (!showdown.helper.isUndefined(gTitles[linkId])) {
|
|
title = gTitles[linkId];
|
|
}
|
|
if (!showdown.helper.isUndefined(gDims[linkId])) {
|
|
width = gDims[linkId].width;
|
|
height = gDims[linkId].height;
|
|
}
|
|
} else {
|
|
return wholeMatch;
|
|
}
|
|
}
|
|
|
|
altText = altText.replace(/"/g, '"');
|
|
altText = showdown.helper.escapeCharacters(altText, '*_', false);
|
|
url = showdown.helper.escapeCharacters(url, '*_', false);
|
|
var result = '<img src="' + url + '" alt="' + altText + '"';
|
|
|
|
if (title) {
|
|
title = title.replace(/"/g, '"');
|
|
title = showdown.helper.escapeCharacters(title, '*_', false);
|
|
result += ' title="' + title + '"';
|
|
}
|
|
|
|
if (width && height) {
|
|
width = (width === '*') ? 'auto' : width;
|
|
height = (height === '*') ? 'auto' : height;
|
|
|
|
result += ' width="' + width + '"';
|
|
result += ' height="' + height + '"';
|
|
}
|
|
|
|
result += ' />';
|
|
|
|
return result;
|
|
}
|
|
|
|
// First, handle reference-style labeled images: ![alt text][id]
|
|
text = text.replace(referenceRegExp, writeImageTag);
|
|
|
|
// Next, handle inline images: ![alt text](url =<width>x<height> "optional title")
|
|
text = text.replace(inlineRegExp, writeImageTag);
|
|
|
|
text = globals.converter._dispatch('images.after', text, options);
|
|
return text;
|
|
});
|