Skip to content

Commit b8cb358

Browse files
Small upgrades
- Removed tags from sanitizer rules; - The warning message now is a table (to reuse UI elements); - ctx.RelativePath escaped; - Implemented a panic catch when getting a translation error;
1 parent 90030ac commit b8cb358

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

modules/markup/csv/csv.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bufio"
88
"html"
99
"io"
10+
"net/url"
1011
"regexp"
1112
"strconv"
1213

@@ -39,8 +40,6 @@ func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
3940
{Element: "table", AllowAttr: "class", Regexp: regexp.MustCompile(`data-table`)},
4041
{Element: "th", AllowAttr: "class", Regexp: regexp.MustCompile(`line-num`)},
4142
{Element: "td", AllowAttr: "class", Regexp: regexp.MustCompile(`line-num`)},
42-
{Element: "div", AllowAttr: "class", Regexp: regexp.MustCompile(`tw-flex tw-justify-center tw-items-center tw-py-4 tw-text-14`)},
43-
{Element: "a", AllowAttr: "href", Regexp: regexp.MustCompile(``)},
4443
}
4544
}
4645

@@ -134,10 +133,19 @@ func (r Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.W
134133

135134
// Check if maxRows or maxSize is reached, and if true, warn.
136135
if (row >= maxRows && maxRows != 0) || (rd.InputOffset() >= maxSize && maxSize != 0) {
137-
locale := ctx.Ctx.Value(translation.ContextKey).(translation.Locale)
136+
warn := `<table class="data-table"><tr><td>`
137+
raw_link := ` <a href="` + ctx.Links.RawLink() + `/` + url.PathEscape(ctx.RelativePath) + `">`
138+
139+
// Try to get the user translation
140+
if locale, ok := ctx.Ctx.Value(translation.ContextKey).(translation.Locale); ok {
141+
warn += locale.TrString("repo.file_too_large")
142+
raw_link += locale.TrString("repo.file_view_raw")
143+
} else {
144+
warn += "The file is too large to be shown."
145+
raw_link += "View Raw"
146+
}
138147

139-
// Construct the HTML string
140-
warn := `<div class="tw-flex tw-justify-center tw-items-center tw-py-4 tw-text-14"><div>` + locale.TrString("repo.file_too_large") + ` <a class="source" href="` + ctx.Links.RawLink() + `/` + ctx.RelativePath + `">` + locale.TrString("repo.file_view_raw") + `</a></div></div>`
148+
warn += raw_link + `</a></td></tr></table>`
141149

142150
// Write the HTML string to the output
143151
if _, err := tmpBlock.WriteString(warn); err != nil {

0 commit comments

Comments
 (0)