Skip to content

Commit

Permalink
Small upgrades
Browse files Browse the repository at this point in the history
- 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;
  • Loading branch information
HenriquerPimentel committed May 26, 2024
1 parent 271c274 commit 0656f41
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions modules/markup/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bufio"
"html"
"io"
"net/url"
"regexp"
"strconv"

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

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

// Check if maxRows or maxSize is reached, and if true, warn.
if (row >= maxRows && maxRows != 0) || (rd.InputOffset() >= maxSize && maxSize != 0) {
locale := ctx.Ctx.Value(translation.ContextKey).(translation.Locale)
warn := `<table class="data-table"><tr><td>`
raw_link := ` <a href="` + ctx.Links.RawLink() + `/` + url.PathEscape(ctx.RelativePath) + `">`

// Try to get the user translation
if locale, ok := ctx.Ctx.Value(translation.ContextKey).(translation.Locale); ok {
warn += locale.TrString("repo.file_too_large")
raw_link += locale.TrString("repo.file_view_raw")
} else {
warn += "The file is too large to be shown."
raw_link += "View Raw"
}

// Construct the HTML string
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>`
warn += raw_link + `</a></td></tr></table>`

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

0 comments on commit 0656f41

Please sign in to comment.