mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
137 lines
3.5 KiB
JavaScript
137 lines
3.5 KiB
JavaScript
|
/**
|
||
|
* Created by Estevao on 11-01-2015.
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* Turn Markdown link shortcuts into XHTML <a> tags.
|
||
|
*/
|
||
|
showdown.subParser('anchors', function (text, config, globals) {
|
||
|
'use strict';
|
||
|
|
||
|
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, '*_');
|
||
|
var result = '<a href="' + url + '"';
|
||
|
|
||
|
if (title !== '' && title !== null) {
|
||
|
title = title.replace(/"/g, '"');
|
||
|
title = showdown.helper.escapeCharacters(title, '*_');
|
||
|
result += ' title="' + title + '"';
|
||
|
}
|
||
|
|
||
|
result += '>' + linkText + '</a>';
|
||
|
|
||
|
return result;
|
||
|
};
|
||
|
|
||
|
// First, handle reference-style links: [link text] [id]
|
||
|
/*
|
||
|
text = text.replace(/
|
||
|
( // wrap whole match in $1
|
||
|
\[
|
||
|
(
|
||
|
(?:
|
||
|
\[[^\]]*\] // allow brackets nested one level
|
||
|
|
|
||
|
[^\[] // or anything else
|
||
|
)*
|
||
|
)
|
||
|
\]
|
||
|
|
||
|
[ ]? // one optional space
|
||
|
(?:\n[ ]*)? // one optional newline followed by spaces
|
||
|
|
||
|
\[
|
||
|
(.*?) // id = $3
|
||
|
\]
|
||
|
)()()()() // pad remaining backreferences
|
||
|
/g,_DoAnchors_callback);
|
||
|
*/
|
||
|
text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeAnchorTag);
|
||
|
|
||
|
//
|
||
|
// Next, inline-style links: [link text](url "optional title")
|
||
|
//
|
||
|
|
||
|
/*
|
||
|
text = text.replace(/
|
||
|
( // wrap whole match in $1
|
||
|
\[
|
||
|
(
|
||
|
(?:
|
||
|
\[[^\]]*\] // allow brackets nested one level
|
||
|
|
|
||
|
[^\[\]] // or anything else
|
||
|
)
|
||
|
)
|
||
|
\]
|
||
|
\( // literal paren
|
||
|
[ \t]*
|
||
|
() // no id, so leave $3 empty
|
||
|
<?(.*?)>? // href = $4
|
||
|
[ \t]*
|
||
|
( // $5
|
||
|
(['"]) // quote char = $6
|
||
|
(.*?) // Title = $7
|
||
|
\6 // matching quote
|
||
|
[ \t]* // ignore any spaces/tabs between closing quote and )
|
||
|
)? // title is optional
|
||
|
\)
|
||
|
)
|
||
|
/g,writeAnchorTag);
|
||
|
*/
|
||
|
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(/
|
||
|
( // wrap whole match in $1
|
||
|
\[
|
||
|
([^\[\]]+) // link text = $2; can't contain '[' or ']'
|
||
|
\]
|
||
|
)()()()()() // pad rest of backreferences
|
||
|
/g, writeAnchorTag);
|
||
|
*/
|
||
|
text = text.replace(/(\[([^\[\]]+)\])()()()()()/g, writeAnchorTag);
|
||
|
|
||
|
return text;
|
||
|
|
||
|
|
||
|
});
|