Skip to content

Commit

Permalink
Fix #333
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Nov 12, 2022
1 parent aaeb985 commit a87c577
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
27 changes: 27 additions & 0 deletions extension/_test/table.txt
Expand Up @@ -253,3 +253,30 @@ foo|bar
</thead>
</table>
//= = = = = = = = = = = = = = = = = = = = = = = =//

12: A delimiter can not start with more than 3 spaces
//- - - - - - - - -//
Foo
---
//- - - - - - - - -//
<p>Foo
---</p>
//= = = = = = = = = = = = = = = = = = = = = = = =//

13: A delimiter can not start with more than 3 spaces(w/ tabs)
OPTIONS: {"enableEscape": true}
//- - - - - - - - -//
- aaa

Foo
\t\t---
//- - - - - - - - -//
<ul>
<li>
<p>aaa</p>
<p>Foo
---</p>
</li>
</ul>
//= = = = = = = = = = = = = = = = = = = = = = = =//

4 changes: 4 additions & 0 deletions extension/table.go
Expand Up @@ -122,6 +122,9 @@ func WithTableCellAlignMethod(a TableCellAlignMethod) TableOption {
}

func isTableDelim(bs []byte) bool {
if w, _ := util.IndentWidth(bs, 0); w > 3 {
return false
}
for _, b := range bs {
if !(util.IsSpace(b) || b == '-' || b == '|' || b == ':') {
return false
Expand Down Expand Up @@ -243,6 +246,7 @@ func (b *tableParagraphTransformer) parseRow(segment text.Segment, alignments []
}

func (b *tableParagraphTransformer) parseDelimiter(segment text.Segment, reader text.Reader) []ast.Alignment {

line := segment.Value(reader.Source())
if !isTableDelim(line) {
return nil
Expand Down
17 changes: 9 additions & 8 deletions parser/paragraph.go
Expand Up @@ -3,6 +3,7 @@ package parser
import (
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)

type paragraphParser struct {
Expand Down Expand Up @@ -33,9 +34,8 @@ func (b *paragraphParser) Open(parent ast.Node, reader text.Reader, pc Context)
}

func (b *paragraphParser) Continue(node ast.Node, reader text.Reader, pc Context) State {
_, segment := reader.PeekLine()
segment = segment.TrimLeftSpace(reader.Source())
if segment.IsEmpty() {
line, segment := reader.PeekLine()
if util.IsBlank(line) {
return Close
}
node.Lines().Append(segment)
Expand All @@ -44,13 +44,14 @@ func (b *paragraphParser) Continue(node ast.Node, reader text.Reader, pc Context
}

func (b *paragraphParser) Close(node ast.Node, reader text.Reader, pc Context) {
parent := node.Parent()
if parent == nil {
// paragraph has been transformed
return
}
lines := node.Lines()
if lines.Len() != 0 {
// trim leading spaces
for i := 0; i < lines.Len(); i++ {
l := lines.At(i)
lines.Set(i, l.TrimLeftSpace(reader.Source()))
}

// trim trailing spaces
length := lines.Len()
lastLine := node.Lines().At(length - 1)
Expand Down
4 changes: 3 additions & 1 deletion parser/parser.go
Expand Up @@ -899,11 +899,13 @@ func (p *parser) closeBlocks(from, to int, reader text.Reader, pc Context) {
blocks := pc.OpenedBlocks()
for i := from; i >= to; i-- {
node := blocks[i].Node
blocks[i].Parser.Close(blocks[i].Node, reader, pc)
paragraph, ok := node.(*ast.Paragraph)
if ok && node.Parent() != nil {
p.transformParagraph(paragraph, reader, pc)
}
if node.Parent() != nil { // closes only if node has not been transformed
blocks[i].Parser.Close(blocks[i].Node, reader, pc)
}
}
if from == len(blocks)-1 {
blocks = blocks[0:to]
Expand Down

0 comments on commit a87c577

Please sign in to comment.