Skip to content

Commit fa0d3cf

Browse files
author
Jiang Shang
committed
链接地址中支持括号
1 parent 691d787 commit fa0d3cf

File tree

4 files changed

+38
-55
lines changed

4 files changed

+38
-55
lines changed

dist/Parser.js

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ var Parser = (function () {
324324
text = text.replace(/<([_a-z0-9-\.\+]+@[^@]+\.[a-z]{2,})>/ig, "<a href=\"mailto:$1\">$1</a>");
325325

326326
// autolink url
327-
text = text.replace(/(^|[^"])((http|https|ftp|mailto):[_a-z0-9-\.\/%#@\?\+=~\|\,]+)($|[^"])/ig, "$1<a href=\"$2\">$2</a>$4");
327+
text = text.replace(/(^|[^"])((http|https|ftp|mailto):[_a-z0-9-\.\/%#@\?\+=~\|\,&\(\)]+)($|[^"])/ig, "$1<a href=\"$2\">$2</a>$4");
328328

329329
text = this.call('afterParseInlineBeforeRelease', text);
330330

@@ -346,6 +346,8 @@ var Parser = (function () {
346346
}, {
347347
key: 'parseBlock',
348348
value: function parseBlock(text, lines) {
349+
var _this4 = this;
350+
349351
this.blocks = [];
350352
this.current = 'normal';
351353
this.pos = -1;
@@ -458,34 +460,28 @@ var Parser = (function () {
458460
case /^((?:(?:(?:[ :]*\-[ :]*)+(?:\||\+))|(?:(?:\||\+)(?:[ :]*\-[ :]*)+)|(?:(?:[ :]*\-[ :]*)+(?:\||\+)(?:[ :]*\-[ :]*)+))+)$/g.test(line):
459461
var tableMatches = /^((?:(?:(?:[ :]*\-[ :]*)+(?:\||\+))|(?:(?:\||\+)(?:[ :]*\-[ :]*)+)|(?:(?:[ :]*\-[ :]*)+(?:\||\+)(?:[ :]*\-[ :]*)+))+)$/g.exec(line);
460462
if (this.isBlock('normal')) {
461-
var block = this.getBlock();
462-
var head = false;
463-
464-
if (block.length === 0 || block[0] !== 'normal' || /^\s*$/.test(lines[block[2]])) {
465-
this.startBlock('table', key);
466-
} else {
467-
head = true;
468-
this.backBlock(1, 'table');
469-
}
470-
471-
if (tableMatches[1][0] == '|') {
472-
tableMatches[1] = tableMatches[1].substr(1);
463+
(function () {
464+
var block = _this4.getBlock();
465+
var head = false;
473466

474-
if (tableMatches[1][tableMatches[1].length - 1] == '|') {
475-
tableMatches[1] = tableMatches[1].slice(0, -1);
467+
if (block.length === 0 || block[0] !== 'normal' || /^\s*$/.test(lines[block[2]])) {
468+
_this4.startBlock('table', key);
469+
} else {
470+
head = true;
471+
_this4.backBlock(1, 'table');
476472
}
477-
}
478473

479-
var rows = tableMatches[1].split(/(\+|\|)/);
480-
var aligns = [];
481-
var _iteratorNormalCompletion = true;
482-
var _didIteratorError = false;
483-
var _iteratorError = undefined;
474+
if (tableMatches[1][0] == '|') {
475+
tableMatches[1] = tableMatches[1].substr(1);
484476

485-
try {
486-
for (var _iterator = rows[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
487-
var row = _step.value;
477+
if (tableMatches[1][tableMatches[1].length - 1] == '|') {
478+
tableMatches[1] = tableMatches[1].slice(0, -1);
479+
}
480+
}
488481

482+
var rows = tableMatches[1].split(/(\+|\|)/);
483+
var aligns = [];
484+
rows.forEach(function (row) {
489485
var align = 'none';
490486

491487
if (tableMatches = row.match(/^\s*(:?)\-+(:?)\s*$/)) {
@@ -499,23 +495,10 @@ var Parser = (function () {
499495
}
500496

501497
aligns.push(align);
502-
}
503-
} catch (err) {
504-
_didIteratorError = true;
505-
_iteratorError = err;
506-
} finally {
507-
try {
508-
if (!_iteratorNormalCompletion && _iterator['return']) {
509-
_iterator['return']();
510-
}
511-
} finally {
512-
if (_didIteratorError) {
513-
throw _iteratorError;
514-
}
515-
}
516-
}
498+
});
517499

518-
this.setBlock(key, [head, aligns]);
500+
_this4.setBlock(key, [head, aligns]);
501+
})();
519502
}
520503
break;
521504

@@ -688,10 +671,10 @@ var Parser = (function () {
688671
}, {
689672
key: 'parsePre',
690673
value: function parsePre(lines) {
691-
var _this4 = this;
674+
var _this5 = this;
692675

693676
lines.forEach(function (line, ind) {
694-
lines[ind] = _this4.htmlspecialchars(line.substr(4));
677+
lines[ind] = _this5.htmlspecialchars(line.substr(4));
695678
});
696679
var str = lines.join('\n');
697680

@@ -763,7 +746,7 @@ var Parser = (function () {
763746
}, {
764747
key: 'parseList',
765748
value: function parseList(lines) {
766-
var _this5 = this;
749+
var _this6 = this;
767750

768751
var html = '';
769752
var minSpace = 99999;
@@ -818,7 +801,7 @@ var Parser = (function () {
818801
}
819802

820803
if (leftLines.length) {
821-
html += "<li>" + _this5.parse(leftLines.join("\n")) + "</li>";
804+
html += "<li>" + _this6.parse(leftLines.join("\n")) + "</li>";
822805
}
823806

824807
leftLines = [text];
@@ -845,7 +828,7 @@ var Parser = (function () {
845828
}, {
846829
key: 'parseTable',
847830
value: function parseTable(lines, value) {
848-
var _this6 = this;
831+
var _this7 = this;
849832

850833
var _value = _slicedToArray(value, 2);
851834

@@ -924,7 +907,7 @@ var Parser = (function () {
924907
html += ' align="' + aligns[key] + '"';
925908
}
926909

927-
html += '>' + _this6.parseInline(text) + ('</' + tag + '>');
910+
html += '>' + _this7.parseInline(text) + ('</' + tag + '>');
928911
});
929912

930913
html += '</tr>';
@@ -937,9 +920,9 @@ var Parser = (function () {
937920
};
938921

939922
for (var key in lines) {
940-
var _ret = _loop(key);
923+
var _ret2 = _loop(key);
941924

942-
if (_ret === 'continue') continue;
925+
if (_ret2 === 'continue') continue;
943926
}
944927

945928
if (body !== null) {
@@ -970,10 +953,10 @@ var Parser = (function () {
970953
}, {
971954
key: 'parseNormal',
972955
value: function parseNormal(lines) {
973-
var _this7 = this;
956+
var _this8 = this;
974957

975958
lines = lines.map(function (line) {
976-
return _this7.parseInline(line);
959+
return _this8.parseInline(line);
977960
});
978961

979962
var str = lines.join("\n").trim();
@@ -1031,10 +1014,10 @@ var Parser = (function () {
10311014
}, {
10321015
key: 'parseHtml',
10331016
value: function parseHtml(lines, type) {
1034-
var _this8 = this;
1017+
var _this9 = this;
10351018

10361019
lines.forEach(function (line) {
1037-
line = _this8.parseInline(line, _this8.specialWhiteList[type] ? _this8.specialWhiteList[type] : '');
1020+
line = _this9.parseInline(line, _this9.specialWhiteList[type] ? _this9.specialWhiteList[type] : '');
10381021
});
10391022

10401023
return lines.join("\n");

hyperdown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@
370370
text = text.replace(/<([_a-z0-9-\.\+]+@[^@]+\.[a-z]{2,})>/ig, "<a href=\"mailto:$1\">$1</a>");
371371

372372
// autolink url
373-
text = text.replace(/(^|[^"])((http|https|ftp|mailto):[_a-z0-9-\.\/%#@\?\+=~\|\,]+)($|[^"])/ig, "$1<a href=\"$2\">$2</a>$4");
373+
text = text.replace(/(^|[^"])((http|https|ftp|mailto):[_a-z0-9-\.\/%#@\?\+=~\|\,&\(\)]+)($|[^"])/ig, "$1<a href=\"$2\">$2</a>$4");
374374

375375
text = this.call('afterParseInlineBeforeRelease', text);
376376

src/Parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export default class Parser {
270270
text = text.replace(/<([_a-z0-9-\.\+]+@[^@]+\.[a-z]{2,})>/ig, "<a href=\"mailto:$1\">$1</a>")
271271

272272
// autolink url
273-
text = text.replace(/(^|[^"])((http|https|ftp|mailto):[_a-z0-9-\.\/%#@\?\+=~\|\,]+)($|[^"])/ig,
273+
text = text.replace(/(^|[^"])((http|https|ftp|mailto):[_a-z0-9-\.\/%#@\?\+=~\|\,&\(\)]+)($|[^"])/ig,
274274
"$1<a href=\"$2\">$2</a>$4")
275275

276276
text = this.call('afterParseInlineBeforeRelease', text);

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('HyperDown.js', function() {
4242
assert.equal('<p><a href="http://sf.gg">sf.gg</a>123</p>', parser.makeHtml('[sf.gg](http://sf.gg)123'));
4343
});
4444
it('type2', function() {
45-
assert.equal('<p>123<a href="http://sf.gg">http://sf.gg</a> 123 <a href="http://sf.gg">http://sf.gg</a></p>', parser.makeHtml('123http://sf.gg 123 http://sf.gg'));
45+
assert.equal('<p>123<a href="http://sf.gg(tt)">http://sf.gg(tt)</a> 123 <a href="http://sf.gg">http://sf.gg</a></p>', parser.makeHtml('123http://sf.gg(tt) 123 http://sf.gg'));
4646
});
4747
it('type3', function() {
4848
assert.equal('<p><a href="http://sf.gg">sf.gg</a></p>', parser.makeHtml('[sf.gg][1]\n [1]: http://sf.gg'));

0 commit comments

Comments
 (0)