Skip to content

Commit e194d89

Browse files
GiteaBotsilverwind
andauthored
Improve and fix markup code preview rendering (#35777) (#35787)
Backport #35777 by @silverwind 1. Add the color on the link to the referenced file, which is the more likely thing the user wants to click 2. Use monospace font on the SHA 3. Tweak text colors 4. Change SHA link to go to the commit instead of the repo root with commit filter set 5. Added the repo name to the file link text 6. Fix broken line numbering rendering The only major difference to GitHub is now the missing line numbers. Before: <img width="286" height="162" alt="Screenshot 2025-10-29 at 19 09 59" src="https://github.com/user-attachments/assets/f16b4eec-caf2-4c31-a2b5-ae5f41747d4b" /> After: <img width="378" height="157" alt="image" src="https://github.com/user-attachments/assets/0c91dfd3-0910-4b2d-a43b-8c87cfbb933e" /> For comparison, GitHub rendering: <img width="286" height="177" alt="image" src="https://github.com/user-attachments/assets/8a9a07b7-9153-4415-9d7a-5685853e472a" /> Co-authored-by: silverwind <[email protected]>
1 parent 04b6f90 commit e194d89

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

modules/markup/sanitizer_default.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ func (st *Sanitizer) createDefaultPolicy() *bluemonday.Policy {
3030
// Chroma always uses 1-2 letters for style names, we could tolerate it at the moment
3131
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^\w{0,2}$`)).OnElements("span")
3232

33+
// Line numbers on codepreview
34+
policy.AllowAttrs("data-line-number").OnElements("span")
35+
3336
// Custom URL-Schemes
3437
if len(setting.Markdown.CustomURLSchemes) > 0 {
3538
policy.AllowURLSchemes(setting.Markdown.CustomURLSchemes...)

services/markup/renderhelper_codepreview.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func renderRepoFileCodePreview(ctx context.Context, opts markup.RenderCodePrevie
110110
"FilePath": opts.FilePath,
111111
"LineStart": opts.LineStart,
112112
"LineStop": realLineStop,
113+
"RepoName": opts.RepoName,
113114
"RepoLink": dbRepo.Link(),
114115
"CommitID": opts.CommitID,
115116
"HighlightLines": highlightLines,

services/markup/renderhelper_codepreview_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ func TestRenderHelperCodePreview(t *testing.T) {
2424
OwnerName: "user2",
2525
RepoName: "repo1",
2626
CommitID: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
27-
FilePath: "/README.md",
27+
FilePath: "README.md",
2828
LineStart: 1,
2929
LineStop: 2,
3030
})
3131
assert.NoError(t, err)
3232
assert.Equal(t, `<div class="code-preview-container file-content">
3333
<div class="code-preview-header">
34-
<a href="http://full" class="muted" rel="nofollow">/README.md</a>
35-
repo.code_preview_line_from_to:1,2,<a href="/user2/repo1/src/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d" rel="nofollow">65f1bf27bc</a>
34+
<a href="http://full" class="tw-font-semibold" rel="nofollow">repo1/README.md</a>
35+
repo.code_preview_line_from_to:1,2,<a href="/user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d" class="muted tw-font-mono tw-text-text" rel="nofollow">65f1bf27bc</a>
3636
</div>
3737
<table class="file-view">
3838
<tbody><tr>
@@ -52,14 +52,14 @@ func TestRenderHelperCodePreview(t *testing.T) {
5252
OwnerName: "user2",
5353
RepoName: "repo1",
5454
CommitID: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
55-
FilePath: "/README.md",
55+
FilePath: "README.md",
5656
LineStart: 1,
5757
})
5858
assert.NoError(t, err)
5959
assert.Equal(t, `<div class="code-preview-container file-content">
6060
<div class="code-preview-header">
61-
<a href="http://full" class="muted" rel="nofollow">/README.md</a>
62-
repo.code_preview_line_in:1,<a href="/user2/repo1/src/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d" rel="nofollow">65f1bf27bc</a>
61+
<a href="http://full" class="tw-font-semibold" rel="nofollow">repo1/README.md</a>
62+
repo.code_preview_line_in:1,<a href="/user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d" class="muted tw-font-mono tw-text-text" rel="nofollow">65f1bf27bc</a>
6363
</div>
6464
<table class="file-view">
6565
<tbody><tr>
@@ -76,7 +76,7 @@ func TestRenderHelperCodePreview(t *testing.T) {
7676
OwnerName: "user15",
7777
RepoName: "big_test_private_1",
7878
CommitID: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
79-
FilePath: "/README.md",
79+
FilePath: "README.md",
8080
LineStart: 1,
8181
LineStop: 10,
8282
})

templates/base/markup_codepreview.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="code-preview-container file-content">
22
<div class="code-preview-header">
3-
<a href="{{.FullURL}}" class="muted" rel="nofollow">{{.FilePath}}</a>
4-
{{$link := HTMLFormat `<a href="%s/src/commit/%s" rel="nofollow">%s</a>` .RepoLink .CommitID (.CommitID | ShortSha) -}}
3+
<a href="{{.FullURL}}" class="tw-font-semibold" rel="nofollow">{{.RepoName}}/{{.FilePath}}</a>
4+
{{$link := HTMLFormat `<a href="%s/commit/%s" class="muted tw-font-mono tw-text-text" rel="nofollow">%s</a>` .RepoLink .CommitID (.CommitID | ShortSha) -}}
55
{{- if eq .LineStart .LineStop -}}
66
{{ctx.Locale.Tr "repo.code_preview_line_in" .LineStart $link}}
77
{{- else -}}

web_src/css/markup/codepreview.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
}
66

77
.markup .code-preview-container .code-preview-header {
8+
color: var(--color-text-light-1);
89
border-bottom: 1px solid var(--color-secondary);
910
padding: 0.5em;
1011
font-size: 12px;

0 commit comments

Comments
 (0)