From bfc7dd51962f62a2ffbad3500438dc3bb84710fb Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Mon, 10 Oct 2022 20:13:20 +0200 Subject: [PATCH 1/2] Preserve newlines in plain text message --- ui/messages/html/parser.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/ui/messages/html/parser.go b/ui/messages/html/parser.go index e474030..d2dc573 100644 --- a/ui/messages/html/parser.go +++ b/ui/messages/html/parser.go @@ -388,16 +388,39 @@ func (parser *htmlParser) tagNodeToEntity(node *html.Node) Entity { var spaces = regexp.MustCompile("\\s+") +// textToHtmlEntity converts a plain text string into an HtmlEntity while preserving newlines. +func textToHtmlEntity(text string) Entity { + lines := strings.SplitAfter(text, "\n") + if len(lines) == 1 { + return NewTextEntity(text) + } + ent := &ContainerEntity{ + BaseEntity: &BaseEntity{Tag: "span"}, + } + for _, line := range lines { + line_len := len(line) + if line_len == 0 { + continue + } + if line[line_len-1:] == "\n" { + ent.Children = append(ent.Children, NewTextEntity(line[:line_len-1]), NewBreakEntity()) + } else { + ent.Children = append(ent.Children, NewTextEntity(line)) + } + } + return ent +} + func TextToEntity(text string, eventID id.EventID, linkify bool) Entity { if len(text) == 0 { return nil } if !linkify { - return NewTextEntity(text) + return textToHtmlEntity(text) } indices := xurls.Strict().FindAllStringIndex(text, -1) if len(indices) == 0 { - return NewTextEntity(text) + return textToHtmlEntity(text) } ent := &ContainerEntity{ BaseEntity: &BaseEntity{Tag: "span"}, @@ -406,7 +429,7 @@ func TextToEntity(text string, eventID id.EventID, linkify bool) Entity { for i, item := range indices { start, end := item[0], item[1] if start > lastEnd { - ent.Children = append(ent.Children, NewTextEntity(text[lastEnd:start])) + ent.Children = append(ent.Children, textToHtmlEntity(text[lastEnd:start])) } link := text[start:end] linkID := fmt.Sprintf("%s-%d", eventID, i) @@ -414,7 +437,7 @@ func TextToEntity(text string, eventID id.EventID, linkify bool) Entity { lastEnd = end } if lastEnd < len(text) { - ent.Children = append(ent.Children, NewTextEntity(text[lastEnd:])) + ent.Children = append(ent.Children, textToHtmlEntity(text[lastEnd:])) } return ent } From 77b3fffba1ca2184ee17b1adc2a524c5782014e5 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Wed, 19 Oct 2022 13:30:16 +0200 Subject: [PATCH 2/2] s/Html/HTML/g --- ui/messages/html/parser.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ui/messages/html/parser.go b/ui/messages/html/parser.go index d2dc573..605e062 100644 --- a/ui/messages/html/parser.go +++ b/ui/messages/html/parser.go @@ -388,8 +388,8 @@ func (parser *htmlParser) tagNodeToEntity(node *html.Node) Entity { var spaces = regexp.MustCompile("\\s+") -// textToHtmlEntity converts a plain text string into an HtmlEntity while preserving newlines. -func textToHtmlEntity(text string) Entity { +// textToHTMLEntity converts a plain text string into an HTML Entity while preserving newlines. +func textToHTMLEntity(text string) Entity { lines := strings.SplitAfter(text, "\n") if len(lines) == 1 { return NewTextEntity(text) @@ -416,11 +416,11 @@ func TextToEntity(text string, eventID id.EventID, linkify bool) Entity { return nil } if !linkify { - return textToHtmlEntity(text) + return textToHTMLEntity(text) } indices := xurls.Strict().FindAllStringIndex(text, -1) if len(indices) == 0 { - return textToHtmlEntity(text) + return textToHTMLEntity(text) } ent := &ContainerEntity{ BaseEntity: &BaseEntity{Tag: "span"}, @@ -429,7 +429,7 @@ func TextToEntity(text string, eventID id.EventID, linkify bool) Entity { for i, item := range indices { start, end := item[0], item[1] if start > lastEnd { - ent.Children = append(ent.Children, textToHtmlEntity(text[lastEnd:start])) + ent.Children = append(ent.Children, textToHTMLEntity(text[lastEnd:start])) } link := text[start:end] linkID := fmt.Sprintf("%s-%d", eventID, i) @@ -437,7 +437,7 @@ func TextToEntity(text string, eventID id.EventID, linkify bool) Entity { lastEnd = end } if lastEnd < len(text) { - ent.Children = append(ent.Children, textToHtmlEntity(text[lastEnd:])) + ent.Children = append(ent.Children, textToHTMLEntity(text[lastEnd:])) } return ent }