Add new element style 'enumeration', used for items in ordered lists

This commit is contained in:
Christian Muehlhaeuser
2019-12-05 10:05:15 +01:00
parent 1e195cc3de
commit 82d6db7c7d
2 changed files with 25 additions and 3 deletions
+22 -3
View File
@@ -2,6 +2,7 @@ package gold
import ( import (
"io" "io"
"strconv"
bf "gopkg.in/russross/blackfriday.v2" bf "gopkg.in/russross/blackfriday.v2"
) )
@@ -10,9 +11,27 @@ type ItemElement struct {
} }
func (e *ItemElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error { func (e *ItemElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error {
el := &BaseElement{ var el *BaseElement
Token: string(node.Literal),
Style: Item, if node.ListData.ListFlags&bf.ListTypeOrdered > 0 {
var l int64
n := node
for n.Prev != nil && (n.Prev.Type == bf.Item) {
l++
n = n.Prev
}
el = &BaseElement{
Token: string(node.Literal),
Style: Enumeration,
Prefix: strconv.FormatInt(l+1, 10),
}
} else {
el = &BaseElement{
Token: string(node.Literal),
Style: Item,
}
} }
return el.Render(w, node, tr) return el.Render(w, node, tr)
} }
+3
View File
@@ -11,6 +11,7 @@ const (
BlockQuote BlockQuote
List List
Item Item
Enumeration
Paragraph Paragraph
Heading Heading
HorizontalRule HorizontalRule
@@ -90,6 +91,8 @@ func keyToType(key string) (StyleType, error) {
return List, nil return List, nil
case "item": case "item":
return Item, nil return Item, nil
case "enumeration":
return Enumeration, nil
case "paragraph": case "paragraph":
return Paragraph, nil return Paragraph, nil
case "heading": case "heading":