Skip to content

Commit

Permalink
Really fix empty line rendering
Browse files Browse the repository at this point in the history
Followup to go-gitea#19798. Trim off both the .line and .cl classes from
Chroma's HTML which made the old conditional work again. This fixed Copy
of YAML files for me.
  • Loading branch information
silverwind committed Jun 14, 2022
1 parent 460c03c commit 8a74dca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
10 changes: 6 additions & 4 deletions modules/highlight/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,18 @@ func File(numLines int, fileName, language string, code []byte) []string {
m := make([]string, 0, numLines)
for _, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) {
content := string(v)

// remove useless wrapper nodes
content = strings.Replace(content, "<span class=\"line\"><span class=\"cl\">", "", 1)
content = strings.TrimPrefix(content, `</span></span>`)

// 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</span>"
} else if content == `</span></span><span class="line"><span class="cl">` {
content += "\n"
}
content = strings.TrimSuffix(content, `<span class="w">`)
content = strings.TrimPrefix(content, `</span>`)

m = append(m, content)
}
if finalNewLine {
Expand Down
52 changes: 27 additions & 25 deletions modules/highlight/highlight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ func TestFile(t *testing.T) {
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
`),
want: util.Dedent(`
<span class="line"><span class="cl"><span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>
</span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">steps</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"><span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span>
</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span>
</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">environment</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span>
</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">commands</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>
</span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go build -v</span>
</span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span></span></span>
<span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span><span class="w">
</span><span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default</span><span class="w">
</span><span class="w">
</span><span class="w"></span><span class="nt">steps</span><span class="p">:</span><span class="w">
</span><span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span><span class="w">
</span><span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span><span class="w">
</span><span class="w"> </span><span class="nt">environment</span><span class="p">:</span><span class="w">
</span><span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span><span class="w">
</span><span class="w"> </span><span class="nt">commands</span><span class="p">:</span><span class="w">
</span><span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span><span class="w">
</span><span class="w"> </span>- <span class="l">go build -v</span><span class="w">
</span><span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span></span></span>
`),
},
{
Expand All @@ -76,19 +77,20 @@ func TestFile(t *testing.T) {
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
`)+"\n", "name: default", "name: default ", 1),
want: util.Dedent(`
<span class="line"><span class="cl"><span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>
</span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default </span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">steps</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"><span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span>
</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span>
</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">environment</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span>
</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">commands</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>
</span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go build -v</span>
</span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span>
</span></span>
<span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span><span class="w">
</span><span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default </span><span class="w">
</span><span class="w">
</span><span class="w"></span><span class="nt">steps</span><span class="p">:</span><span class="w">
</span><span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span><span class="w">
</span><span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span><span class="w">
</span><span class="w"> </span><span class="nt">environment</span><span class="p">:</span><span class="w">
</span><span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span><span class="w">
</span><span class="w"> </span><span class="nt">commands</span><span class="p">:</span><span class="w">
</span><span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span><span class="w">
</span><span class="w"> </span>- <span class="l">go build -v</span><span class="w">
</span><span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span><span class="w">
</span>
<span class="w">
</span>
`),
Expand Down

0 comments on commit 8a74dca

Please sign in to comment.