@@ -295,6 +295,7 @@ export default class Parser {
295
295
let emptyCount = 0
296
296
// analyze by line
297
297
for ( let key in lines ) {
298
+ key = parseInt ( key ) // ES6 的 bug for key in Array 循环时返回的 key 是字符串,不是 int
298
299
let line = lines [ key ]
299
300
// code block is special
300
301
if ( matches = line . match ( / ^ ( \s * ) ( ~ | ` ) { 3 , } ( [ ^ ` ~ ] * ) $ / i) ) {
@@ -353,8 +354,9 @@ export default class Parser {
353
354
// list
354
355
case / ^ ( \s * ) ( (?: [ 0 - 9 a - z ] \. ) | \- | \+ | \* ) \s + / . test ( line ) :
355
356
let matches = line . match ( / ^ ( \s * ) ( (?: [ 0 - 9 a - z ] \. ) | \- | \+ | \* ) \s + / )
357
+
356
358
let listSpace = matches [ 1 ] . length
357
- let emptyCount = 0
359
+ emptyCount = 0
358
360
359
361
// opened
360
362
if ( this . isBlock ( 'list' ) ) {
@@ -481,7 +483,7 @@ export default class Parser {
481
483
}
482
484
483
485
emptyCount ++
484
- } else if ( emptyCount == 0 ) {
486
+ } else if ( emptyCount === 0 ) {
485
487
this . setBlock ( key )
486
488
} else {
487
489
this . startBlock ( 'normal' , key )
@@ -514,7 +516,7 @@ export default class Parser {
514
516
}
515
517
} else {
516
518
let block = this . getBlock ( )
517
- if ( ! block || ! block . length || block [ 0 ] !== 'normal' ) {
519
+ if ( block === null || block . length === 0 || block [ 0 ] !== 'normal' ) {
518
520
this . startBlock ( 'normal' , key )
519
521
} else {
520
522
this . setBlock ( key )
@@ -535,7 +537,7 @@ export default class Parser {
535
537
optimizeBlocks ( blocks , lines ) {
536
538
blocks = this . call ( 'beforeOptimizeBlocks' , blocks , lines )
537
539
538
- blocks . forEach ( function ( block , key ) {
540
+ blocks . forEach ( ( block , key ) => {
539
541
let prevBlock = blocks [ key - 1 ] ? blocks [ key - 1 ] : null
540
542
let nextBlock = blocks [ key + 1 ] ? blocks [ key + 1 ] : null
541
543
@@ -552,13 +554,13 @@ export default class Parser {
552
554
}
553
555
554
556
if ( 'normal' === type ) {
555
- // one sigle empty line
557
+ // combine two splitted list
556
558
if ( from === to && lines [ from ] . match ( / ^ \s * $ / )
557
- && prevBlock && nextBlock ) {
559
+ && prevBlock . length && nextBlock . length ) {
558
560
if ( prevBlock [ 0 ] === 'list' && nextBlock [ 0 ] === 'list' ) {
559
561
// combine 3 blocks
560
562
blocks [ key - 1 ] = [ 'list' , prevBlock [ 1 ] , nextBlock [ 2 ] , null ]
561
- array_splice ( blocks , key , 2 )
563
+ blocks . splice ( key , 2 )
562
564
}
563
565
}
564
566
}
@@ -840,11 +842,11 @@ export default class Parser {
840
842
* @return string
841
843
*/
842
844
parseNormal ( lines ) {
843
- lines . forEach ( ( line , key ) => {
844
- lines [ key ] = this . parseInline ( line )
845
+ lines = lines . map ( line => {
846
+ return this . parseInline ( line )
845
847
} )
846
848
847
- let str = lines . join ( "\n" )
849
+ let str = lines . join ( "\n" ) . trim ( )
848
850
str = str . replace ( / ( \n \s * ) { 2 , } / , "</p><p>" )
849
851
str = str . replace ( / \n / , "<br>" )
850
852
0 commit comments