Skip to content

Commit 2116942

Browse files
author
Jiang Shang
committed
修复多级quote
1 parent 2408abd commit 2116942

File tree

3 files changed

+66
-42
lines changed

3 files changed

+66
-42
lines changed

dist/Parser.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,10 @@ var Parser = (function () {
570570
// normal
571571
default:
572572
if (this.isBlock('list')) {
573-
var _matches = line.match(/^(\s*)/);
574-
575-
if (line.length == _matches[1].length) {
573+
// let matches = line.match(/^(\s*)/)
574+
//
575+
// if (line.length == matches[1].length) { // empty line
576+
if (/^(\s*)/.test(line)) {
576577
// empty line
577578
if (emptyCount > 0) {
578579
this.startBlock('normal', key);
@@ -587,9 +588,9 @@ var Parser = (function () {
587588
this.startBlock('normal', key);
588589
}
589590
} else if (this.isBlock('footnote')) {
590-
var _matches2 = line.match(/^(\s*)/);
591+
var _matches = line.match(/^(\s*)/);
591592

592-
if (_matches2[1].length >= this.getBlock()[3][0]) {
593+
if (_matches[1].length >= this.getBlock()[3][0]) {
593594
this.setBlock(key);
594595
} else {
595596
this.startBlock('normal', key);
@@ -613,10 +614,19 @@ var Parser = (function () {
613614
this.startBlock('normal', key);
614615
}
615616
} else if (this.isBlock('quote')) {
616-
if (/^\s*$/.test(line)) {
617-
this.startBlock('normal', key);
618-
} else {
617+
if (/^(\s*)/.test(line)) {
618+
// empty line
619+
if (emptyCount > 0) {
620+
this.startBlock('normal', key);
621+
} else {
622+
this.setBlock(key);
623+
}
624+
625+
emptyCount++;
626+
} else if ($emptyCount == 0) {
619627
this.setBlock(key);
628+
} else {
629+
this.startBlock('normal', key);
620630
}
621631
} else {
622632
var block = this.getBlock();
@@ -664,11 +674,13 @@ var Parser = (function () {
664674
}
665675

666676
if ('normal' === type) {
667-
// combine two splitted list
677+
// combine two blocks
678+
var types = ['list', 'quote'];
679+
668680
if (from === to && lines[from].match(/^\s*$/) && prevBlock && nextBlock) {
669-
if (prevBlock[0] === 'list' && nextBlock[0] === 'list') {
681+
if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0] !== -1)) {
670682
// combine 3 blocks
671-
blocks[key - 1] = ['list', prevBlock[1], nextBlock[2], null];
683+
blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], NULL];
672684
blocks.splice(key, 2);
673685
}
674686
}
@@ -777,7 +789,6 @@ var Parser = (function () {
777789
}, {
778790
key: 'parseQuote',
779791
value: function parseQuote(lines) {
780-
console.log(lines);
781792
lines.forEach(function (line, key) {
782793
lines[key] = line.replace(/^\s*> ?/, '');
783794
});
@@ -1248,7 +1259,4 @@ var Parser = (function () {
12481259
})();
12491260

12501261
exports['default'] = Parser;
1251-
1252-
var parser = new Parser();
1253-
console.log(parser.makeHtml('>1234\n1234'));
12541262
module.exports = exports['default'];

hyperdown.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,10 @@
616616
// normal
617617
default:
618618
if (this.isBlock('list')) {
619-
var _matches = line.match(/^(\s*)/);
620-
621-
if (line.length == _matches[1].length) {
619+
// let matches = line.match(/^(\s*)/)
620+
//
621+
// if (line.length == matches[1].length) { // empty line
622+
if (/^(\s*)/.test(line)) {
622623
// empty line
623624
if (emptyCount > 0) {
624625
this.startBlock('normal', key);
@@ -633,9 +634,9 @@
633634
this.startBlock('normal', key);
634635
}
635636
} else if (this.isBlock('footnote')) {
636-
var _matches2 = line.match(/^(\s*)/);
637+
var _matches = line.match(/^(\s*)/);
637638

638-
if (_matches2[1].length >= this.getBlock()[3][0]) {
639+
if (_matches[1].length >= this.getBlock()[3][0]) {
639640
this.setBlock(key);
640641
} else {
641642
this.startBlock('normal', key);
@@ -659,10 +660,19 @@
659660
this.startBlock('normal', key);
660661
}
661662
} else if (this.isBlock('quote')) {
662-
if (/^\s*$/.test(line)) {
663-
this.startBlock('normal', key);
664-
} else {
663+
if (/^(\s*)/.test(line)) {
664+
// empty line
665+
if (emptyCount > 0) {
666+
this.startBlock('normal', key);
667+
} else {
668+
this.setBlock(key);
669+
}
670+
671+
emptyCount++;
672+
} else if ($emptyCount == 0) {
665673
this.setBlock(key);
674+
} else {
675+
this.startBlock('normal', key);
666676
}
667677
} else {
668678
var block = this.getBlock();
@@ -710,11 +720,13 @@
710720
}
711721

712722
if ('normal' === type) {
713-
// combine two splitted list
723+
// combine two blocks
724+
var types = ['list', 'quote'];
725+
714726
if (from === to && lines[from].match(/^\s*$/) && prevBlock && nextBlock) {
715-
if (prevBlock[0] === 'list' && nextBlock[0] === 'list') {
727+
if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0] !== -1)) {
716728
// combine 3 blocks
717-
blocks[key - 1] = ['list', prevBlock[1], nextBlock[2], null];
729+
blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], NULL];
718730
blocks.splice(key, 2);
719731
}
720732
}
@@ -823,7 +835,6 @@
823835
}, {
824836
key: 'parseQuote',
825837
value: function parseQuote(lines) {
826-
console.log(lines);
827838
lines.forEach(function (line, key) {
828839
lines[key] = line.replace(/^\s*> ?/, '');
829840
});
@@ -1294,9 +1305,6 @@
12941305
})();
12951306

12961307
exports['default'] = Parser;
1297-
1298-
var parser = new Parser();
1299-
console.log(parser.makeHtml('>1234\n1234'));
13001308
module.exports = exports['default'];
13011309

13021310
/***/ },

src/Parser.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,10 @@ export default class Parser {
514514
// normal
515515
default:
516516
if (this.isBlock('list')) {
517-
let matches = line.match(/^(\s*)/)
518-
519-
if (line.length == matches[1].length) { // empty line
517+
// let matches = line.match(/^(\s*)/)
518+
//
519+
// if (line.length == matches[1].length) { // empty line
520+
if (/^(\s*)/.test(line)) { // empty line
520521
if (emptyCount > 0) {
521522
this.startBlock('normal', key)
522523
} else {
@@ -556,10 +557,18 @@ export default class Parser {
556557
this.startBlock('normal', key)
557558
}
558559
} else if (this.isBlock('quote')) {
559-
if (/^\s*$/.test(line)) {
560-
this.startBlock('normal', key)
561-
} else {
560+
if (/^(\s*)/.test(line)) { // empty line
561+
if (emptyCount > 0) {
562+
this.startBlock('normal', key)
563+
} else {
564+
this.setBlock(key)
565+
}
566+
567+
emptyCount ++
568+
} else if ($emptyCount == 0) {
562569
this.setBlock(key)
570+
} else {
571+
this.startBlock('normal', key)
563572
}
564573
} else {
565574
let block = this.getBlock()
@@ -601,12 +610,14 @@ export default class Parser {
601610
}
602611

603612
if ('normal' === type) {
604-
// combine two splitted list
613+
// combine two blocks
614+
let types = ['list', 'quote']
615+
605616
if (from === to && lines[from].match(/^\s*$/)
606617
&& prevBlock && nextBlock) {
607-
if (prevBlock[0] === 'list' && nextBlock[0] === 'list') {
618+
if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0] !== -1)) {
608619
// combine 3 blocks
609-
blocks[key - 1] = ['list', prevBlock[1], nextBlock[2], null]
620+
blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], NULL];
610621
blocks.splice(key, 2)
611622
}
612623
}
@@ -697,7 +708,6 @@ export default class Parser {
697708
* @return string
698709
*/
699710
parseQuote(lines) {
700-
console.log(lines)
701711
lines.forEach( (line, key) => {
702712
lines[key] = line.replace(/^\s*> ?/, '')
703713
})
@@ -1089,5 +1099,3 @@ export default class Parser {
10891099
return this
10901100
}
10911101
}
1092-
var parser = new Parser()
1093-
console.log(parser.makeHtml('>1234\n1234'))

0 commit comments

Comments
 (0)