From 31dc0259f36a68153e071502d66b43e71b4f0d36 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Tue, 22 Oct 2024 13:39:32 -0600 Subject: [PATCH] connector/matrixfmt: use different bullet types for each nesting of lists Signed-off-by: Sumner Evans --- pkg/connector/matrixfmt/html.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/connector/matrixfmt/html.go b/pkg/connector/matrixfmt/html.go index c52c55c6..c1a232a7 100644 --- a/pkg/connector/matrixfmt/html.go +++ b/pkg/connector/matrixfmt/html.go @@ -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++