diff --git a/block.go b/block.go index f96191e..ff5ef49 100644 --- a/block.go +++ b/block.go @@ -245,10 +245,9 @@ func (p *parser) prefixHeader(out *bytes.Buffer, data []byte) int { if id == "" && p.flags&AutoHeaderIDs != 0 { id = sanitized_anchor_name.Create(string(data[i:end])) } - work := func() { - p.inline(out, data[i:end]) - } - p.r.Header(out, work, level, id) + tocMarker := p.r.BeginHeader(out, level, id) + p.inline(out, data[i:end]) + p.r.EndHeader(out, level, id, tocMarker) } return skip } @@ -1318,20 +1317,14 @@ func (p *parser) paragraph(out *bytes.Buffer, data []byte) int { eol-- } - // render the header - // this ugly double closure avoids forcing variables onto the heap - work := func(o *bytes.Buffer, pp *parser, d []byte) func() { - return func() { - pp.inline(o, d) - } - }(out, p, data[prev:eol]) - id := "" if p.flags&AutoHeaderIDs != 0 { id = sanitized_anchor_name.Create(string(data[prev:eol])) } - p.r.Header(out, work, level, id) + tocMarker := p.r.BeginHeader(out, level, id) + p.inline(out, data[prev:eol]) + p.r.EndHeader(out, level, id, tocMarker) // find the end of the underline for data[i] != '\n' { diff --git a/html.go b/html.go index 3ddc446..4654911 100644 --- a/html.go +++ b/html.go @@ -202,7 +202,7 @@ func (options *Html) TitleBlock(out *bytes.Buffer, text []byte) { out.WriteString("\n") } -func (options *Html) Header(out *bytes.Buffer, text func(), level int, id string) { +func (options *Html) BeginHeader(out *bytes.Buffer, level int, id string) int { doubleSpace(out) if id == "" && options.flags&Toc != 0 { @@ -225,12 +225,13 @@ func (options *Html) Header(out *bytes.Buffer, text func(), level int, id string out.WriteString(fmt.Sprintf("", level)) } - tocMarker := out.Len() - text() + return out.Len() +} +func (r *Html) EndHeader(out *bytes.Buffer, level int, id string, tocMarker int) { // are we building a table of contents? - if options.flags&Toc != 0 { - options.TocHeaderWithAnchor(out.Bytes()[tocMarker:], level, id) + if r.flags&Toc != 0 { + r.TocHeaderWithAnchor(out.Bytes()[tocMarker:], level, id) } out.WriteString(fmt.Sprintf("\n", level)) diff --git a/latex.go b/latex.go index 6bc9baa..d4221c6 100644 --- a/latex.go +++ b/latex.go @@ -72,7 +72,7 @@ func (options *Latex) BlockHtml(out *bytes.Buffer, text []byte) { out.WriteString("\n\\end{verbatim}\n") } -func (options *Latex) Header(out *bytes.Buffer, text func(), level int, id string) { +func (r *Latex) BeginHeader(out *bytes.Buffer, level int, id string) int { switch level { case 1: out.WriteString("\n\\section{") @@ -87,7 +87,10 @@ func (options *Latex) Header(out *bytes.Buffer, text func(), level int, id strin case 6: out.WriteString("\n\\textbf{") } - text() + return out.Len() +} + +func (r *Latex) EndHeader(out *bytes.Buffer, level int, id string, tocMarker int) { out.WriteString("}\n") } diff --git a/markdown.go b/markdown.go index fb27869..e534f0e 100644 --- a/markdown.go +++ b/markdown.go @@ -163,7 +163,8 @@ type Renderer interface { BlockCode(out *bytes.Buffer, text []byte, lang string) BlockQuote(out *bytes.Buffer, text []byte) BlockHtml(out *bytes.Buffer, text []byte) - Header(out *bytes.Buffer, text func(), level int, id string) + BeginHeader(out *bytes.Buffer, level int, id string) int + EndHeader(out *bytes.Buffer, level int, id string, tocMarker int) HRule(out *bytes.Buffer) List(out *bytes.Buffer, text func(), flags ListType) ListItem(out *bytes.Buffer, text []byte, flags ListType)