Skip to content

Commit

Permalink
Fix #464, Fix #465
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Oct 12, 2024
1 parent fa88006 commit 697e44c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
22 changes: 22 additions & 0 deletions _test/extra.txt
Original file line number Diff line number Diff line change
Expand Up @@ -809,3 +809,25 @@ text" /></p>
</ul>
//= = = = = = = = = = = = = = = = = = = = = = = =//

65: Nested fenced code block with tab
//- - - - - - - - -//
> ```
> 0
> ```
//- - - - - - - - -//
<blockquote>
<pre><code> 0
</code></pre>
</blockquote>
//= = = = = = = = = = = = = = = = = = = = = = = =//

66: EOF should be rendered as a newline with an unclosed block
//- - - - - - - - -//
> ```
> 0
//- - - - - - - - -//
<blockquote>
<pre><code> 0
</code></pre>
</blockquote>
//= = = = = = = = = = = = = = = = = = = = = = = =//
11 changes: 6 additions & 5 deletions parser/blockquote.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ func (b *blockquoteParser) process(reader text.Reader) bool {
reader.Advance(pos)
return true
}
if line[pos] == ' ' || line[pos] == '\t' {
pos++
}
reader.Advance(pos)
if line[pos-1] == '\t' {
reader.SetPadding(2)
if line[pos] == ' ' || line[pos] == '\t' {
padding := 0
if line[pos] == '\t' {
padding = util.TabWidth(reader.LineOffset()) - 1
}
reader.AdvanceAndSetPadding(1, padding)
}
return true
}
Expand Down
8 changes: 8 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,10 +878,17 @@ func (p *parser) Parse(reader text.Reader, opts ...ParseOption) ast.Node {
blockReader := text.NewBlockReader(reader.Source(), nil)
p.walkBlock(root, func(node ast.Node) {
p.parseBlock(blockReader, node, pc)
lines := node.Lines()
if lines != nil && lines.Len() != 0 {
s := lines.At(lines.Len() - 1)
s.EOB = true
lines.Set(lines.Len()-1, s)
}
})
for _, at := range p.astTransformers {
at.Transform(root, reader, pc)
}

// root.Dump(reader.Source(), 0)
return root
}
Expand Down Expand Up @@ -1256,4 +1263,5 @@ func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context)
for _, ip := range p.closeBlockers {
ip.CloseBlock(parent, block, pc)
}

}
10 changes: 9 additions & 1 deletion text/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package text

import (
"bytes"

"github.com/yuin/goldmark/util"
)

Expand All @@ -18,6 +19,9 @@ type Segment struct {

// Padding is a padding length of the segment.
Padding int

// EOB is true if the segment is end of the block.
EOB bool
}

// NewSegment return a new Segment.
Expand Down Expand Up @@ -45,7 +49,11 @@ func (t *Segment) Value(buffer []byte) []byte {
}
result := make([]byte, 0, t.Padding+t.Stop-t.Start+1)
result = append(result, bytes.Repeat(space, t.Padding)...)
return append(result, buffer[t.Start:t.Stop]...)
result = append(result, buffer[t.Start:t.Stop]...)
if t.EOB && len(result) > 0 && result[len(result)-1] != '\n' {
result = append(result, '\n')
}
return result
}

// Len returns a length of the segment.
Expand Down
6 changes: 6 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,13 @@ func IndentPositionPadding(bs []byte, currentPos, paddingv, width int) (pos, pad
w := 0
i := 0
l := len(bs)
p := paddingv
for ; i < l; i++ {
if p > 0 {
p--
w++
continue
}
if bs[i] == '\t' && w < width {
w += TabWidth(currentPos + w)
} else if bs[i] == ' ' && w < width {
Expand Down

0 comments on commit 697e44c

Please sign in to comment.