Skip to content

Commit

Permalink
Ensure empty lines are copiable and final new line too
Browse files Browse the repository at this point in the history
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 go-gitea#16434

Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Aug 12, 2021
1 parent 2289580 commit 426c149
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions modules/highlight/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == `</span><span class="w">` {
content += "\n"
}
m[line] = content
}
if finalNewLine {
m[numLines+1] = "<span class=\"w\">\n</span>"
}

for i, line := range m {
log.Info("%d: %q", i, line)
}

return m
}

Expand Down

0 comments on commit 426c149

Please sign in to comment.