8000 Merge branch 'fix-bug' into 'master' · segmentfault/HyperDown.js@8cbab48 · GitHub
[go: up one dir, main page]

Skip to content 10000

Commit 8cbab48

Browse files
author
Ren Yubin
committed
Merge branch 'fix-bug' into 'master'
fix: improve table parsing regex in Parser.js and Parser.coffee See merge request segmentfault/HyperDown.js!3
2 parents 9a842a8 + 0a03b0c commit 8cbab48

File tree

3 files changed

+70
-67
lines changed

3 files changed

+70
-67
lines changed

Parser.js

Lines changed: 36 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"license": "BSD-4-Clause",
99
"author": "SegmentFault",
10-
"version": "2.4.30",
10+
"version": "2.4.31",
1111
"scripts": {
1212
"test": "mocha",
1313
"build": "cake build"

src/Parser.coffee

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -613,45 +613,46 @@ class Parser
613613

614614

615615
parseBlockTable: (block, key, line, state, lines) ->
616-
if !!(matches = line.match /^\s*(\|?[ :]*-+[ :]*(?:\|[ :]*-+[ :]*)*\|?)\s*$/)
617-
if @isBlock 'table'
618-
block[3][0].push block[3][2]
619-
block[3][2] += 1
620-
@setBlock key, block[3]
621-
else
622-
head = 0
623-
624-
if not block? or block[0] != 'normal' or lines[block[2]].match /^\s*$/
625-
@startBlock 'table', key
616+
if !!(matches = line.match /^\s*(\|?[ :]*-{2,}[ :]*(?:[\|\+][ :]*-{2,}[ :]*)*\|?)\s*$/)
617+
if matches[1].indexOf('|') >= 0 or matches[1].indexOf('+') >= 0
618+
if @isBlock 'table'
619+
block[3][0].push block[3][2]
620+
block[3][2] += 1
621+
@setBlock key, block[3]
626622
else
627-
head = 1
628-
@backBlock 1, 'table'
623+
head = 0
629624

630-
if matches[1][0] == '|'
631-
matches[1] = matches[1].substring 1
625+
if not block? or block[0] != 'normal' or lines[block[2]].match /^\s*$/
626+
@startBlock 'table', key
627+
else
628+
head = 1
629+
@backBlock 1, 'table'
632630

633-
if matches[1][matches[1].length - 1] == '|'
634-
matches[1] = matches[1].substring 0, matches[1].length - 1
631+
if matches[1][0] == '|'
632+
matches[1] = matches[1].substring 1
635633

636-
rows = matches[1].split /\+|\|/
637-
aligns = []
634+
if matches[1][matches[1].length - 1] == '|'
635+
matches[1] = matches[1].substring 0, matches[1].length - 1
638636

639-
for row in rows
640-
align = 'none'
637+
rows = matches[1].split /\+|\|/
638+
aligns = []
641639

642-
if !!(matches = row.match /^\s*(:?)\-+(:?)\s*$/)
643-
if !!matches[1] && !!matches[2]
644-
align = 'center'
645-
else if !!matches[1]
646-
align = 'left'
647-
else if !!matches[2]
648-
align = 'right'
640+
for row in rows
641+
align = 'none'
649642

650-
aligns.push align
643+
if !!(matches = row.match /^\s*(:?)\-+(:?)\s*$/)
644+
if !!matches[1] && !!matches[2]
645+
align = 'center'
646+
else if !!matches[1]
647+
align = 'left'
648+
else if !!matches[2]
649+
align = 'right'
651650

652-
@setBlock key, [[head], aligns, head + 1]
651+
aligns.push align
653652

654-
return no
653+
@setBlock key, [[head], aligns, head + 1]
654+
655+
return no
655656

656657
yes
657658

@@ -682,7 +683,7 @@ class Parser
682683

683684

684685
parseBlockShr: (block, key, line) ->
685-
if !!(line.match /^(\* *){3,}\s*$/)
686+
if !!(line.match /^\*{3,}\s*$/)
686687
@startBlock 'hr', key
687688
.endBlock()
688689

@@ -692,7 +693,7 @@ class Parser
692693

693694

694695
parseBlockDhr: (block, key, line) ->
695-
if !!(line.match /^(- *){3,}\s*$/)
696+
if !!(line.match /^-{3,}\s*$/)
696697
@startBlock 'hr', key
697698
.endBlock()
698699

0 commit comments

Comments
 (0)
0