From 426c149ae8e5eb4188c7fd6c3652b4314985f416 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Thu, 12 Aug 2021 21:09:35 +0100 Subject: [PATCH] Ensure empty lines are copiable and final new line too When files are highlighted the newline character needs to be added in a whitespace compliant mode. Also ensure the final empty newline is rendered. Fix #16434 Signed-off-by: Andrew Thornton --- modules/highlight/highlight.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index 568035fbb7fb..4fbd5ef97926 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -166,16 +166,33 @@ func File(numLines int, fileName string, code []byte) map[int]string { } htmlw.Flush() + finalNewLine := false + if len(code) > 0 { + finalNewLine = code[len(code)-1] == '\n' + } + m := make(map[int]string, numLines) for k, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) { line := k + 1 content := string(v) + log.Info("%s", content) + //need to keep lines that are only \n so copy/paste works properly in browser if content == "" { content = "\n" + } else if content == `` { + content += "\n" } m[line] = content } + if finalNewLine { + m[numLines+1] = "\n" + } + + for i, line := range m { + log.Info("%d: %q", i, line) + } + return m }