Skip to content

Commit

Permalink
Fix #470
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Oct 15, 2024
1 parent bc993b4 commit ad15651
Show file tree
Hide file tree
Showing 10 changed files with 387 additions and 82 deletions.
5 changes: 5 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ func (n *BaseNode) Text(source []byte) []byte {
var buf bytes.Buffer
for c := n.firstChild; c != nil; c = c.NextSibling() {
buf.Write(c.Text(source))
if sb, ok := c.(interface {
SoftLineBreak() bool
}); ok && sb.SoftLineBreak() {
buf.WriteByte('\n')
}
}
return buf.Bytes()
}
Expand Down
63 changes: 0 additions & 63 deletions ast/ast_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
package ast

import (
"bytes"
"reflect"
"testing"

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

func TestRemoveChildren(t *testing.T) {
root := NewDocument()

node1 := NewDocument()

node2 := NewDocument()

root.AppendChild(root, node1)
root.AppendChild(root, node2)

root.RemoveChildren(root)

t.Logf("%+v", node2.PreviousSibling())
}

func TestWalk(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -76,48 +58,3 @@ func node(n Node, children ...Node) Node {
}
return n
}

func TestBaseBlock_Text(t *testing.T) {
source := []byte(`# Heading
code block here
and also here
A paragraph
` + "```" + `somelang
fenced code block
` + "```" + `
The end`)

t.Run("fetch text from code block", func(t *testing.T) {
block := NewCodeBlock()
block.lines = text.NewSegments()
block.lines.Append(text.Segment{Start: 15, Stop: 31})
block.lines.Append(text.Segment{Start: 32, Stop: 46})

expected := []byte("code block here\nand also here\n")
if !bytes.Equal(expected, block.Text(source)) {
t.Errorf("Expected: %q, got: %q", string(expected), string(block.Text(source)))
}
})

t.Run("fetch text from fenced code block", func(t *testing.T) {
block := NewFencedCodeBlock(&Text{
Segment: text.Segment{Start: 63, Stop: 71},
})
block.lines = text.NewSegments()
block.lines.Append(text.Segment{Start: 72, Stop: 90})

expectedLang := []byte("somelang")
if !bytes.Equal(expectedLang, block.Language(source)) {
t.Errorf("Expected: %q, got: %q", string(expectedLang), string(block.Language(source)))
}

expected := []byte("fenced code block\n")
if !bytes.Equal(expected, block.Text(source)) {
t.Errorf("Expected: %q, got: %q", string(expected), string(block.Text(source)))
}
})
}
39 changes: 29 additions & 10 deletions ast/block.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ast

import (
"bytes"
"fmt"
"strings"

Expand Down Expand Up @@ -48,15 +47,6 @@ func (b *BaseBlock) SetLines(v *textm.Segments) {
b.lines = v
}

// Text implements Node.Text.
func (b *BaseBlock) Text(source []byte) []byte {
var buf bytes.Buffer
for _, line := range b.Lines().Sliced(0, b.Lines().Len()) {
buf.Write(line.Value(source))
}
return buf.Bytes()
}

// A Document struct is a root node of Markdown text.
type Document struct {
BaseBlock
Expand Down Expand Up @@ -140,6 +130,11 @@ func (n *TextBlock) Kind() NodeKind {
return KindTextBlock
}

// Text implements Node.Text.
func (n *TextBlock) Text(source []byte) []byte {
return n.Lines().Value(source)
}

// NewTextBlock returns a new TextBlock node.
func NewTextBlock() *TextBlock {
return &TextBlock{
Expand All @@ -165,6 +160,11 @@ func (n *Paragraph) Kind() NodeKind {
return KindParagraph
}

// Text implements Node.Text.
func (n *Paragraph) Text(source []byte) []byte {
return n.Lines().Value(source)
}

// NewParagraph returns a new Paragraph node.
func NewParagraph() *Paragraph {
return &Paragraph{
Expand Down Expand Up @@ -259,6 +259,11 @@ func (n *CodeBlock) Kind() NodeKind {
return KindCodeBlock
}

// Text implements Node.Text.
func (n *CodeBlock) Text(source []byte) []byte {
return n.Lines().Value(source)
}

// NewCodeBlock returns a new CodeBlock node.
func NewCodeBlock() *CodeBlock {
return &CodeBlock{
Expand Down Expand Up @@ -314,6 +319,11 @@ func (n *FencedCodeBlock) Kind() NodeKind {
return KindFencedCodeBlock
}

// Text implements Node.Text.
func (n *FencedCodeBlock) Text(source []byte) []byte {
return n.Lines().Value(source)
}

// NewFencedCodeBlock return a new FencedCodeBlock node.
func NewFencedCodeBlock(info *Text) *FencedCodeBlock {
return &FencedCodeBlock{
Expand Down Expand Up @@ -508,6 +518,15 @@ func (n *HTMLBlock) Kind() NodeKind {
return KindHTMLBlock
}

// Text implements Node.Text.
func (n *HTMLBlock) Text(source []byte) []byte {
ret := n.Lines().Value(source)
if n.HasClosure() {
ret = append(ret, n.ClosureLine.Value(source)...)
}
return ret
}

// NewHTMLBlock returns a new HTMLBlock node.
func NewHTMLBlock(typ HTMLBlockType) *HTMLBlock {
return &HTMLBlock{
Expand Down
10 changes: 10 additions & 0 deletions ast/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,11 @@ func (n *AutoLink) Label(source []byte) []byte {
return n.value.Text(source)
}

// Text implements Node.Text.
func (n *AutoLink) Text(source []byte) []byte {
return n.value.Text(source)
}

// NewAutoLink returns a new AutoLink node.
func NewAutoLink(typ AutoLinkType, value *Text) *AutoLink {
return &AutoLink{
Expand Down Expand Up @@ -541,6 +546,11 @@ func (n *RawHTML) Kind() NodeKind {
return KindRawHTML
}

// Text implements Node.Text.
func (n *RawHTML) Text(source []byte) []byte {
return n.Segments.Value(source)
}

// NewRawHTML returns a new RawHTML node.
func NewRawHTML() *RawHTML {
return &RawHTML{
Expand Down
Loading

0 comments on commit ad15651

Please sign in to comment.