diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini
index 476b30502681c..5987eeaaa983c 100644
--- a/custom/conf/app.example.ini
+++ b/custom/conf/app.example.ini
@@ -1134,6 +1134,9 @@ PATH =
;;
;; Whether to enable a Service Worker to cache frontend assets
;USE_SERVICE_WORKER = false
+;;
+;; Whether to hide a final newline in rendered code
+;HIDE_FINAL_NEWLINE = true
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md
index 4c43ba7ddb9f6..21d1f70f90c85 100644
--- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md
+++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md
@@ -190,6 +190,7 @@ The following configuration set `Content-Type: application/vnd.android.package-a
- `DEFAULT_SHOW_FULL_NAME`: **false**: Whether the full name of the users should be shown where possible. If the full name isn't set, the username will be used.
- `SEARCH_REPO_DESCRIPTION`: **true**: Whether to search within description at repository search on explore page.
- `USE_SERVICE_WORKER`: **false**: Whether to enable a Service Worker to cache frontend assets.
+- `HIDE_FINAL_NEWLINE`: **true**: Whether to hide a final newline in rendered code
### UI - Admin (`ui.admin`)
diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go
index 344be78144ec5..4691b397a1192 100644
--- a/modules/highlight/highlight.go
+++ b/modules/highlight/highlight.go
@@ -127,6 +127,7 @@ func CodeFromLexer(lexer chroma.Lexer, code string) string {
}
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
return strings.TrimSuffix(htmlbuf.String(), "\n")
@@ -190,8 +191,9 @@ func File(numLines int, fileName, language string, code []byte) []string {
}
htmlw.Flush()
+
finalNewLine := false
- if len(code) > 0 {
+ if !setting.UI.HideFinalNewline && len(code) > 0 {
finalNewLine = code[len(code)-1] == '\n'
}
@@ -208,6 +210,7 @@ func File(numLines int, fileName, language string, code []byte) []string {
content = strings.TrimPrefix(content, ``)
m = append(m, content)
}
+
if finalNewLine {
m = append(m, "\n")
}
diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go
index 2f305bb589c93..9a0a6dcc92c2c 100644
--- a/modules/highlight/highlight_test.go
+++ b/modules/highlight/highlight_test.go
@@ -53,8 +53,6 @@ steps:
` - go build -v`,
` - go test -v -race -coverprofile=coverage.txt -covermode=atomic
`,
- `
-`,
},
},
{
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index 5e317b39ea289..3ca7e7f27da7f 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -228,6 +228,7 @@ var (
CustomEmojisMap map[string]string `ini:"-"`
SearchRepoDescription bool
UseServiceWorker bool
+ HideFinalNewline bool
Notification struct {
MinTimeout time.Duration
@@ -1069,6 +1070,7 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
UI.DefaultShowFullName = Cfg.Section("ui").Key("DEFAULT_SHOW_FULL_NAME").MustBool(false)
UI.SearchRepoDescription = Cfg.Section("ui").Key("SEARCH_REPO_DESCRIPTION").MustBool(true)
UI.UseServiceWorker = Cfg.Section("ui").Key("USE_SERVICE_WORKER").MustBool(false)
+ UI.HideFinalNewline = Cfg.Section("ui").Key("HIDE_FINAL_NEWLINE").MustBool(true)
HasRobotsTxt, err = util.IsFile(path.Join(CustomPath, "robots.txt"))
if err != nil {