Skip to content

Adding support for markdown in checks description #17980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
a4bcb40
WIP: Add support for markdown in checks
Caellion Dec 14, 2021
9f13a65
Merge branch 'main' of https://github.com/Caellion/gitea into main
Caellion Dec 14, 2021
615028b
update private key to pass tests
Caellion Dec 14, 2021
415175d
naming, markup class
Caellion Dec 14, 2021
6b75d44
also use markdown-supporting statuses in pop-up
Caellion Dec 14, 2021
c9e754b
Update templates/repo/commit_statuses.tmpl
Caellion Dec 15, 2021
fcf972b
Update templates/repo/pulls/status.tmpl
Caellion Dec 15, 2021
c4fd589
Update web_src/less/_base.less
Caellion Dec 15, 2021
358ba98
Merge https://github.com/go-gitea/gitea into upstream-master
Caellion Dec 15, 2021
aa587f5
Update templates/repo/commit_statuses.tmpl
Caellion Dec 15, 2021
d046160
Update templates/repo/pulls/status.tmpl
Caellion Dec 15, 2021
8605af6
Update web_src/less/_base.less
Caellion Dec 15, 2021
fd082dc
Merge branch 'main' of https://github.com/Caellion/gitea
Caellion Dec 15, 2021
3a0d6ef
Update modules/templates/helper.go
Caellion Dec 15, 2021
46a2090
partial suggested changes
Caellion Dec 15, 2021
c37264d
it actually is log.Warn
Caellion Dec 15, 2021
fda6ca3
`template.HTML(html.EscapeString(raw))` instead of `template.HTML(mar…
Caellion Dec 15, 2021
5e11356
Merge branch 'main' into main
Caellion Dec 15, 2021
ea7782a
Merge branch 'go-gitea:main' into main
Caellion Dec 16, 2021
22461a2
Update helper.go
Caellion Dec 16, 2021
b1d817f
Update modules/templates/helper.go
Caellion Dec 16, 2021
fc75a26
Should be what @silverwind wanted, but I don't have working gofmt her…
Caellion Dec 16, 2021
0624676
forgot to `save` before `commit`
Caellion Dec 16, 2021
017a609
go please... these are comments, not code
Caellion Dec 16, 2021
72b83ff
fixed comment again
Caellion Dec 16, 2021
aec7916
Merge branch 'main' into main
Caellion Dec 16, 2021
c1d1d4a
`markupType` instead of `Type`
Caellion Dec 16, 2021
fe29360
Merge branch 'main' of https://github.com/Caellion/gitea into main
Caellion Dec 16, 2021
52124b7
it was meant to be `raw`
Caellion Dec 16, 2021
7c401a3
but not that much `raw`
Caellion Dec 16, 2021
252cfe2
ok, now i think i got that right, if error, then raw, else rendered
Caellion Dec 16, 2021
585dfe5
Apply suggestions from code review
Caellion Dec 17, 2021
671b457
Merge branch 'main' into main
Caellion Dec 17, 2021
1905d60
Merge branch 'main' into main
Caellion Dec 20, 2021
f98f234
Merge branch 'main' into main
Caellion Dec 27, 2021
b4eec88
Merge branch 'go-gitea:main' into main
Caellion Jan 20, 2022
47131ad
Merge branch 'go-gitea:main' into main
Caellion Feb 2, 2022
182d358
gofumpt
Caellion Feb 2, 2022
8adf230
Merge branch 'main' of https://github.com/go-gitea/gitea
Caellion Feb 16, 2022
450ed1a
Merge branch 'main' into main
Caellion Feb 24, 2022
5658b43
Merge branch 'go-gitea:main' into main
Caellion Mar 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/svg"
Expand Down Expand Up @@ -98,6 +99,7 @@ func NewFuncMap() []template.FuncMap {
"SafeJS": SafeJS,
"JSEscape": JSEscape,
"Str2html": Str2html,
"RenderMarkup": RenderMarkup,
"TimeSince": timeutil.TimeSince,
"TimeSinceUnix": timeutil.TimeSinceUnix,
"RawTimeSince": timeutil.RawTimeSince,
Expand Down Expand Up @@ -622,6 +624,28 @@ func Str2html(raw string) template.HTML {
return template.HTML(markup.Sanitize(raw))
}

// RenderMarkup render markdown/markup specified in type to sanitized HTML
func RenderMarkup(markupType, raw string) template.HTML {
var err error
var renderedContent string

if markupType == "markdown" {
if renderedContent, err = markdown.RenderString(&markup.RenderContext{}, raw); err != nil {
log.Warn("RenderMarkup: Invalid markdown? %v", err)
return template.HTML(html.EscapeString(raw))
}
return template.HTML(markup.Sanitize(renderedContent))
} else if markupType == "markup" {
if renderedContent, err = markup.RenderString(&markup.RenderContext{}, raw); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has already included markdown rendering.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it seems we just needs markdown here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was requested by sliverwind . #17980 (review)

Personally I also prefer to keep it simple and only introduce one render.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess we can actually simplify and only support markdown, at least until we can detect other types (like RST) based on entered content.

By the way, is there speaking anything against just using Str2html?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIR it didn't render markdown tables

Copy link
Member

@silverwind silverwind Feb 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah seems like Str2html is mislabeled and actually renders HTML strings into template.HMTL, so markdown.RenderString seems fine.

log.Warn("RenderMarkup: Invalid markup? %v", err)
return template.HTML(html.EscapeString(raw))
}
return template.HTML(markup.Sanitize(renderedContent))
}

return template.HTML(fmt.Sprintf(`RenderMarkup: Unsupported markup type: %s`, markupType))
}

// Escape escapes a HTML string
func Escape(raw string) string {
return html.EscapeString(raw)
Expand Down
1 change: 1 addition & 0 deletions templates/repo/commit_statuses.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{{if .TargetURL}}
<div class="ui"><a href="{{.TargetURL}}" target="_blank" rel="noopener noreferrer">{{$.root.i18n.Tr "repo.pulls.status_checks_details"}}</a></div>
{{end}}
<div class="ui markup mt-4">{{RenderMarkup "markdown" .Description}}</div>
</div>
{{end}}
</div>
Expand Down
3 changes: 2 additions & 1 deletion templates/repo/pulls/status.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
{{range $.LatestCommitStatuses}}
<div class="ui attached segment">
<span>{{template "repo/commit_status" .}}</span>
<span class="ui">{{.Context}} <span class="text grey">{{.Description}}</span></span>
<span class="ui">{{.Context}}</span>
<div class="ui right">
{{if $.is_context_required}}
{{if (call $.is_context_required .Context)}}<div class="ui label">{{$.i18n.Tr "repo.pulls.status_checks_requested"}}</div>{{end}}
{{end}}
<span class="ui">{{if .TargetURL}}<a href="{{.TargetURL}}">{{$.i18n.Tr "repo.pulls.status_checks_details"}}</a>{{end}}</span>
</div>
<div class="ui markup mt-4">{{RenderMarkup "markdown" .Description}}</div>
</div>
{{end}}
{{end}}