Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure empty lines are copiable and final new line too #16678

Merged
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)

zeripath marked this conversation as resolved.
Show resolved Hide resolved
//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)
}

zeripath marked this conversation as resolved.
Show resolved Hide resolved
return m
}

Expand Down