Skip to content

Commit

Permalink
修复多级quote
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiang Shang committed Nov 5, 2015
1 parent 2408abd commit 2116942
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 42 deletions.
38 changes: 23 additions & 15 deletions dist/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,10 @@ var Parser = (function () {
// normal
default:
if (this.isBlock('list')) {
var _matches = line.match(/^(\s*)/);

if (line.length == _matches[1].length) {
// let matches = line.match(/^(\s*)/)
//
// if (line.length == matches[1].length) { // empty line
if (/^(\s*)/.test(line)) {
// empty line
if (emptyCount > 0) {
this.startBlock('normal', key);
Expand All @@ -587,9 +588,9 @@ var Parser = (function () {
this.startBlock('normal', key);
}
} else if (this.isBlock('footnote')) {
var _matches2 = line.match(/^(\s*)/);
var _matches = line.match(/^(\s*)/);

if (_matches2[1].length >= this.getBlock()[3][0]) {
if (_matches[1].length >= this.getBlock()[3][0]) {
this.setBlock(key);
} else {
this.startBlock('normal', key);
Expand All @@ -613,10 +614,19 @@ var Parser = (function () {
this.startBlock('normal', key);
}
} else if (this.isBlock('quote')) {
if (/^\s*$/.test(line)) {
this.startBlock('normal', key);
} else {
if (/^(\s*)/.test(line)) {
// empty line
if (emptyCount > 0) {
this.startBlock('normal', key);
} else {
this.setBlock(key);
}

emptyCount++;
} else if ($emptyCount == 0) {
this.setBlock(key);
} else {
this.startBlock('normal', key);
}
} else {
var block = this.getBlock();
Expand Down Expand Up @@ -664,11 +674,13 @@ var Parser = (function () {
}

if ('normal' === type) {
// combine two splitted list
// combine two blocks
var types = ['list', 'quote'];

if (from === to && lines[from].match(/^\s*$/) && prevBlock && nextBlock) {
if (prevBlock[0] === 'list' && nextBlock[0] === 'list') {
if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0] !== -1)) {
// combine 3 blocks
blocks[key - 1] = ['list', prevBlock[1], nextBlock[2], null];
blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], NULL];
blocks.splice(key, 2);
}
}
Expand Down Expand Up @@ -777,7 +789,6 @@ var Parser = (function () {
}, {
key: 'parseQuote',
value: function parseQuote(lines) {
console.log(lines);
lines.forEach(function (line, key) {
lines[key] = line.replace(/^\s*> ?/, '');
});
Expand Down Expand Up @@ -1248,7 +1259,4 @@ var Parser = (function () {
})();

exports['default'] = Parser;

var parser = new Parser();
console.log(parser.makeHtml('>1234\n1234'));
module.exports = exports['default'];
38 changes: 23 additions & 15 deletions hyperdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,10 @@
// normal
default:
if (this.isBlock('list')) {
var _matches = line.match(/^(\s*)/);

if (line.length == _matches[1].length) {
// let matches = line.match(/^(\s*)/)
//
// if (line.length == matches[1].length) { // empty line
if (/^(\s*)/.test(line)) {
// empty line
if (emptyCount > 0) {
this.startBlock('normal', key);
Expand All @@ -633,9 +634,9 @@
this.startBlock('normal', key);
}
} else if (this.isBlock('footnote')) {
var _matches2 = line.match(/^(\s*)/);
var _matches = line.match(/^(\s*)/);

if (_matches2[1].length >= this.getBlock()[3][0]) {
if (_matches[1].length >= this.getBlock()[3][0]) {
this.setBlock(key);
} else {
this.startBlock('normal', key);
Expand All @@ -659,10 +660,19 @@
this.startBlock('normal', key);
}
} else if (this.isBlock('quote')) {
if (/^\s*$/.test(line)) {
this.startBlock('normal', key);
} else {
if (/^(\s*)/.test(line)) {
// empty line
if (emptyCount > 0) {
this.startBlock('normal', key);
} else {
this.setBlock(key);
}

emptyCount++;
} else if ($emptyCount == 0) {
this.setBlock(key);
} else {
this.startBlock('normal', key);
}
} else {
var block = this.getBlock();
Expand Down Expand Up @@ -710,11 +720,13 @@
}

if ('normal' === type) {
// combine two splitted list
// combine two blocks
var types = ['list', 'quote'];

if (from === to && lines[from].match(/^\s*$/) && prevBlock && nextBlock) {
if (prevBlock[0] === 'list' && nextBlock[0] === 'list') {
if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0] !== -1)) {
// combine 3 blocks
blocks[key - 1] = ['list', prevBlock[1], nextBlock[2], null];
blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], NULL];
blocks.splice(key, 2);
}
}
Expand Down Expand Up @@ -823,7 +835,6 @@
}, {
key: 'parseQuote',
value: function parseQuote(lines) {
console.log(lines);
lines.forEach(function (line, key) {
lines[key] = line.replace(/^\s*> ?/, '');
});
Expand Down Expand Up @@ -1294,9 +1305,6 @@
})();

exports['default'] = Parser;

var parser = new Parser();
console.log(parser.makeHtml('>1234\n1234'));
module.exports = exports['default'];

/***/ },
Expand Down
32 changes: 20 additions & 12 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,10 @@ export default class Parser {
// normal
default:
if (this.isBlock('list')) {
let matches = line.match(/^(\s*)/)

if (line.length == matches[1].length) { // empty line
// let matches = line.match(/^(\s*)/)
//
// if (line.length == matches[1].length) { // empty line
if (/^(\s*)/.test(line)) { // empty line
if (emptyCount > 0) {
this.startBlock('normal', key)
} else {
Expand Down Expand Up @@ -556,10 +557,18 @@ export default class Parser {
this.startBlock('normal', key)
}
} else if (this.isBlock('quote')) {
if (/^\s*$/.test(line)) {
this.startBlock('normal', key)
} else {
if (/^(\s*)/.test(line)) { // empty line
if (emptyCount > 0) {
this.startBlock('normal', key)
} else {
this.setBlock(key)
}

emptyCount ++
} else if ($emptyCount == 0) {
this.setBlock(key)
} else {
this.startBlock('normal', key)
}
} else {
let block = this.getBlock()
Expand Down Expand Up @@ -601,12 +610,14 @@ export default class Parser {
}

if ('normal' === type) {
// combine two splitted list
// combine two blocks
let types = ['list', 'quote']

if (from === to && lines[from].match(/^\s*$/)
&& prevBlock && nextBlock) {
if (prevBlock[0] === 'list' && nextBlock[0] === 'list') {
if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0] !== -1)) {
// combine 3 blocks
blocks[key - 1] = ['list', prevBlock[1], nextBlock[2], null]
blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], NULL];
blocks.splice(key, 2)
}
}
Expand Down Expand Up @@ -697,7 +708,6 @@ export default class Parser {
* @return string
*/
parseQuote(lines) {
console.log(lines)
lines.forEach( (line, key) => {
lines[key] = line.replace(/^\s*> ?/, '')
})
Expand Down Expand Up @@ -1089,5 +1099,3 @@ export default class Parser {
return this
}
}
var parser = new Parser()
console.log(parser.makeHtml('>1234\n1234'))

0 comments on commit 2116942

Please sign in to comment.