2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Turn Markdown link shortcuts into XHTML <a> tags.
|
|
|
|
*/
|
2015-08-03 10:47:49 +08:00
|
|
|
showdown.subParser('anchors', function (text, options, globals) {
|
2015-01-19 19:37:21 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('anchors.before', text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
var writeAnchorTag = function (wholeMatch, m1, m2, m3, m4, m5, m6, m7) {
|
|
|
|
if (showdown.helper.isUndefined(m7)) {
|
|
|
|
m7 = '';
|
|
|
|
}
|
|
|
|
wholeMatch = m1;
|
|
|
|
var linkText = m2,
|
|
|
|
linkId = m3.toLowerCase(),
|
|
|
|
url = m4,
|
|
|
|
title = m7;
|
|
|
|
|
|
|
|
if (!url) {
|
|
|
|
if (!linkId) {
|
|
|
|
// lower-case and turn embedded newlines into spaces
|
|
|
|
linkId = linkText.toLowerCase().replace(/ ?\n/g, ' ');
|
|
|
|
}
|
|
|
|
url = '#' + linkId;
|
|
|
|
|
|
|
|
if (!showdown.helper.isUndefined(globals.gUrls[linkId])) {
|
|
|
|
url = globals.gUrls[linkId];
|
|
|
|
if (!showdown.helper.isUndefined(globals.gTitles[linkId])) {
|
|
|
|
title = globals.gTitles[linkId];
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2015-01-19 19:37:21 +08:00
|
|
|
} else {
|
|
|
|
if (wholeMatch.search(/\(\s*\)$/m) > -1) {
|
|
|
|
// Special case for explicit empty url
|
|
|
|
url = '';
|
|
|
|
} else {
|
|
|
|
return wholeMatch;
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2015-01-19 19:37:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 22:57:43 +08:00
|
|
|
url = showdown.helper.escapeCharacters(url, '*_', false);
|
2015-01-19 19:37:21 +08:00
|
|
|
var result = '<a href="' + url + '"';
|
|
|
|
|
|
|
|
if (title !== '' && title !== null) {
|
|
|
|
title = title.replace(/"/g, '"');
|
2015-01-19 22:57:43 +08:00
|
|
|
title = showdown.helper.escapeCharacters(title, '*_', false);
|
2015-01-19 19:37:21 +08:00
|
|
|
result += ' title="' + title + '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
result += '>' + linkText + '</a>';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
// First, handle reference-style links: [link text] [id]
|
2015-08-03 10:47:49 +08:00
|
|
|
text = text.replace(/(\[((?:\[[^\]]*]|[^\[\]])*)][ ]?(?:\n[ ]*)?\[(.*?)])()()()()/g, writeAnchorTag);
|
2015-01-19 19:37:21 +08:00
|
|
|
|
|
|
|
// Next, inline-style links: [link text](url "optional title")
|
2015-08-03 10:47:49 +08:00
|
|
|
text = text.replace(/(\[((?:\[[^\]]*]|[^\[\]])*)]\([ \t]*()<?(.*?(?:\(.*?\).*?)?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,
|
2015-01-19 19:37:21 +08:00
|
|
|
writeAnchorTag);
|
|
|
|
|
|
|
|
// Last, handle reference-style shortcuts: [link text]
|
|
|
|
// These must come last in case you've also got [link test][1]
|
|
|
|
// or [link test](/foo)
|
2015-08-03 10:47:49 +08:00
|
|
|
text = text.replace(/(\[([^\[\]]+)])()()()()()/g, writeAnchorTag);
|
2015-01-19 19:37:21 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('anchors.after', text, options, globals);
|
2015-01-19 19:37:21 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|