Skip to content

Commit

Permalink
Really fix empty line rendering
Browse files Browse the repository at this point in the history
Fixes: #19331

Followup to #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 while also reducing the amount of rendered HTML nodes.
  • Loading branch information
silverwind committed Jun 14, 2022
1 parent 460c03c commit e1383bf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
17 changes: 12 additions & 5 deletions modules/highlight/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,25 @@ func File(numLines int, fileName, language string, code []byte) []string {
}

m := make([]string, 0, numLines)
for _, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) {
for i, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) {
content := string(v)

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

// if there's no final newline, closing tags will be on last line
if (!finalNewLine && i == numLines - 1) {
content = strings.TrimSuffix(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
53 changes: 28 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>
`),
},
{
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 All @@ -99,6 +101,7 @@ func TestFile(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got := strings.Join(File(tt.numLines, tt.fileName, "", []byte(tt.code)), "\n")
assert.Equal(t, tt.want, got)
assert.Equal(t, strings.Count(got, "<span"), strings.Count(got, "</span>"))
})
}
}

0 comments on commit e1383bf

Please sign in to comment.