mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-08-30 05:36:42 -04:00
HeadingElement now supports indent & margin
This commit is contained in:
+29
-2
@@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/muesli/reflow"
|
||||
bf "gopkg.in/russross/blackfriday.v2"
|
||||
)
|
||||
|
||||
@@ -12,15 +13,41 @@ type HeadingElement struct {
|
||||
}
|
||||
|
||||
func (e *HeadingElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error {
|
||||
var indent uint
|
||||
var margin uint
|
||||
rules := tr.style[Heading]
|
||||
if rules != nil {
|
||||
indent = rules.Indent
|
||||
margin = rules.Margin
|
||||
}
|
||||
|
||||
iw := &IndentWriter{
|
||||
Indent: indent + margin,
|
||||
IndentFunc: func(wr io.Writer) {
|
||||
renderText(w, tr.blockStack.Parent().Style, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: w,
|
||||
},
|
||||
}
|
||||
|
||||
flow := reflow.NewReflow(tr.WordWrap - int(indent) - int(margin*2) - int(tr.blockStack.Indent()) - int(tr.blockStack.Margin())*2)
|
||||
|
||||
var pre string
|
||||
if node.Prev != nil {
|
||||
pre = "\n"
|
||||
}
|
||||
|
||||
el := &BaseElement{
|
||||
Prefix: pre,
|
||||
Token: fmt.Sprintf("%s %s", strings.Repeat("#", node.HeadingData.Level), node.FirstChild.Literal),
|
||||
Style: Heading,
|
||||
}
|
||||
return el.Render(w, node, tr)
|
||||
err := el.Render(flow, node, tr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
flow.Close()
|
||||
_, err = iw.Write(flow.Bytes())
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user