From 58c20698d68f14b213a53a682fee640cf987d8e9 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Mon, 10 Oct 2022 18:40:54 +0200 Subject: [PATCH 1/2] Fix mangled newlines in some code blocks Because some tokens can contain newlines and not only comment tokens, I removed the comments specific code to handle newlines in a more generic way. --- ui/messages/html/parser.go | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/ui/messages/html/parser.go b/ui/messages/html/parser.go index ce4caee..f4839db 100644 --- a/ui/messages/html/parser.go +++ b/ui/messages/html/parser.go @@ -305,27 +305,19 @@ func (parser *htmlParser) syntaxHighlight(text, language string) Entity { var children []Entity for _, token := range tokens { - if token.Value == "\n" { - children = append(children, NewBreakEntity()) - - } else if token.Type.String() == "CommentSingle" { - children = append(children, tokenToTextEntity(style, &token)) - children = append(children, NewBreakEntity()) - - } else if token.Type.String() == "CommentMultiline" { - lines := strings.Split(token.Value, "\n") - for i, line := range lines { - t := token.Clone() - t.Value = line - children = append(children, tokenToTextEntity(style, &t)) - - if i < len(lines)-1 { - children = append(children, NewBreakEntity()) - } + lines := strings.SplitAfter(token.Value, "\n") + for _, line := range lines { + line_len := len(line) + if line_len == 0 { + continue } + t := token.Clone() + t.Value = line + children = append(children, tokenToTextEntity(style, &t)) - } else { - children = append(children, tokenToTextEntity(style, &token)) + if line[line_len-1:] == "\n" { + children = append(children, NewBreakEntity()) + } } } From 1cff17a8570689e0f2709d1c3960475160aae662 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Mon, 10 Oct 2022 19:13:30 +0200 Subject: [PATCH 2/2] Fix codeblocks doubled newlines in plaintext mode --- ui/messages/html/parser.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ui/messages/html/parser.go b/ui/messages/html/parser.go index f4839db..e1a6381 100644 --- a/ui/messages/html/parser.go +++ b/ui/messages/html/parser.go @@ -312,11 +312,13 @@ func (parser *htmlParser) syntaxHighlight(text, language string) Entity { continue } t := token.Clone() - t.Value = line - children = append(children, tokenToTextEntity(style, &t)) if line[line_len-1:] == "\n" { - children = append(children, NewBreakEntity()) + t.Value = line[:line_len-1] + children = append(children, tokenToTextEntity(style, &t), NewBreakEntity()) + } else { + t.Value = line + children = append(children, tokenToTextEntity(style, &t)) } } }