Skip to content

Commit 876748f

Browse files
author
Jiang Shang
committed
跟进
1 parent 9f9d77a commit 876748f

File tree

4 files changed

+218
-167
lines changed

4 files changed

+218
-167
lines changed

dist/Parser.js

Lines changed: 77 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ var Parser = (function () {
7575
}, {
7676
key: 'makeHolder',
7777
value: function makeHolder(str) {
78-
var key = '|' + this.uniqid + this.id + '|';
78+
var key = '|\r' + this.uniqid + this.id + '\r|';
7979
this.id++;
8080
this.holders[key] = str;
8181
return key;
@@ -194,10 +194,16 @@ var Parser = (function () {
194194
}, {
195195
key: 'releaseHolder',
196196
value: function releaseHolder(text) {
197-
for (var key in this.holders) {
198-
var value = this.holders[key];
199-
text = text.replace(key, value);
197+
198+
var deep = 0;
199+
while (text.indexOf("|\r") !== -1 && deep < 10) {
200+
for (var key in this.holders) {
201+
var value = this.holders[key];
202+
text = text.replace(key, value);
203+
}
204+
deep++;
200205
}
206+
201207
this.holders.clear();
202208

203209
return text;
@@ -293,15 +299,19 @@ var Parser = (function () {
293299
}
294300

295301
// escape
296-
var escapeMatches = /\\(`|\*|_)/.exec(text);
302+
var escapeMatches = /\\(`|\*|_|~)/.exec(text);
297303
if (escapeMatches) {
298304
text = this.makeHolder(this.htmlspecialchars(escapeMatches[1]));
299305
}
300306

301307
// strong and em and some fuck
302-
text = text.replace(/(_|\*){3}(.+?)\1{3}/, "<strong><em>$2</em></strong>");
303-
text = text.replace(/(_|\*){2}(.+?)\1{2}/, "<strong>$2</strong>");
304-
text = text.replace(/(_|\*)(.+?)\1/, "<em>$2</em>");
308+
text = text.replace(/(\*{3})(.+?)\1/, "<strong><em>$2</em></strong>");
309+
text = text.replace(/(\*{2})(.+?)\1/, "<strong>$2</strong>");
310+
text = text.replace(/(\*)(.+?)\1/, "<em>$2</em>");
311+
text = text.replace(/(\s+)(_{3})(.+?)\2(\s+)/, "$1<strong><em>$3</em></strong>$4");
312+
text = text.replace(/(\s+)(_{2})(.+?)\2(\s+)/, "$1<strong>$3</strong>$4");
313+
text = text.replace(/(\s+)(_)(.+?)\2(\s+)/, "$1<em>$3</em>$4");
314+
text = text.replace(/(~{2})(.+?)\1/, "<del>$2</del>");
305315
text = text.replace(/<(https?:\/\/.+)>/i, "<a href=\"$1\">$1</a>");
306316
text = text.replace(/<([_a-z0-9-\.\+]+@[^@]+\.[a-z]{2,})>/i, "<a href=\"mailto:$1\">$1</a>");
307317

@@ -312,6 +322,7 @@ var Parser = (function () {
312322

313323
// release
314324
text = this.releaseHolder(text);
325+
315326
text = this.call('afterParseInline', text);
316327

317328
return text;
@@ -365,9 +376,9 @@ var Parser = (function () {
365376
var htmlPattern1 = new RegExp('^\s*<(' + special + ')(\s+[^>]*)?>', 'i');
366377
var htmlPattern2 = new RegExp('<\/(' + special + ')>\s*$', 'i');
367378
if (matches = line.match(htmlPattern1)) {
368-
tag = matches[1].toLowerCase();
369-
if (!this.isBlock('html', tag) && !this.isBlock('pre')) {
370-
this.startBlock('html', key, tag);
379+
var _tag = matches[1].toLowerCase();
380+
if (!this.isBlock('html', _tag) && !this.isBlock('pre')) {
381+
this.startBlock('html', key, _tag);
371382
}
372383

373384
continue;
@@ -413,8 +424,17 @@ var Parser = (function () {
413424
this.startBlock('definition', key).endBlock();
414425
break;
415426

427+
// block quote
428+
case /^\s*>/.test(line):
429+
if (this.isBlock('quote')) {
430+
this.setBlock(key);
431+
} else {
432+
this.startBlock('quote', key);
433+
}
434+
break;
435+
416436
// pre
417-
case /^ {4,}/.test(line):
437+
case /^ {4}/.test(line):
418438
emptyCount = 0;
419439
if (this.isBlock('pre')) {
420440
this.setBlock(key);
@@ -506,15 +526,6 @@ var Parser = (function () {
506526
}
507527
break;
508528

509-
// block quote
510-
case /^>/.test(line):
511-
if (this.isBlock('quote')) {
512-
this.setBlock(key);
513-
} else {
514-
this.startBlock('quote', key);
515-
}
516-
break;
517-
518529
// hr
519530
case /^[-\*]{3,}\s*$/.test(line):
520531
this.startBlock('hr', key).endBlock();
@@ -745,7 +756,7 @@ var Parser = (function () {
745756
key: 'parseQuote',
746757
value: function parseQuote(lines) {
747758
lines.forEach(function (line, key) {
748-
lines[key] = line.replace(/^> ?/, '');
759+
lines[key] = line.replace(/^\s*> ?/, '');
749760
});
750761
var str = lines.join('\n');
751762
return (/^\s*$/.test(str) ? '' : '<blockquote>' + this.parse(str) + '</blockquote>'
@@ -894,62 +905,53 @@ var Parser = (function () {
894905
var ignore = head ? 1 : 0;
895906

896907
var html = '<table>';
897-
var body = null;
908+
var body = false;
898909

899-
for (var key in lines) {
910+
var _loop = function (key) {
900911
var line = lines[key];
901912
if (key === ignore) {
902913
head = false;
903914
body = true;
904-
continue;
915+
return 'continue';
905916
}
917+
918+
if (line) {
919+
line = line.trim();
920+
}
921+
906922
if (line[0] === '|') {
907923
line = line.substr(1);
924+
908925
if (line[line.length - 1] === '|') {
909926
line = line.substr(0, -1);
910927
}
911928
}
912929

913-
line = line.replace(/^(\|?)(.*?)\1$/, "$2", line);
914-
var rows = line.split('|').map(function (item) {
915-
return item.trim();
930+
var rows = line.split('|').map(function (row) {
931+
if (row.match(/^\s+$/)) {
932+
return ' ';
933+
} else {
934+
return row.trim();
935+
}
916936
});
937+
917938
var columns = [];
918939
var last = -1;
919940

920-
var _iteratorNormalCompletion5 = true;
921-
var _didIteratorError5 = false;
922-
var _iteratorError5 = undefined;
923-
924-
try {
925-
for (var _iterator5 = rows[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
926-
var row = _step5.value;
927-
928-
if (row.length > 0) {
929-
last++;
930-
columns[last] = [1, row];
931-
} else if (columns[last]) {
932-
columns[last][0]++;
933-
}
934-
}
935-
} catch (err) {
936-
_didIteratorError5 = true;
937-
_iteratorError5 = err;
938-
} finally {
939-
try {
940-
if (!_iteratorNormalCompletion5 && _iterator5['return']) {
941-
_iterator5['return']();
942-
}
943-
} finally {
944-
if (_didIteratorError5) {
945-
throw _iteratorError5;
946-
}
941+
rows.forEach(function (row) {
942+
if (row.length > 0) {
943+
last++;
944+
columns[last] = [columns[last] ? columns[last][0] + 1 : 1, row];
945+
} else if (columns[last]) {
946+
columns[last][0]++;
947+
} else {
948+
columns[0] = [1, row];
947949
}
948-
}
950+
});
949951

950-
if (head) {
952+
if (head === true) {
951953
html += '<thead>';
952-
} else if (body) {
954+
} else if (body === true) {
953955
html += '<tbody>';
954956
}
955957

@@ -982,6 +984,12 @@ var Parser = (function () {
982984
} else if (body) {
983985
body = false;
984986
}
987+
};
988+
989+
for (var key in lines) {
990+
var _ret = _loop(key);
991+
992+
if (_ret === 'continue') continue;
985993
}
986994

987995
if (body !== null) {
@@ -1074,27 +1082,27 @@ var Parser = (function () {
10741082
}, {
10751083
key: 'parseHtml',
10761084
value: function parseHtml(lines, type) {
1077-
var _iteratorNormalCompletion6 = true;
1078-
var _didIteratorError6 = false;
1079-
var _iteratorError6 = undefined;
1085+
var _iteratorNormalCompletion5 = true;
1086+
var _didIteratorError5 = false;
1087+
var _iteratorError5 = undefined;
10801088

10811089
try {
1082-
for (var _iterator6 = lines[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
1083-
var line = _step6.value;
1090+
for (var _iterator5 = lines[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
1091+
var line = _step5.value;
10841092

10851093
line = this.parseInline(line, this.specialWhiteList[type] ? this.specialWhiteList[type] : '');
10861094
}
10871095
} catch (err) {
1088-
_didIteratorError6 = true;
1089-
_iteratorError6 = err;
1096+
_didIteratorError5 = true;
1097+
_iteratorError5 = err;
10901098
} finally {
10911099
try {
1092-
if (!_iteratorNormalCompletion6 && _iterator6['return']) {
1093-
_iterator6['return']();
1100+
if (!_iteratorNormalCompletion5 && _iterator5['return']) {
1101+
_iterator5['return']();
10941102
}
10951103
} finally {
1096-
if (_didIteratorError6) {
1097-
throw _iteratorError6;
1104+
if (_didIteratorError5) {
1105+
throw _iteratorError5;
10981106
}
10991107
}
11001108
}

0 commit comments

Comments
 (0)