diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go
index ed0548578a..079f7a44bd 100644
--- a/modules/highlight/highlight.go
+++ b/modules/highlight/highlight.go
@@ -116,8 +116,8 @@ func Code(fileName, code string) string {
return strings.TrimSuffix(htmlbuf.String(), "\n")
}
-// File returns map with line lumbers and HTML version of code with chroma syntax highlighting classes
-func File(numLines int, fileName string, code []byte) map[int]string {
+// File returns a slice of chroma syntax highlighted lines of code
+func File(numLines int, fileName string, code []byte) []string {
NewContext()
if len(code) > sizeLimit {
@@ -171,9 +171,8 @@ func File(numLines int, fileName string, code []byte) map[int]string {
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
+ 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 == "" {
@@ -183,26 +182,25 @@ func File(numLines int, fileName string, code []byte) map[int]string {
}
content = strings.TrimSuffix(content, ``)
content = strings.TrimPrefix(content, ``)
- m[line] = content
+ m = append(m, content)
}
if finalNewLine {
- m[numLines+1] = "\n"
+ m = append(m, "\n")
}
return m
}
// return unhiglighted map
-func plainText(code string, numLines int) map[int]string {
- m := make(map[int]string, numLines)
- for k, v := range strings.SplitN(string(code), "\n", numLines) {
- line := k + 1
+func plainText(code string, numLines int) []string {
+ m := make([]string, 0, numLines)
+ for _, v := range strings.SplitN(string(code), "\n", numLines) {
content := string(v)
//need to keep lines that are only \n so copy/paste works properly in browser
if content == "" {
content = "\n"
}
- m[line] = gohtml.EscapeString(content)
+ m = append(m, gohtml.EscapeString(content))
}
return m
}
diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go
index 7c5afaa52c..0a67e4c602 100644
--- a/modules/highlight/highlight_test.go
+++ b/modules/highlight/highlight_test.go
@@ -19,7 +19,7 @@ func TestFile(t *testing.T) {
numLines int
fileName string
code string
- want map[int]string
+ want []string
}{
{
name: ".drone.yml",
@@ -38,22 +38,22 @@ steps:
- go build -v
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
`,
- want: map[int]string{
- 1: `kind: pipeline`,
- 2: `name: default`,
- 3: `
+ want: []string{
+ `kind: pipeline`,
+ `name: default`,
+ `
`,
- 4: `steps:`,
- 5: `- name: test`,
- 6: ` image: golang:1.13`,
- 7: ` environment:`,
- 8: ` GOPROXY: https://goproxy.cn`,
- 9: ` commands:`,
- 10: ` - go get -u`,
- 11: ` - go build -v`,
- 12: ` - go test -v -race -coverprofile=coverage.txt -covermode=atomic
+ `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
`,
- 13: `
+ `
`,
},
},
@@ -74,21 +74,21 @@ steps:
- go build -v
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
`,
- want: map[int]string{
- 1: `kind: pipeline`,
- 2: `name: default `,
- 3: `
+ want: []string{
+ `kind: pipeline`,
+ `name: default `,
+ `
`,
- 4: `steps:`,
- 5: `- name: test`,
- 6: ` image: golang:1.13`,
- 7: ` environment:`,
- 8: ` GOPROXY: https://goproxy.cn`,
- 9: ` commands:`,
- 10: ` - go get -u`,
- 11: ` - go build -v`,
- 12: ` - go test -v -race -coverprofile=coverage.txt -covermode=atomic`,
- 13: ` `,
+ `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`,
+ ` `,
},
},
}
diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl
index 8c73f1252b..b6198bd0e2 100644
--- a/templates/repo/view_file.tmpl
+++ b/templates/repo/view_file.tmpl
@@ -99,7 +99,8 @@
{{else}}
- {{range $line, $code := .FileContent}}
+ {{range $idx, $code := .FileContent}}
+ {{$line := Add $idx 1}}
|