Skip to content

Commit ad15651

Browse files
committed
Fix #470
1 parent bc993b4 commit ad15651

File tree

10 files changed

+387
-82
lines changed

10 files changed

+387
-82
lines changed

ast/ast.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,11 @@ func (n *BaseNode) Text(source []byte) []byte {
379379
var buf bytes.Buffer
380380
for c := n.firstChild; c != nil; c = c.NextSibling() {
381381
buf.Write(c.Text(source))
382+
if sb, ok := c.(interface {
383+
SoftLineBreak() bool
384+
}); ok && sb.SoftLineBreak() {
385+
buf.WriteByte('\n')
386+
}
382387
}
383388
return buf.Bytes()
384389
}

ast/ast_test.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
11
package ast
22

33
import (
4-
"bytes"
54
"reflect"
65
"testing"
7-
8-
"github.com/yuin/goldmark/text"
96
)
107

11-
func TestRemoveChildren(t *testing.T) {
12-
root := NewDocument()
13-
14-
node1 := NewDocument()
15-
16-
node2 := NewDocument()
17-
18-
root.AppendChild(root, node1)
19-
root.AppendChild(root, node2)
20-
21-
root.RemoveChildren(root)
22-
23-
t.Logf("%+v", node2.PreviousSibling())
24-
}
25-
268
func TestWalk(t *testing.T) {
279
tests := []struct {
2810
name string
@@ -76,48 +58,3 @@ func node(n Node, children ...Node) Node {
7658
}
7759
return n
7860
}
79-
80-
func TestBaseBlock_Text(t *testing.T) {
81-
source := []byte(`# Heading
82-
83-
code block here
84-
and also here
85-
86-
A paragraph
87-
88-
` + "```" + `somelang
89-
fenced code block
90-
` + "```" + `
91-
92-
The end`)
93-
94-
t.Run("fetch text from code block", func(t *testing.T) {
95-
block := NewCodeBlock()
96-
block.lines = text.NewSegments()
97-
block.lines.Append(text.Segment{Start: 15, Stop: 31})
98-
block.lines.Append(text.Segment{Start: 32, Stop: 46})
99-
100-
expected := []byte("code block here\nand also here\n")
101-
if !bytes.Equal(expected, block.Text(source)) {
102-
t.Errorf("Expected: %q, got: %q", string(expected), string(block.Text(source)))
103-
}
104-
})
105-
106-
t.Run("fetch text from fenced code block", func(t *testing.T) {
107-
block := NewFencedCodeBlock(&Text{
108-
Segment: text.Segment{Start: 63, Stop: 71},
109-
})
110-
block.lines = text.NewSegments()
111-
block.lines.Append(text.Segment{Start: 72, Stop: 90})
112-
113-
expectedLang := []byte("somelang")
114-
if !bytes.Equal(expectedLang, block.Language(source)) {
115-
t.Errorf("Expected: %q, got: %q", string(expectedLang), string(block.Language(source)))
116-
}
117-
118-
expected := []byte("fenced code block\n")
119-
if !bytes.Equal(expected, block.Text(source)) {
120-
t.Errorf("Expected: %q, got: %q", string(expected), string(block.Text(source)))
121-
}
122-
})
123-
}

ast/block.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ast
22

33
import (
4-
"bytes"
54
"fmt"
65
"strings"
76

@@ -48,15 +47,6 @@ func (b *BaseBlock) SetLines(v *textm.Segments) {
4847
b.lines = v
4948
}
5049

51-
// Text implements Node.Text.
52-
func (b *BaseBlock) Text(source []byte) []byte {
53-
var buf bytes.Buffer
54-
for _, line := range b.Lines().Sliced(0, b.Lines().Len()) {
55-
buf.Write(line.Value(source))
56-
}
57-
return buf.Bytes()
58-
}
59-
6050
// A Document struct is a root node of Markdown text.
6151
type Document struct {
6252
BaseBlock
@@ -140,6 +130,11 @@ func (n *TextBlock) Kind() NodeKind {
140130
return KindTextBlock
141131
}
142132

133+
// Text implements Node.Text.
134+
func (n *TextBlock) Text(source []byte) []byte {
135+
return n.Lines().Value(source)
136+
}
137+
143138
// NewTextBlock returns a new TextBlock node.
144139
func NewTextBlock() *TextBlock {
145140
return &TextBlock{
@@ -165,6 +160,11 @@ func (n *Paragraph) Kind() NodeKind {
165160
return KindParagraph
166161
}
167162

163+
// Text implements Node.Text.
164+
func (n *Paragraph) Text(source []byte) []byte {
165+
return n.Lines().Value(source)
166+
}
167+
168168
// NewParagraph returns a new Paragraph node.
169169
func NewParagraph() *Paragraph {
170170
return &Paragraph{
@@ -259,6 +259,11 @@ func (n *CodeBlock) Kind() NodeKind {
259259
return KindCodeBlock
260260
}
261261

262+
// Text implements Node.Text.
263+
func (n *CodeBlock) Text(source []byte) []byte {
264+
return n.Lines().Value(source)
265+
}
266+
262267
// NewCodeBlock returns a new CodeBlock node.
263268
func NewCodeBlock() *CodeBlock {
264269
return &CodeBlock{
@@ -314,6 +319,11 @@ func (n *FencedCodeBlock) Kind() NodeKind {
314319
return KindFencedCodeBlock
315320
}
316321

322+
// Text implements Node.Text.
323+
func (n *FencedCodeBlock) Text(source []byte) []byte {
324+
return n.Lines().Value(source)
325+
}
326+
317327
// NewFencedCodeBlock return a new FencedCodeBlock node.
318328
func NewFencedCodeBlock(info *Text) *FencedCodeBlock {
319329
return &FencedCodeBlock{
@@ -508,6 +518,15 @@ func (n *HTMLBlock) Kind() NodeKind {
508518
return KindHTMLBlock
509519
}
510520

521+
// Text implements Node.Text.
522+
func (n *HTMLBlock) Text(source []byte) []byte {
523+
ret := n.Lines().Value(source)
524+
if n.HasClosure() {
525+
ret = append(ret, n.ClosureLine.Value(source)...)
526+
}
527+
return ret
528+
}
529+
511530
// NewHTMLBlock returns a new HTMLBlock node.
512531
func NewHTMLBlock(typ HTMLBlockType) *HTMLBlock {
513532
return &HTMLBlock{

ast/inline.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ func (n *AutoLink) Label(source []byte) []byte {
503503
return n.value.Text(source)
504504
}
505505

506+
// Text implements Node.Text.
507+
func (n *AutoLink) Text(source []byte) []byte {
508+
return n.value.Text(source)
509+
}
510+
506511
// NewAutoLink returns a new AutoLink node.
507512
func NewAutoLink(typ AutoLinkType, value *Text) *AutoLink {
508513
return &AutoLink{
@@ -541,6 +546,11 @@ func (n *RawHTML) Kind() NodeKind {
541546
return KindRawHTML
542547
}
543548

549+
// Text implements Node.Text.
550+
func (n *RawHTML) Text(source []byte) []byte {
551+
return n.Segments.Value(source)
552+
}
553+
544554
// NewRawHTML returns a new RawHTML node.
545555
func NewRawHTML() *RawHTML {
546556
return &RawHTML{

0 commit comments

Comments
 (0)