connector/matrixfmt: use different bullet types for each nesting of lists
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
@@ -222,6 +222,7 @@ type Context struct {
|
|||||||
AllowedMentions *event.Mentions
|
AllowedMentions *event.Mentions
|
||||||
TagStack TagStack
|
TagStack TagStack
|
||||||
PreserveWhitespace bool
|
PreserveWhitespace bool
|
||||||
|
ListDepth int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContext(ctx context.Context) Context {
|
func NewContext(ctx context.Context) Context {
|
||||||
@@ -241,6 +242,11 @@ func (ctx Context) WithWhitespace() Context {
|
|||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctx Context) WithIncrementedListDepth() Context {
|
||||||
|
ctx.ListDepth++
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
// HTMLParser is a somewhat customizable Matrix HTML parser.
|
// HTMLParser is a somewhat customizable Matrix HTML parser.
|
||||||
type HTMLParser struct {
|
type HTMLParser struct {
|
||||||
GetGhostDetails func(context.Context, id.UserID) (networkid.UserID, string, int64, bool)
|
GetGhostDetails func(context.Context, id.UserID) (networkid.UserID, string, int64, bool)
|
||||||
@@ -276,8 +282,13 @@ func Digits(num int) int {
|
|||||||
return int(math.Floor(math.Log10(float64(num))) + 1)
|
return int(math.Floor(math.Log10(float64(num))) + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var listBullets = []string{"●", "○", "■", "‣"}
|
||||||
|
|
||||||
func (parser *HTMLParser) listToString(node *html.Node, ctx Context) *EntityString {
|
func (parser *HTMLParser) listToString(node *html.Node, ctx Context) *EntityString {
|
||||||
ordered := node.Data == "ol"
|
ordered := node.Data == "ol"
|
||||||
|
if !ordered {
|
||||||
|
ctx = ctx.WithIncrementedListDepth()
|
||||||
|
}
|
||||||
taggedChildren := parser.nodeToTaggedStrings(node.FirstChild, ctx)
|
taggedChildren := parser.nodeToTaggedStrings(node.FirstChild, ctx)
|
||||||
counter := 1
|
counter := 1
|
||||||
indentLength := 0
|
indentLength := 0
|
||||||
@@ -305,7 +316,7 @@ func (parser *HTMLParser) listToString(node *html.Node, ctx Context) *EntityStri
|
|||||||
}
|
}
|
||||||
prefix = fmt.Sprintf("%d. %s", counter, strings.Repeat(" ", indexPadding))
|
prefix = fmt.Sprintf("%d. %s", counter, strings.Repeat(" ", indexPadding))
|
||||||
} else {
|
} else {
|
||||||
prefix = "* "
|
prefix = fmt.Sprintf("%s ", listBullets[(ctx.ListDepth-1)%len(listBullets)])
|
||||||
}
|
}
|
||||||
es := NewEntityString(prefix).Append(child.EntityString)
|
es := NewEntityString(prefix).Append(child.EntityString)
|
||||||
counter++
|
counter++
|
||||||
|
|||||||
Reference in New Issue
Block a user