Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc adjustments #29

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions passthrough/passthrough.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func startsWith(b []byte, s string) bool {
return string(b[:len(s)]) == s
}

type passthroughInline struct {
// PassthroughInline is a node representing a inline passthrough.
type PassthroughInline struct {
ast.BaseInline

// The segment of text that this inline passthrough represents.
Expand All @@ -37,20 +38,20 @@ type passthroughInline struct {
Delimiters *Delimiters
}

func newPassthroughInline(segment text.Segment, delimiters *Delimiters) *passthroughInline {
return &passthroughInline{
func newPassthroughInline(segment text.Segment, delimiters *Delimiters) *PassthroughInline {
return &PassthroughInline{
Segment: segment,
Delimiters: delimiters,
}
}

// Text implements Node.Text.
func (n *passthroughInline) Text(source []byte) []byte {
func (n *PassthroughInline) Text(source []byte) []byte {
return n.Segment.Value(source)
}

// Dump implements Node.Dump.
func (n *passthroughInline) Dump(source []byte, level int) {
func (n *PassthroughInline) Dump(source []byte, level int) {
indent := strings.Repeat(" ", level)
fmt.Printf("%sPassthroughInline {\n", indent)
indent2 := strings.Repeat(" ", level+1)
Expand All @@ -62,7 +63,7 @@ func (n *passthroughInline) Dump(source []byte, level int) {
var KindPassthroughInline = ast.NewNodeKind("PassthroughInline")

// Kind implements Node.Kind.
func (n *passthroughInline) Kind() ast.NodeKind {
func (n *PassthroughInline) Kind() ast.NodeKind {
return KindPassthroughInline
}

Expand Down Expand Up @@ -198,6 +199,8 @@ func (r *passthroughInlineRenderer) renderRawInline(w util.BufWriter, source []b
// with the matching block delimiters.
type PassthroughBlock struct {
ast.BaseBlock
// The matched delimiters
Delimiters *Delimiters
}

// Dump implements Node.Dump.
Expand All @@ -214,9 +217,10 @@ func (n *PassthroughBlock) Kind() ast.NodeKind {
}

// newPassthroughBlock return a new PassthroughBlock node.
func newPassthroughBlock() *PassthroughBlock {
func newPassthroughBlock(delimiters *Delimiters) *PassthroughBlock {
return &PassthroughBlock{
BaseBlock: ast.BaseBlock{},
Delimiters: delimiters,
BaseBlock: ast.BaseBlock{},
}
}

Expand Down Expand Up @@ -300,7 +304,7 @@ func (p *passthroughInlineTransformer) Transform(
currentParagraph.AppendChild(currentParagraph, currentNode)
currentNode = nextNode
} else if currentNode.Kind() == KindPassthroughInline {
inline := currentNode.(*passthroughInline)
inline := currentNode.(*PassthroughInline)

// Only split into a new block if the delimiters are block delimiters
if !containsDelimiters(p.BlockDelimiters, inline.Delimiters) {
Expand All @@ -309,7 +313,7 @@ func (p *passthroughInlineTransformer) Transform(
continue
}

newBlock := newPassthroughBlock()
newBlock := newPassthroughBlock(inline.Delimiters)
newBlock.Lines().Append(inline.Segment)
if len(currentParagraph.Text(reader.Source())) > 0 {
parent.InsertAfter(parent, insertionPoint, currentParagraph)
Expand Down
42 changes: 42 additions & 0 deletions passthrough/passthrough_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

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

qt "github.com/frankban/quicktest"
Expand Down Expand Up @@ -54,6 +55,23 @@ func Parse(t *testing.T, input string) string {
return strings.TrimSpace(buf.String())
}

func ParseWalk(t testing.TB, input string, cb func(n ast.Node, entering bool) bool) {
t.Helper()
md := buildTestParser()
doc := md.Parser().Parse(text.NewReader([]byte(input)))
err := ast.Walk(
doc,
func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if cb(n, entering) {
return ast.WalkSkipChildren, nil
}
return ast.WalkContinue, nil
})
if err != nil {
t.Fatal(err)
}
}

func TestEmphasisOutsideOfMathmode(t *testing.T) {
input := "Emph: _wow_"
expected := "<p>Emph: <em>wow</em></p>"
Expand Down Expand Up @@ -549,6 +567,30 @@ $$a^*=x-b^*$$
c.Assert(actual, qt.Equals, expected)
}

func TestNodeDelimiter(t *testing.T) {
input := `
Block $$a^*=x-b^*$$ equation
Inline $a^*=x-b^*$ equation

`

c := qt.New(t)

ParseWalk(t, input, func(n ast.Node, entering bool) bool {
if entering {
switch nn := n.(type) {
case *PassthroughBlock:
c.Assert(nn.Delimiters.Open, qt.Equals, "$$")
c.Assert(nn.Delimiters.Close, qt.Equals, "$$")
case *PassthroughInline:
c.Assert(nn.Delimiters.Open, qt.Equals, "$")
c.Assert(nn.Delimiters.Close, qt.Equals, "$")
}
}
return false
})
}

func BenchmarkWithAndWithoutPassthrough(b *testing.B) {
const input = `
## Block
Expand Down