Skip to content

Commit 3870345

Browse files
author
Jiang Shang
committed
fix #5
1 parent 2bb46b9 commit 3870345

File tree

3 files changed

+137
-146
lines changed

3 files changed

+137
-146
lines changed

dist/Parser.js

+62-67
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,25 @@ var Parser = (function () {
110110
}, {
111111
key: 'makeFootnotes',
112112
value: function makeFootnotes(html) {
113-
if (this.footnotes.length > 0) {
114-
html += '<div class="footnotes"><hr><ol>';
115-
var index = 1;
116-
var val = this.footnotes.pop();
117-
while (val) {
118-
if (typeof val === 'string') {
119-
val += ' <a href="#fnref-' + index + '" class="footnote-backref">&#8617;</a>';
120-
} else {
121-
val[val.length - 1] += ' <a href="#fnref-' + index + '" class="footnote-backref">&#8617;</a>';
122-
val = val.length > 1 ? this.parse(val.join("\n")) : this.parseInline(val[0]);
123-
}
113+
var _this2 = this;
124114

125-
html += '<li id="fn-' + index + '">' + val + '</li>';
115+
if (this.footnotes.length > 0) {
116+
(function () {
117+
html += '<div class="footnotes"><hr><ol>';
118+
var index = 1;
119+
_this2.footnotes.forEach(function (val) {
120+
if (typeof val === 'string') {
121+
val += ' <a href="#fnref-' + index + '" class="footnote-backref">&#8617;</a>';
122+
} else {
123+
val[val.length - 1] += ' <a href="#fnref-' + index + '" class="footnote-backref">&#8617;</a>';
124+
val = val.length > 1 ? _this2.parse(val.join("\n")) : _this2.parseInline(val[0]);
125+
}
126126

127-
index++;
128-
val = this.footnotes.pop();
129-
}
130-
html += '</ol></div>';
127+
html += '<li id="fn-' + index + '">' + val + '</li>';
128+
index++;
129+
});
130+
html += '</ol></div>';
131+
})();
131132
}
132133
return html;
133134
}
@@ -141,7 +142,7 @@ var Parser = (function () {
141142
}, {
142143
key: 'parse',
143144
value: function parse(text) {
144-
var _this2 = this;
145+
var _this3 = this;
145146

146147
var lines = text.split("\n");
147148
var blocks = this.parseBlock(text, lines);
@@ -158,9 +159,9 @@ var Parser = (function () {
158159
var extract = lines.slice(start, end + 1);
159160
var method = 'parse' + type.slice(0, 1).toUpperCase() + type.slice(1);
160161
var beforeMethod = 'beforeParse' + type.slice(0, 1).toUpperCase() + type.slice(1);
161-
extract = _this2.call(beforeMethod, extract, value);
162-
var result = _this2[method](extract, value);
163-
result = _this2.call('after' + method.slice(0, 1).toUpperCase() + method.slice(1), result, value);
162+
extract = _this3.call(beforeMethod, extract, value);
163+
var result = _this3[method](extract, value);
164+
result = _this3.call('after' + method.slice(0, 1).toUpperCase() + method.slice(1), result, value);
164165

165166
html += result;
166167
});
@@ -177,23 +178,20 @@ var Parser = (function () {
177178
*/
178179
}, {
179180
key: 'call',
180-
value: function call(type) {
181-
for (var _len = arguments.length, value = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
182-
value[_key - 1] = arguments[_key];
183-
}
184-
181+
value: function call(type, value) {
185182
if (!this.hooks[type]) {
186-
return value[0];
183+
return value;
187184
}
188185

189-
var args = value;
186+
var args = [].slice.call(arguments);
187+
args = args.slice(1);
190188

191189
this.hooks[type].forEach(function (callback) {
192-
value = callback(args);
190+
value = callback.apply(null, args);
193191
args[0] = value;
194192
});
195193

196-
return value[0];
194+
return value;
197195
}
198196

199197
/**
@@ -231,7 +229,7 @@ var Parser = (function () {
231229
}, {
232230
key: 'parseInline',
233231
value: function parseInline(text) {
234-
var _this3 = this;
232+
var _this4 = this;
235233

236234
var whiteList = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1];
237235
var clearHolders = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
@@ -245,15 +243,15 @@ var Parser = (function () {
245243

246244
// link
247245
text = text.replace(/<(https?:\/\/.+)>/ig, function (match, p1) {
248-
return _this3.makeHolder('<a href="' + p1 + '">' + p1 + '</a>');
246+
return _this4.makeHolder('<a href="' + p1 + '">' + p1 + '</a>');
249247
});
250248

251249
text = text.replace(/<(\/?)([a-z0-9-]+)(\s+[^>]*)?>/ig, function (match, p1, p2, p3) {
252-
var whiteLists = _this3.commonWhiteList + '|' + whiteList;
250+
var whiteLists = _this4.commonWhiteList + '|' + whiteList;
253251
if (whiteLists.toLowerCase().indexOf(p2.toLowerCase()) !== -1) {
254-
return _this3.makeHolder(match);
252+
return _this4.makeHolder(match);
255253
} else {
256-
return _this3.htmlspecialchars(match);
254+
return _this4.htmlspecialchars(match);
257255
}
258256
});
259257

@@ -266,11 +264,11 @@ var Parser = (function () {
266264
var id = _this.footnotes.indexOf(p1);
267265

268266
if (id === -1) {
269-
id = _this.footnotes.length + 1;
270-
_this.footnotes[id] = _this3.parseInline(p1, '', false);
267+
id = _this.footnotes.length;
268+
_this.footnotes.push(_this4.parseInline(p1, '', false));
271269
}
272270

273-
return _this.makeHolder('<sup id="fnref-' + id + '"><a href="#fn-' + id + '" class="footnote-ref">' + id + '</a></sup>');
271+
return _this.makeHolder('<sup id="fnref-' + (id + 1) + '"><a href="#fn-' + (id + 1) + '" class="footnote-ref">' + (id + 1) + '</a></sup>');
274272
});
275273

276274
// image
@@ -339,34 +337,34 @@ var Parser = (function () {
339337
}, {
340338
key: 'parseInlineCallback',
341339
value: function parseInlineCallback(text) {
342-
var _this4 = this;
340+
var _this5 = this;
343341

344342
text = text.replace(/(\*{3})(.+?)\1/g, function (match, p1, p2) {
345-
return '<strong><em>' + _this4.parseInlineCallback(p2) + '</em></strong>';
343+
return '<strong><em>' + _this5.parseInlineCallback(p2) + '</em></strong>';
346344
});
347345

348346
text = text.replace(/(\*{2})(.+?)\1/g, function (match, p1, p2) {
349-
return '<strong>' + _this4.parseInlineCallback(p2) + '</strong>';
347+
return '<strong>' + _this5.parseInlineCallback(p2) + '</strong>';
350348
});
351349

352350
text = text.replace(/(\*)(.+?)\1/g, function (match, p1, p2) {
353-
return '<em>' + _this4.parseInlineCallback(p2) + '</em>';
351+
return '<em>' + _this5.parseInlineCallback(p2) + '</em>';
354352
});
355353

356354
text = text.replace(/(\s+|^)(_{3})(.+?)\2(\s+|$)/g, function (match, p1, p2, p3, p4) {
357-
return p1 + '<strong><em>' + _this4.parseInlineCallback(p3) + '</em></strong>' + p4;
355+
return p1 + '<strong><em>' + _this5.parseInlineCallback(p3) + '</em></strong>' + p4;
358356
});
359357

360358
text = text.replace(/(\s+|^)(_{2})(.+?)\2(\s+|$)/g, function (match, p1, p2, p3, p4) {
361-
return p1 + '<strong>' + _this4.parseInlineCallback(p3) + '</strong>' + p4;
359+
return p1 + '<strong>' + _this5.parseInlineCallback(p3) + '</strong>' + p4;
362360
});
363361

364362
text = text.replace(/(\s+|^)(_)(.+?)\2(\s+|$)/g, function (match, p1, p2, p3, p4) {
365-
return p1 + '<em>' + _this4.parseInlineCallback(p3) + '</em>' + p4;
363+
return p1 + '<em>' + _this5.parseInlineCallback(p3) + '</em>' + p4;
366364
});
367365

368366
text = text.replace(/(~{2})(.+?)\1/g, function (match, p1, p2) {
369-
return '<del>' + _this4.parseInlineCallback(p2) + '</del>';
367+
return '<del>' + _this5.parseInlineCallback(p2) + '</del>';
370368
});
371369
return text;
372370
}
@@ -381,7 +379,7 @@ var Parser = (function () {
381379
}, {
382380
key: 'parseBlock',
383381
value: function parseBlock(text, lines) {
384-
var _this5 = this;
382+
var _this6 = this;
385383

386384
this.blocks = [];
387385
this.current = 'normal';
@@ -496,14 +494,14 @@ var Parser = (function () {
496494
var tableMatches = /^((?:(?:(?:[ :]*\-[ :]*)+(?:\||\+))|(?:(?:\||\+)(?:[ :]*\-[ :]*)+)|(?:(?:[ :]*\-[ :]*)+(?:\||\+)(?:[ :]*\-[ :]*)+))+)$/g.exec(line);
497495
if (this.isBlock('normal')) {
498496
(function () {
499-
var block = _this5.getBlock();
497+
var block = _this6.getBlock();
500498
var head = false;
501499

502500
if (block.length === 0 || block[0] !== 'normal' || /^\s*$/.test(lines[block[2]])) {
503-
_this5.startBlock('table', key);
501+
_this6.startBlock('table', key);
504502
} else {
505503
head = true;
506-
_this5.backBlock(1, 'table');
504+
_this6.backBlock(1, 'table');
507505
}
508506

509507
if (tableMatches[1][0] == '|') {
@@ -532,7 +530,7 @@ var Parser = (function () {
532530
aligns.push(align);
533531
});
534532

535-
_this5.setBlock(key, [head, aligns]);
533+
_this6.setBlock(key, [head, aligns]);
536534
})();
537535
}
538536
break;
@@ -671,9 +669,9 @@ var Parser = (function () {
671669
var types = ['list', 'quote'];
672670

673671
if (from === to && lines[from].match(/^\s*$/) && prevBlock && nextBlock) {
674-
if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0] !== -1)) {
672+
if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0]) !== -1) {
675673
// combine 3 blocks
676-
blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], NULL];
674+
blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], null];
677675
blocks.splice(key, 2);
678676
}
679677
}
@@ -724,10 +722,10 @@ var Parser = (function () {
724722
}, {
725723
key: 'parsePre',
726724
value: function parsePre(lines) {
727-
var _this6 = this;
725+
var _this7 = this;
728726

729727
lines.forEach(function (line, ind) {
730-
lines[ind] = _this6.htmlspecialchars(line.substr(4));
728+
lines[ind] = _this7.htmlspecialchars(line.substr(4));
731729
});
732730
var str = lines.join('\n');
733731

@@ -799,7 +797,7 @@ var Parser = (function () {
799797
}, {
800798
key: 'parseList',
801799
value: function parseList(lines) {
802-
var _this7 = this;
800+
var _this8 = this;
803801

804802
var html = '';
805803
var minSpace = 99999;
@@ -846,7 +844,7 @@ var Parser = (function () {
846844
leftLines.push(line.replace(pattern, ''));
847845
} else {
848846
if (leftLines.length) {
849-
html += "<li>" + _this7.parse(leftLines.join("\n")) + "</li>";
847+
html += "<li>" + _this8.parse(leftLines.join("\n")) + "</li>";
850848
}
851849
if (lastType !== type) {
852850
if (lastType.length) {
@@ -880,7 +878,7 @@ var Parser = (function () {
880878
}, {
881879
key: 'parseTable',
882880
value: function parseTable(lines, value) {
883-
var _this8 = this;
881+
var _this9 = this;
884882

885883
var _value = _slicedToArray(value, 2);
886884

@@ -959,7 +957,7 @@ var Parser = (function () {
959957
html += ' align="' + aligns[key] + '"';
960958
}
961959

962-
html += '>' + _this8.parseInline(text) + ('</' + tag + '>');
960+
html += '>' + _this9.parseInline(text) + ('</' + tag + '>');
963961
});
964962

965963
html += '</tr>';
@@ -972,9 +970,9 @@ var Parser = (function () {
972970
};
973971

974972
for (var key in lines) {
975-
var _ret2 = _loop(key);
973+
var _ret3 = _loop(key);
976974

977-
if (_ret2 === 'continue') continue;
975+
if (_ret3 === 'continue') continue;
978976
}
979977

980978
if (body !== null) {
@@ -1005,10 +1003,10 @@ var Parser = (function () {
10051003
}, {
10061004
key: 'parseNormal',
10071005
value: function parseNormal(lines) {
1008-
var _this9 = this;
1006+
var _this10 = this;
10091007

10101008
lines = lines.map(function (line) {
1011-
return _this9.parseInline(line);
1009+
return _this10.parseInline(line);
10121010
});
10131011

10141012
var str = lines.join("\n").trim();
@@ -1066,10 +1064,10 @@ var Parser = (function () {
10661064
}, {
10671065
key: 'parseHtml',
10681066
value: function parseHtml(lines, type) {
1069-
var _this10 = this;
1067+
var _this11 = this;
10701068

10711069
lines.forEach(function (line) {
1072-
line = _this10.parseInline(line, _this10.specialWhiteList[type] ? _this10.specialWhiteList[type] : '');
1070+
line = _this11.parseInline(line, _this11.specialWhiteList[type] ? _this11.specialWhiteList[type] : '');
10731071
});
10741072

10751073
return lines.join("\n");
@@ -1252,7 +1250,4 @@ var Parser = (function () {
12521250
})();
12531251

12541252
exports['default'] = Parser;
1255-
1256-
var parser = new Parser();
1257-
console.log(parser.makeHtml('[Genymotion](https://www.genymotion.com/)是。[Genymotion](https://www.genymotion.com/) [VirtualBox](https://www.virtualbox.org/)富的[付费版](https://shop.genymotion.com/index.php?controller=order-opc)'));
12581253
module.exports = exports['default'];

0 commit comments

Comments
 (0)