mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
70 lines
2.1 KiB
JavaScript
70 lines
2.1 KiB
JavaScript
/**
|
|
* Turn Markdown link shortcuts into XHTML <a> tags.
|
|
*/
|
|
showdown.subParser('anchors', function (text, options, globals) {
|
|
'use strict';
|
|
|
|
text = globals.converter._dispatch('anchors.before', text, options, globals);
|
|
|
|
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];
|
|
}
|
|
} else {
|
|
if (wholeMatch.search(/\(\s*\)$/m) > -1) {
|
|
// Special case for explicit empty url
|
|
url = '';
|
|
} else {
|
|
return wholeMatch;
|
|
}
|
|
}
|
|
}
|
|
|
|
url = showdown.helper.escapeCharacters(url, '*_', false);
|
|
var result = '<a href="' + url + '"';
|
|
|
|
if (title !== '' && title !== null) {
|
|
title = title.replace(/"/g, '"');
|
|
title = showdown.helper.escapeCharacters(title, '*_', false);
|
|
result += ' title="' + title + '"';
|
|
}
|
|
|
|
result += '>' + linkText + '</a>';
|
|
|
|
return result;
|
|
};
|
|
|
|
// First, handle reference-style links: [link text] [id]
|
|
text = text.replace(/(\[((?:\[[^\]]*]|[^\[\]])*)][ ]?(?:\n[ ]*)?\[(.*?)])()()()()/g, writeAnchorTag);
|
|
|
|
// Next, inline-style links: [link text](url "optional title")
|
|
text = text.replace(/(\[((?:\[[^\]]*]|[^\[\]])*)]\([ \t]*()<?(.*?(?:\(.*?\).*?)?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,
|
|
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)
|
|
text = text.replace(/(\[([^\[\]]+)])()()()()()/g, writeAnchorTag);
|
|
|
|
text = globals.converter._dispatch('anchors.after', text, options, globals);
|
|
return text;
|
|
});
|