Skip to content

Commit

Permalink
后台预览,自动识别链接
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqi committed Oct 12, 2014
1 parent 04b1ef8 commit 1e69fdb
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions admin/js/markdown-extra-extended.js
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,44 @@ Markdown_Parser.prototype.encodeAmpsAndAngles = function(text) {

Markdown_Parser.prototype.doAutoLinks = function(text) {
var self = this;
text = text.replace(/(="|<)?\b(https?|ftp)(:\/\/[-A-Z0-9+&@#\/%?=~_|\[\]\(\)!:,\.;]*[-A-Z0-9+&@#\/%=~_|\[\])])(?=$|\W)/i, function (match, lookbehind, protocol, link) {
if (lookbehind) {
return match;
}

if (link[link.length - 1] != ')') {
return '<' + protocol + link + '>';
}

var level = 0, re = /[()]/;
while (matches = re.exec(link)) {
if ('(' == matches[0]) {
if (level <= 0) {
level = 1;
} else {
level ++;
}
} else {
level --;
}

re.lastIndex = matches.index + 1;
}

var tail = '';
if (level < 0) {
re = new RegExp("\\){1," + (- level) + "}$");
link = link.replace(re, function (match) {
tail = match;
return '';
});
}

var url = self.encodeAttribute(protocol, link);
link = '<a href="' + url + '">' + url + '</a>';
return self.hashPart(link) + tail;
});

text = text.replace(/<((https?|ftp|dict):[^'">\s]+)>/i, function(match, address) {
//console.log(match);
var url = self.encodeAttribute(address);
Expand Down

0 comments on commit 1e69fdb

Please sign in to comment.