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:
Sumner Evans
2024-10-22 13:39:32 -06:00
parent 13f21a7c70
commit 31dc0259f3
+12 -1
View File
@@ -222,6 +222,7 @@ type Context struct {
AllowedMentions *event.Mentions
TagStack TagStack
PreserveWhitespace bool
ListDepth int
}
func NewContext(ctx context.Context) Context {
@@ -241,6 +242,11 @@ func (ctx Context) WithWhitespace() Context {
return ctx
}
func (ctx Context) WithIncrementedListDepth() Context {
ctx.ListDepth++
return ctx
}
// HTMLParser is a somewhat customizable Matrix HTML parser.
type HTMLParser struct {
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)
}
var listBullets = []string{"●", "○", "■", "‣"}
func (parser *HTMLParser) listToString(node *html.Node, ctx Context) *EntityString {
ordered := node.Data == "ol"
if !ordered {
ctx = ctx.WithIncrementedListDepth()
}
taggedChildren := parser.nodeToTaggedStrings(node.FirstChild, ctx)
counter := 1
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))
} else {
prefix = "* "
prefix = fmt.Sprintf("%s ", listBullets[(ctx.ListDepth-1)%len(listBullets)])
}
es := NewEntityString(prefix).Append(child.EntityString)
counter++