diff --git a/passthrough/passthrough.go b/passthrough/passthrough.go index 62e1acc..866226d 100644 --- a/passthrough/passthrough.go +++ b/passthrough/passthrough.go @@ -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. @@ -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{}, } } @@ -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) diff --git a/passthrough/passthrough_test.go b/passthrough/passthrough_test.go index ce06b43..7b09cf5 100644 --- a/passthrough/passthrough_test.go +++ b/passthrough/passthrough_test.go @@ -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" @@ -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 := "

Emph: wow

" @@ -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