Skip to content

Commit

Permalink
Add PassthroughBlock.Delimiters
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Aug 5, 2024
1 parent f5d3d00 commit ca07402
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
9 changes: 6 additions & 3 deletions passthrough/passthrough.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,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 @@ -215,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 @@ -310,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

0 comments on commit ca07402

Please sign in to comment.