From 074759286580bd40837a9370314f6345770b8582 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 3 Aug 2022 01:37:28 +0800 Subject: [PATCH] Adjust line detection in highlight.go (#20612) The code for detection of lines in highlight.go is somewhat too complex and doesn't take account of how Chroma is actually splitting things into lines for us. Remove both the .line and .cl classes from Chroma's HTML which made the old conditional work again. This fixed Copy of YAML files while also reducing the amount of rendered HTML nodes. Co-authored-by: zeripath Co-authored-by: Lauris BH --- modules/highlight/highlight.go | 45 +++++++++--------- modules/highlight/highlight_test.go | 72 +++++++++++++++++++---------- 2 files changed, 68 insertions(+), 49 deletions(-) diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index a72f26d5f0..d967df1f98 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -26,7 +26,7 @@ import ( ) // don't index files larger than this many bytes for performance purposes -const sizeLimit = 1000000 +const sizeLimit = 1024 * 1024 var ( // For custom user mapping @@ -58,7 +58,7 @@ func NewContext() { func Code(fileName, language, code string) string { NewContext() - // diff view newline will be passed as empty, change to literal \n so it can be copied + // diff view newline will be passed as empty, change to literal '\n' so it can be copied // preserve literal newline in blame view if code == "" || code == "\n" { return "\n" @@ -104,6 +104,11 @@ func Code(fileName, language, code string) string { return CodeFromLexer(lexer, code) } +type nopPreWrapper struct{} + +func (nopPreWrapper) Start(code bool, styleAttr string) string { return "" } +func (nopPreWrapper) End(code bool) string { return "" } + // CodeFromLexer returns a HTML version of code string with chroma syntax highlighting classes func CodeFromLexer(lexer chroma.Lexer, code string) string { formatter := html.New(html.WithClasses(true), @@ -126,9 +131,9 @@ func CodeFromLexer(lexer chroma.Lexer, code string) string { return code } - htmlw.Flush() + _ = htmlw.Flush() // Chroma will add newlines for certain lexers in order to highlight them properly - // Once highlighted, strip them here so they don't cause copy/paste trouble in HTML output + // Once highlighted, strip them here, so they don't cause copy/paste trouble in HTML output return strings.TrimSuffix(htmlbuf.String(), "\n") } @@ -141,7 +146,7 @@ func File(numLines int, fileName, language string, code []byte) []string { } formatter := html.New(html.WithClasses(true), html.WithLineNumbers(false), - html.PreventSurroundingPre(true), + html.WithPreWrapper(nopPreWrapper{}), ) if formatter == nil { @@ -189,27 +194,19 @@ func File(numLines int, fileName, language string, code []byte) []string { return plainText(string(code), numLines) } - htmlw.Flush() + _ = htmlw.Flush() finalNewLine := false if len(code) > 0 { finalNewLine = code[len(code)-1] == '\n' } - m := make([]string, 0, numLines) - for _, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) { - content := string(v) - // need to keep lines that are only \n so copy/paste works properly in browser - if content == "" { - content = "\n" - } else if content == `` { - content += "\n" - } else if content == `` { - content += "\n" - } - content = strings.TrimSuffix(content, ``) - content = strings.TrimPrefix(content, ``) - m = append(m, content) + m := strings.SplitN(htmlbuf.String(), ``, numLines) + if len(m) > 0 { + m[0] = m[0][len(``):] + last := m[len(m)-1] + m[len(m)-1] = last[:len(last)-len(``)] } + if finalNewLine { m = append(m, "\n") } @@ -219,14 +216,14 @@ func File(numLines int, fileName, language string, code []byte) []string { // return unhiglighted map func plainText(code string, numLines int) []string { - m := make([]string, 0, numLines) - for _, v := range strings.SplitN(string(code), "\n", numLines) { - content := string(v) + m := strings.SplitN(code, "\n", numLines) + + for i, content := range m { // need to keep lines that are only \n so copy/paste works properly in browser if content == "" { content = "\n" } - m = append(m, gohtml.EscapeString(content)) + m[i] = gohtml.EscapeString(content) } return m } diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go index e5dfedd2b3..87628d1bbb 100644 --- a/modules/highlight/highlight_test.go +++ b/modules/highlight/highlight_test.go @@ -43,18 +43,29 @@ func TestFile(t *testing.T) { - go test -v -race -coverprofile=coverage.txt -covermode=atomic `), want: util.Dedent(` - kind: pipeline - name: default - - steps: - - name: test - image: golang:1.13 - environment: - GOPROXY: https://goproxy.cn - commands: - - go get -u - - go build -v - - go test -v -race -coverprofile=coverage.txt -covermode=atomic + kind: pipeline + + name: default + + + + steps: + + - name: test + + image: golang:1.13 + + environment: + + GOPROXY: https://goproxy.cn + + commands: + + - go get -u + + - go build -v + + - go test -v -race -coverprofile=coverage.txt -covermode=atomic `), }, { @@ -76,19 +87,30 @@ func TestFile(t *testing.T) { - go test -v -race -coverprofile=coverage.txt -covermode=atomic `)+"\n", "name: default", "name: default ", 1), want: util.Dedent(` - kind: pipeline - name: default - - steps: - - name: test - image: golang:1.13 - environment: - GOPROXY: https://goproxy.cn - commands: - - go get -u - - go build -v - - go test -v -race -coverprofile=coverage.txt -covermode=atomic - + kind: pipeline + + name: default + + + + steps: + + - name: test + + image: golang:1.13 + + environment: + + GOPROXY: https://goproxy.cn + + commands: + + - go get -u + + - go build -v + + - go test -v -race -coverprofile=coverage.txt -covermode=atomic + `),