From 38703458c4c1b42ef70e870082c674b80982204c Mon Sep 17 00:00:00 2001 From: Jiang Shang Date: Mon, 9 Nov 2015 20:36:18 +0800 Subject: [PATCH] fix #5 --- dist/Parser.js | 129 ++++++++++++++++++++++++------------------------- hyperdown.js | 126 ++++++++++++++++++++++++----------------------- src/Parser.js | 28 +++++------ 3 files changed, 137 insertions(+), 146 deletions(-) diff --git a/dist/Parser.js b/dist/Parser.js index dd0193c..7ddc473 100644 --- a/dist/Parser.js +++ b/dist/Parser.js @@ -110,24 +110,25 @@ var Parser = (function () { }, { key: 'makeFootnotes', value: function makeFootnotes(html) { - if (this.footnotes.length > 0) { - html += '

    '; - var index = 1; - var val = this.footnotes.pop(); - while (val) { - if (typeof val === 'string') { - val += ' '; - } else { - val[val.length - 1] += ' '; - val = val.length > 1 ? this.parse(val.join("\n")) : this.parseInline(val[0]); - } + var _this2 = this; - html += '
  1. ' + val + '
  2. '; + if (this.footnotes.length > 0) { + (function () { + html += '

      '; + var index = 1; + _this2.footnotes.forEach(function (val) { + if (typeof val === 'string') { + val += ' '; + } else { + val[val.length - 1] += ' '; + val = val.length > 1 ? _this2.parse(val.join("\n")) : _this2.parseInline(val[0]); + } - index++; - val = this.footnotes.pop(); - } - html += '
    '; + html += '
  3. ' + val + '
  4. '; + index++; + }); + html += '
'; + })(); } return html; } @@ -141,7 +142,7 @@ var Parser = (function () { }, { key: 'parse', value: function parse(text) { - var _this2 = this; + var _this3 = this; var lines = text.split("\n"); var blocks = this.parseBlock(text, lines); @@ -158,9 +159,9 @@ var Parser = (function () { var extract = lines.slice(start, end + 1); var method = 'parse' + type.slice(0, 1).toUpperCase() + type.slice(1); var beforeMethod = 'beforeParse' + type.slice(0, 1).toUpperCase() + type.slice(1); - extract = _this2.call(beforeMethod, extract, value); - var result = _this2[method](extract, value); - result = _this2.call('after' + method.slice(0, 1).toUpperCase() + method.slice(1), result, value); + extract = _this3.call(beforeMethod, extract, value); + var result = _this3[method](extract, value); + result = _this3.call('after' + method.slice(0, 1).toUpperCase() + method.slice(1), result, value); html += result; }); @@ -177,23 +178,20 @@ var Parser = (function () { */ }, { key: 'call', - value: function call(type) { - for (var _len = arguments.length, value = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { - value[_key - 1] = arguments[_key]; - } - + value: function call(type, value) { if (!this.hooks[type]) { - return value[0]; + return value; } - var args = value; + var args = [].slice.call(arguments); + args = args.slice(1); this.hooks[type].forEach(function (callback) { - value = callback(args); + value = callback.apply(null, args); args[0] = value; }); - return value[0]; + return value; } /** @@ -231,7 +229,7 @@ var Parser = (function () { }, { key: 'parseInline', value: function parseInline(text) { - var _this3 = this; + var _this4 = this; var whiteList = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1]; var clearHolders = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2]; @@ -245,15 +243,15 @@ var Parser = (function () { // link text = text.replace(/<(https?:\/\/.+)>/ig, function (match, p1) { - return _this3.makeHolder('' + p1 + ''); + return _this4.makeHolder('' + p1 + ''); }); text = text.replace(/<(\/?)([a-z0-9-]+)(\s+[^>]*)?>/ig, function (match, p1, p2, p3) { - var whiteLists = _this3.commonWhiteList + '|' + whiteList; + var whiteLists = _this4.commonWhiteList + '|' + whiteList; if (whiteLists.toLowerCase().indexOf(p2.toLowerCase()) !== -1) { - return _this3.makeHolder(match); + return _this4.makeHolder(match); } else { - return _this3.htmlspecialchars(match); + return _this4.htmlspecialchars(match); } }); @@ -266,11 +264,11 @@ var Parser = (function () { var id = _this.footnotes.indexOf(p1); if (id === -1) { - id = _this.footnotes.length + 1; - _this.footnotes[id] = _this3.parseInline(p1, '', false); + id = _this.footnotes.length; + _this.footnotes.push(_this4.parseInline(p1, '', false)); } - return _this.makeHolder('' + id + ''); + return _this.makeHolder('' + (id + 1) + ''); }); // image @@ -339,34 +337,34 @@ var Parser = (function () { }, { key: 'parseInlineCallback', value: function parseInlineCallback(text) { - var _this4 = this; + var _this5 = this; text = text.replace(/(\*{3})(.+?)\1/g, function (match, p1, p2) { - return '' + _this4.parseInlineCallback(p2) + ''; + return '' + _this5.parseInlineCallback(p2) + ''; }); text = text.replace(/(\*{2})(.+?)\1/g, function (match, p1, p2) { - return '' + _this4.parseInlineCallback(p2) + ''; + return '' + _this5.parseInlineCallback(p2) + ''; }); text = text.replace(/(\*)(.+?)\1/g, function (match, p1, p2) { - return '' + _this4.parseInlineCallback(p2) + ''; + return '' + _this5.parseInlineCallback(p2) + ''; }); text = text.replace(/(\s+|^)(_{3})(.+?)\2(\s+|$)/g, function (match, p1, p2, p3, p4) { - return p1 + '' + _this4.parseInlineCallback(p3) + '' + p4; + return p1 + '' + _this5.parseInlineCallback(p3) + '' + p4; }); text = text.replace(/(\s+|^)(_{2})(.+?)\2(\s+|$)/g, function (match, p1, p2, p3, p4) { - return p1 + '' + _this4.parseInlineCallback(p3) + '' + p4; + return p1 + '' + _this5.parseInlineCallback(p3) + '' + p4; }); text = text.replace(/(\s+|^)(_)(.+?)\2(\s+|$)/g, function (match, p1, p2, p3, p4) { - return p1 + '' + _this4.parseInlineCallback(p3) + '' + p4; + return p1 + '' + _this5.parseInlineCallback(p3) + '' + p4; }); text = text.replace(/(~{2})(.+?)\1/g, function (match, p1, p2) { - return '' + _this4.parseInlineCallback(p2) + ''; + return '' + _this5.parseInlineCallback(p2) + ''; }); return text; } @@ -381,7 +379,7 @@ var Parser = (function () { }, { key: 'parseBlock', value: function parseBlock(text, lines) { - var _this5 = this; + var _this6 = this; this.blocks = []; this.current = 'normal'; @@ -496,14 +494,14 @@ var Parser = (function () { var tableMatches = /^((?:(?:(?:[ :]*\-[ :]*)+(?:\||\+))|(?:(?:\||\+)(?:[ :]*\-[ :]*)+)|(?:(?:[ :]*\-[ :]*)+(?:\||\+)(?:[ :]*\-[ :]*)+))+)$/g.exec(line); if (this.isBlock('normal')) { (function () { - var block = _this5.getBlock(); + var block = _this6.getBlock(); var head = false; if (block.length === 0 || block[0] !== 'normal' || /^\s*$/.test(lines[block[2]])) { - _this5.startBlock('table', key); + _this6.startBlock('table', key); } else { head = true; - _this5.backBlock(1, 'table'); + _this6.backBlock(1, 'table'); } if (tableMatches[1][0] == '|') { @@ -532,7 +530,7 @@ var Parser = (function () { aligns.push(align); }); - _this5.setBlock(key, [head, aligns]); + _this6.setBlock(key, [head, aligns]); })(); } break; @@ -671,9 +669,9 @@ var Parser = (function () { var types = ['list', 'quote']; if (from === to && lines[from].match(/^\s*$/) && prevBlock && nextBlock) { - if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0] !== -1)) { + if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0]) !== -1) { // combine 3 blocks - blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], NULL]; + blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], null]; blocks.splice(key, 2); } } @@ -724,10 +722,10 @@ var Parser = (function () { }, { key: 'parsePre', value: function parsePre(lines) { - var _this6 = this; + var _this7 = this; lines.forEach(function (line, ind) { - lines[ind] = _this6.htmlspecialchars(line.substr(4)); + lines[ind] = _this7.htmlspecialchars(line.substr(4)); }); var str = lines.join('\n'); @@ -799,7 +797,7 @@ var Parser = (function () { }, { key: 'parseList', value: function parseList(lines) { - var _this7 = this; + var _this8 = this; var html = ''; var minSpace = 99999; @@ -846,7 +844,7 @@ var Parser = (function () { leftLines.push(line.replace(pattern, '')); } else { if (leftLines.length) { - html += "
  • " + _this7.parse(leftLines.join("\n")) + "
  • "; + html += "
  • " + _this8.parse(leftLines.join("\n")) + "
  • "; } if (lastType !== type) { if (lastType.length) { @@ -880,7 +878,7 @@ var Parser = (function () { }, { key: 'parseTable', value: function parseTable(lines, value) { - var _this8 = this; + var _this9 = this; var _value = _slicedToArray(value, 2); @@ -959,7 +957,7 @@ var Parser = (function () { html += ' align="' + aligns[key] + '"'; } - html += '>' + _this8.parseInline(text) + (''); + html += '>' + _this9.parseInline(text) + (''); }); html += ''; @@ -972,9 +970,9 @@ var Parser = (function () { }; for (var key in lines) { - var _ret2 = _loop(key); + var _ret3 = _loop(key); - if (_ret2 === 'continue') continue; + if (_ret3 === 'continue') continue; } if (body !== null) { @@ -1005,10 +1003,10 @@ var Parser = (function () { }, { key: 'parseNormal', value: function parseNormal(lines) { - var _this9 = this; + var _this10 = this; lines = lines.map(function (line) { - return _this9.parseInline(line); + return _this10.parseInline(line); }); var str = lines.join("\n").trim(); @@ -1066,10 +1064,10 @@ var Parser = (function () { }, { key: 'parseHtml', value: function parseHtml(lines, type) { - var _this10 = this; + var _this11 = this; lines.forEach(function (line) { - line = _this10.parseInline(line, _this10.specialWhiteList[type] ? _this10.specialWhiteList[type] : ''); + line = _this11.parseInline(line, _this11.specialWhiteList[type] ? _this11.specialWhiteList[type] : ''); }); return lines.join("\n"); @@ -1252,7 +1250,4 @@ var Parser = (function () { })(); exports['default'] = Parser; - -var parser = new Parser(); -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)')); module.exports = exports['default']; diff --git a/hyperdown.js b/hyperdown.js index 1a5b267..053ac57 100644 --- a/hyperdown.js +++ b/hyperdown.js @@ -156,24 +156,25 @@ }, { key: 'makeFootnotes', value: function makeFootnotes(html) { - if (this.footnotes.length > 0) { - html += '

      '; - var index = 1; - var val = this.footnotes.pop(); - while (val) { - if (typeof val === 'string') { - val += ' '; - } else { - val[val.length - 1] += ' '; - val = val.length > 1 ? this.parse(val.join("\n")) : this.parseInline(val[0]); - } + var _this2 = this; - html += '
    1. ' + val + '
    2. '; + if (this.footnotes.length > 0) { + (function () { + html += '

        '; + var index = 1; + _this2.footnotes.forEach(function (val) { + if (typeof val === 'string') { + val += ' '; + } else { + val[val.length - 1] += ' '; + val = val.length > 1 ? _this2.parse(val.join("\n")) : _this2.parseInline(val[0]); + } - index++; - val = this.footnotes.pop(); - } - html += '
      '; + html += '
    3. ' + val + '
    4. '; + index++; + }); + html += '
    '; + })(); } return html; } @@ -187,7 +188,7 @@ }, { key: 'parse', value: function parse(text) { - var _this2 = this; + var _this3 = this; var lines = text.split("\n"); var blocks = this.parseBlock(text, lines); @@ -204,9 +205,9 @@ var extract = lines.slice(start, end + 1); var method = 'parse' + type.slice(0, 1).toUpperCase() + type.slice(1); var beforeMethod = 'beforeParse' + type.slice(0, 1).toUpperCase() + type.slice(1); - extract = _this2.call(beforeMethod, extract, value); - var result = _this2[method](extract, value); - result = _this2.call('after' + method.slice(0, 1).toUpperCase() + method.slice(1), result, value); + extract = _this3.call(beforeMethod, extract, value); + var result = _this3[method](extract, value); + result = _this3.call('after' + method.slice(0, 1).toUpperCase() + method.slice(1), result, value); html += result; }); @@ -223,23 +224,20 @@ */ }, { key: 'call', - value: function call(type) { - for (var _len = arguments.length, value = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { - value[_key - 1] = arguments[_key]; - } - + value: function call(type, value) { if (!this.hooks[type]) { - return value[0]; + return value; } - var args = value; + var args = [].slice.call(arguments); + args = args.slice(1); this.hooks[type].forEach(function (callback) { - value = callback(args); + value = callback.apply(null, args); args[0] = value; }); - return value[0]; + return value; } /** @@ -277,7 +275,7 @@ }, { key: 'parseInline', value: function parseInline(text) { - var _this3 = this; + var _this4 = this; var whiteList = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1]; var clearHolders = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2]; @@ -291,15 +289,15 @@ // link text = text.replace(/<(https?:\/\/.+)>/ig, function (match, p1) { - return _this3.makeHolder('' + p1 + ''); + return _this4.makeHolder('' + p1 + ''); }); text = text.replace(/<(\/?)([a-z0-9-]+)(\s+[^>]*)?>/ig, function (match, p1, p2, p3) { - var whiteLists = _this3.commonWhiteList + '|' + whiteList; + var whiteLists = _this4.commonWhiteList + '|' + whiteList; if (whiteLists.toLowerCase().indexOf(p2.toLowerCase()) !== -1) { - return _this3.makeHolder(match); + return _this4.makeHolder(match); } else { - return _this3.htmlspecialchars(match); + return _this4.htmlspecialchars(match); } }); @@ -312,11 +310,11 @@ var id = _this.footnotes.indexOf(p1); if (id === -1) { - id = _this.footnotes.length + 1; - _this.footnotes[id] = _this3.parseInline(p1, '', false); + id = _this.footnotes.length; + _this.footnotes.push(_this4.parseInline(p1, '', false)); } - return _this.makeHolder('' + id + ''); + return _this.makeHolder('' + (id + 1) + ''); }); // image @@ -385,34 +383,34 @@ }, { key: 'parseInlineCallback', value: function parseInlineCallback(text) { - var _this4 = this; + var _this5 = this; text = text.replace(/(\*{3})(.+?)\1/g, function (match, p1, p2) { - return '' + _this4.parseInlineCallback(p2) + ''; + return '' + _this5.parseInlineCallback(p2) + ''; }); text = text.replace(/(\*{2})(.+?)\1/g, function (match, p1, p2) { - return '' + _this4.parseInlineCallback(p2) + ''; + return '' + _this5.parseInlineCallback(p2) + ''; }); text = text.replace(/(\*)(.+?)\1/g, function (match, p1, p2) { - return '' + _this4.parseInlineCallback(p2) + ''; + return '' + _this5.parseInlineCallback(p2) + ''; }); text = text.replace(/(\s+|^)(_{3})(.+?)\2(\s+|$)/g, function (match, p1, p2, p3, p4) { - return p1 + '' + _this4.parseInlineCallback(p3) + '' + p4; + return p1 + '' + _this5.parseInlineCallback(p3) + '' + p4; }); text = text.replace(/(\s+|^)(_{2})(.+?)\2(\s+|$)/g, function (match, p1, p2, p3, p4) { - return p1 + '' + _this4.parseInlineCallback(p3) + '' + p4; + return p1 + '' + _this5.parseInlineCallback(p3) + '' + p4; }); text = text.replace(/(\s+|^)(_)(.+?)\2(\s+|$)/g, function (match, p1, p2, p3, p4) { - return p1 + '' + _this4.parseInlineCallback(p3) + '' + p4; + return p1 + '' + _this5.parseInlineCallback(p3) + '' + p4; }); text = text.replace(/(~{2})(.+?)\1/g, function (match, p1, p2) { - return '' + _this4.parseInlineCallback(p2) + ''; + return '' + _this5.parseInlineCallback(p2) + ''; }); return text; } @@ -427,7 +425,7 @@ }, { key: 'parseBlock', value: function parseBlock(text, lines) { - var _this5 = this; + var _this6 = this; this.blocks = []; this.current = 'normal'; @@ -542,14 +540,14 @@ var tableMatches = /^((?:(?:(?:[ :]*\-[ :]*)+(?:\||\+))|(?:(?:\||\+)(?:[ :]*\-[ :]*)+)|(?:(?:[ :]*\-[ :]*)+(?:\||\+)(?:[ :]*\-[ :]*)+))+)$/g.exec(line); if (this.isBlock('normal')) { (function () { - var block = _this5.getBlock(); + var block = _this6.getBlock(); var head = false; if (block.length === 0 || block[0] !== 'normal' || /^\s*$/.test(lines[block[2]])) { - _this5.startBlock('table', key); + _this6.startBlock('table', key); } else { head = true; - _this5.backBlock(1, 'table'); + _this6.backBlock(1, 'table'); } if (tableMatches[1][0] == '|') { @@ -578,7 +576,7 @@ aligns.push(align); }); - _this5.setBlock(key, [head, aligns]); + _this6.setBlock(key, [head, aligns]); })(); } break; @@ -717,9 +715,9 @@ var types = ['list', 'quote']; if (from === to && lines[from].match(/^\s*$/) && prevBlock && nextBlock) { - if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0] !== -1)) { + if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0]) !== -1) { // combine 3 blocks - blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], NULL]; + blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], null]; blocks.splice(key, 2); } } @@ -770,10 +768,10 @@ }, { key: 'parsePre', value: function parsePre(lines) { - var _this6 = this; + var _this7 = this; lines.forEach(function (line, ind) { - lines[ind] = _this6.htmlspecialchars(line.substr(4)); + lines[ind] = _this7.htmlspecialchars(line.substr(4)); }); var str = lines.join('\n'); @@ -845,7 +843,7 @@ }, { key: 'parseList', value: function parseList(lines) { - var _this7 = this; + var _this8 = this; var html = ''; var minSpace = 99999; @@ -892,7 +890,7 @@ leftLines.push(line.replace(pattern, '')); } else { if (leftLines.length) { - html += "
  • " + _this7.parse(leftLines.join("\n")) + "
  • "; + html += "
  • " + _this8.parse(leftLines.join("\n")) + "
  • "; } if (lastType !== type) { if (lastType.length) { @@ -926,7 +924,7 @@ }, { key: 'parseTable', value: function parseTable(lines, value) { - var _this8 = this; + var _this9 = this; var _value = _slicedToArray(value, 2); @@ -1005,7 +1003,7 @@ html += ' align="' + aligns[key] + '"'; } - html += '>' + _this8.parseInline(text) + (''); + html += '>' + _this9.parseInline(text) + (''); }); html += ''; @@ -1018,9 +1016,9 @@ }; for (var key in lines) { - var _ret2 = _loop(key); + var _ret3 = _loop(key); - if (_ret2 === 'continue') continue; + if (_ret3 === 'continue') continue; } if (body !== null) { @@ -1051,10 +1049,10 @@ }, { key: 'parseNormal', value: function parseNormal(lines) { - var _this9 = this; + var _this10 = this; lines = lines.map(function (line) { - return _this9.parseInline(line); + return _this10.parseInline(line); }); var str = lines.join("\n").trim(); @@ -1112,10 +1110,10 @@ }, { key: 'parseHtml', value: function parseHtml(lines, type) { - var _this10 = this; + var _this11 = this; lines.forEach(function (line) { - line = _this10.parseInline(line, _this10.specialWhiteList[type] ? _this10.specialWhiteList[type] : ''); + line = _this11.parseInline(line, _this11.specialWhiteList[type] ? _this11.specialWhiteList[type] : ''); }); return lines.join("\n"); diff --git a/src/Parser.js b/src/Parser.js index 2cc8896..9220d20 100644 --- a/src/Parser.js +++ b/src/Parser.js @@ -83,8 +83,7 @@ export default class Parser { if (this.footnotes.length > 0) { html += '

      ' let index = 1 - let val = this.footnotes.pop() - while (val) { + this.footnotes.forEach( val => { if (typeof val === 'string') { val += ` ` } else { @@ -93,10 +92,8 @@ export default class Parser { } html += `
    1. ${val}
    2. ` - index++ - val = this.footnotes.pop() - } + }) html += '
    ' } return html @@ -135,19 +132,20 @@ export default class Parser { * @param value * @return mixed */ - call (type, ...value) { + call (type, value) { if (!this.hooks[type]) { - return value[0] + return value } - let args = value + let args = [].slice.call(arguments) + args = args.slice(1) this.hooks[type].forEach (callback => { - value = callback(args) + value = callback.apply(null, args) args[0] = value }) - return value[0] + return value } /** @@ -209,11 +207,11 @@ export default class Parser { let id = _this.footnotes.indexOf(p1) if (id === -1) { - id = _this.footnotes.length + 1 - _this.footnotes[id] = this.parseInline(p1, '', false) + id = _this.footnotes.length + _this.footnotes.push(this.parseInline(p1, '', false)) } - return _this.makeHolder(`${id}`) + return _this.makeHolder(`${id+1}`) }) // image @@ -608,9 +606,9 @@ export default class Parser { if (from === to && lines[from].match(/^\s*$/) && prevBlock && nextBlock) { - if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0] !== -1)) { + if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0]) !== -1) { // combine 3 blocks - blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], NULL]; + blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], null]; blocks.splice(key, 2) } }