Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c11a8e2

Browse files
authoredNov 9, 2022
Merge branch 'main' into fix-lang
2 parents 242d90f + cb83288 commit c11a8e2

File tree

7 files changed

+169
-49
lines changed

7 files changed

+169
-49
lines changed
 

‎modules/html/html.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package html
6+
7+
// ParseSizeAndClass get size and class from string with default values
8+
// If present, "others" expects the new size first and then the classes to use
9+
func ParseSizeAndClass(defaultSize int, defaultClass string, others ...interface{}) (int, string) {
10+
if len(others) == 0 {
11+
return defaultSize, defaultClass
12+
}
13+
14+
size := defaultSize
15+
_size, ok := others[0].(int)
16+
if ok && _size != 0 {
17+
size = _size
18+
}
19+
20+
if len(others) == 1 {
21+
return size, defaultClass
22+
}
23+
24+
class := defaultClass
25+
if _class, ok := others[1].(string); ok && _class != "" {
26+
if defaultClass == "" {
27+
class = _class
28+
} else {
29+
class = defaultClass + " " + _class
30+
}
31+
}
32+
33+
return size, class
34+
}

‎modules/markup/markdown/ast.go

+34
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,37 @@ func IsColorPreview(node ast.Node) bool {
180180
_, ok := node.(*ColorPreview)
181181
return ok
182182
}
183+
184+
const (
185+
AttentionNote string = "Note"
186+
AttentionWarning string = "Warning"
187+
)
188+
189+
// Attention is an inline for a color preview
190+
type Attention struct {
191+
ast.BaseInline
192+
AttentionType string
193+
}
194+
195+
// Dump implements Node.Dump.
196+
func (n *Attention) Dump(source []byte, level int) {
197+
m := map[string]string{}
198+
m["AttentionType"] = n.AttentionType
199+
ast.DumpHelper(n, source, level, m, nil)
200+
}
201+
202+
// KindAttention is the NodeKind for Attention
203+
var KindAttention = ast.NewNodeKind("Attention")
204+
205+
// Kind implements Node.Kind.
206+
func (n *Attention) Kind() ast.NodeKind {
207+
return KindAttention
208+
}
209+
210+
// NewAttention returns a new Attention node.
211+
func NewAttention(attentionType string) *Attention {
212+
return &Attention{
213+
BaseInline: ast.BaseInline{},
214+
AttentionType: attentionType,
215+
}
216+
}

‎modules/markup/markdown/goldmark.go

+37
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/markup"
1515
"code.gitea.io/gitea/modules/markup/common"
1616
"code.gitea.io/gitea/modules/setting"
17+
"code.gitea.io/gitea/modules/svg"
1718
giteautil "code.gitea.io/gitea/modules/util"
1819

1920
"github.com/microcosm-cc/bluemonday/css"
@@ -46,6 +47,7 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
4647
ctx.TableOfContents = make([]markup.Header, 0, 100)
4748
}
4849

50+
attentionMarkedBlockquotes := make(container.Set[*ast.Blockquote])
4951
_ = ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
5052
if !entering {
5153
return ast.WalkContinue, nil
@@ -184,6 +186,18 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
184186
if css.ColorHandler(strings.ToLower(string(colorContent))) {
185187
v.AppendChild(v, NewColorPreview(colorContent))
186188
}
189+
case *ast.Emphasis:
190+
// check if inside blockquote for attention, expected hierarchy is
191+
// Emphasis < Paragraph < Blockquote
192+
blockquote, isInBlockquote := n.Parent().Parent().(*ast.Blockquote)
193+
if isInBlockquote && !attentionMarkedBlockquotes.Contains(blockquote) {
194+
fullText := string(n.Text(reader.Source()))
195+
if fullText == AttentionNote || fullText == AttentionWarning {
196+
v.SetAttributeString("class", []byte("attention-"+strings.ToLower(fullText)))
197+
v.Parent().InsertBefore(v.Parent(), v, NewAttention(fullText))
198+
attentionMarkedBlockquotes.Add(blockquote)
199+
}
200+
}
187201
}
188202
return ast.WalkContinue, nil
189203
})
@@ -273,6 +287,7 @@ func (r *HTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
273287
reg.Register(KindSummary, r.renderSummary)
274288
reg.Register(KindIcon, r.renderIcon)
275289
reg.Register(ast.KindCodeSpan, r.renderCodeSpan)
290+
reg.Register(KindAttention, r.renderAttention)
276291
reg.Register(KindTaskCheckBoxListItem, r.renderTaskCheckBoxListItem)
277292
reg.Register(east.KindTaskCheckBox, r.renderTaskCheckBox)
278293
}
@@ -309,6 +324,28 @@ func (r *HTMLRenderer) renderCodeSpan(w util.BufWriter, source []byte, n ast.Nod
309324
return ast.WalkContinue, nil
310325
}
311326

327+
// renderAttention renders a quote marked with i.e. "> **Note**" or "> **Warning**" with a corresponding svg
328+
func (r *HTMLRenderer) renderAttention(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
329+
if entering {
330+
_, _ = w.WriteString(`<span class="attention-icon attention-`)
331+
n := node.(*Attention)
332+
_, _ = w.WriteString(strings.ToLower(n.AttentionType))
333+
_, _ = w.WriteString(`">`)
334+
335+
var octiconType string
336+
switch n.AttentionType {
337+
case AttentionNote:
338+
octiconType = "info"
339+
case AttentionWarning:
340+
octiconType = "alert"
341+
}
342+
_, _ = w.WriteString(string(svg.RenderHTML("octicon-" + octiconType)))
343+
} else {
344+
_, _ = w.WriteString("</span>\n")
345+
}
346+
return ast.WalkContinue, nil
347+
}
348+
312349
func (r *HTMLRenderer) renderDocument(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
313350
n := node.(*ast.Document)
314351

‎modules/markup/sanitizer.go

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ func createDefaultPolicy() *bluemonday.Policy {
5858
// For color preview
5959
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^color-preview$`)).OnElements("span")
6060

61+
// For attention
62+
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^attention-\w+$`)).OnElements("strong")
63+
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^attention-icon attention-\w+$`)).OnElements("span", "strong")
64+
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^svg octicon-\w+$`)).OnElements("svg")
65+
policy.AllowAttrs("viewBox", "width", "height", "aria-hidden").OnElements("svg")
66+
policy.AllowAttrs("fill-rule", "d").OnElements("path")
67+
6168
// For Chroma markdown plugin
6269
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^(chroma )?language-[\w-]+( display)?( is-loading)?$`)).OnElements("code")
6370

‎modules/svg/svg.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,43 @@
44

55
package svg
66

7-
// SVGs contains discovered SVGs
8-
var SVGs map[string]string
7+
import (
8+
"fmt"
9+
"html/template"
10+
"regexp"
11+
"strings"
12+
13+
"code.gitea.io/gitea/modules/html"
14+
)
15+
16+
var (
17+
// SVGs contains discovered SVGs
18+
SVGs map[string]string
19+
20+
widthRe = regexp.MustCompile(`width="[0-9]+?"`)
21+
heightRe = regexp.MustCompile(`height="[0-9]+?"`)
22+
)
23+
24+
const defaultSize = 16
925

1026
// Init discovers SVGs and populates the `SVGs` variable
1127
func Init() {
1228
SVGs = Discover()
1329
}
30+
31+
// Render render icons - arguments icon name (string), size (int), class (string)
32+
func RenderHTML(icon string, others ...interface{}) template.HTML {
33+
size, class := html.ParseSizeAndClass(defaultSize, "", others...)
34+
35+
if svgStr, ok := SVGs[icon]; ok {
36+
if size != defaultSize {
37+
svgStr = widthRe.ReplaceAllString(svgStr, fmt.Sprintf(`width="%d"`, size))
38+
svgStr = heightRe.ReplaceAllString(svgStr, fmt.Sprintf(`height="%d"`, size))
39+
}
40+
if class != "" {
41+
svgStr = strings.Replace(svgStr, `class="`, fmt.Sprintf(`class="%s `, class), 1)
42+
}
43+
return template.HTML(svgStr)
44+
}
45+
return template.HTML("")
46+
}

‎modules/templates/helper.go

+8-47
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"code.gitea.io/gitea/modules/emoji"
3636
"code.gitea.io/gitea/modules/git"
3737
giturl "code.gitea.io/gitea/modules/git/url"
38+
gitea_html "code.gitea.io/gitea/modules/html"
3839
"code.gitea.io/gitea/modules/json"
3940
"code.gitea.io/gitea/modules/log"
4041
"code.gitea.io/gitea/modules/markup"
@@ -348,7 +349,7 @@ func NewFuncMap() []template.FuncMap {
348349
}
349350
return false
350351
},
351-
"svg": SVG,
352+
"svg": svg.RenderHTML,
352353
"avatar": Avatar,
353354
"avatarHTML": AvatarHTML,
354355
"avatarByAction": AvatarByAction,
@@ -363,17 +364,17 @@ func NewFuncMap() []template.FuncMap {
363364
if len(urlSort) == 0 && isDefault {
364365
// if sort is sorted as default add arrow tho this table header
365366
if isDefault {
366-
return SVG("octicon-triangle-down", 16)
367+
return svg.RenderHTML("octicon-triangle-down", 16)
367368
}
368369
} else {
369370
// if sort arg is in url test if it correlates with column header sort arguments
370371
// the direction of the arrow should indicate the "current sort order", up means ASC(normal), down means DESC(rev)
371372
if urlSort == normSort {
372373
// the table is sorted with this header normal
373-
return SVG("octicon-triangle-up", 16)
374+
return svg.RenderHTML("octicon-triangle-up", 16)
374375
} else if urlSort == revSort {
375376
// the table is sorted with this header reverse
376-
return SVG("octicon-triangle-down", 16)
377+
return svg.RenderHTML("octicon-triangle-down", 16)
377378
}
378379
}
379380
// the table is NOT sorted with this header
@@ -594,29 +595,6 @@ func NewTextFuncMap() []texttmpl.FuncMap {
594595
}}
595596
}
596597

597-
var (
598-
widthRe = regexp.MustCompile(`width="[0-9]+?"`)
599-
heightRe = regexp.MustCompile(`height="[0-9]+?"`)
600-
)
601-
602-
func parseOthers(defaultSize int, defaultClass string, others ...interface{}) (int, string) {
603-
size := defaultSize
604-
if len(others) > 0 && others[0].(int) != 0 {
605-
size = others[0].(int)
606-
}
607-
608-
class := defaultClass
609-
if len(others) > 1 && others[1].(string) != "" {
610-
if defaultClass == "" {
611-
class = others[1].(string)
612-
} else {
613-
class = defaultClass + " " + others[1].(string)
614-
}
615-
}
616-
617-
return size, class
618-
}
619-
620598
// AvatarHTML creates the HTML for an avatar
621599
func AvatarHTML(src string, size int, class, name string) template.HTML {
622600
sizeStr := fmt.Sprintf(`%d`, size)
@@ -628,26 +606,9 @@ func AvatarHTML(src string, size int, class, name string) template.HTML {
628606
return template.HTML(`<img class="` + class + `" src="` + src + `" title="` + html.EscapeString(name) + `" width="` + sizeStr + `" height="` + sizeStr + `"/>`)
629607
}
630608

631-
// SVG render icons - arguments icon name (string), size (int), class (string)
632-
func SVG(icon string, others ...interface{}) template.HTML {
633-
size, class := parseOthers(16, "", others...)
634-
635-
if svgStr, ok := svg.SVGs[icon]; ok {
636-
if size != 16 {
637-
svgStr = widthRe.ReplaceAllString(svgStr, fmt.Sprintf(`width="%d"`, size))
638-
svgStr = heightRe.ReplaceAllString(svgStr, fmt.Sprintf(`height="%d"`, size))
639-
}
640-
if class != "" {
641-
svgStr = strings.Replace(svgStr, `class="`, fmt.Sprintf(`class="%s `, class), 1)
642-
}
643-
return template.HTML(svgStr)
644-
}
645-
return template.HTML("")
646-
}
647-
648609
// Avatar renders user avatars. args: user, size (int), class (string)
649610
func Avatar(item interface{}, others ...interface{}) template.HTML {
650-
size, class := parseOthers(avatars.DefaultAvatarPixelSize, "ui avatar vm", others...)
611+
size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, "ui avatar vm", others...)
651612

652613
switch t := item.(type) {
653614
case *user_model.User:
@@ -678,7 +639,7 @@ func AvatarByAction(action *activities_model.Action, others ...interface{}) temp
678639

679640
// RepoAvatar renders repo avatars. args: repo, size(int), class (string)
680641
func RepoAvatar(repo *repo_model.Repository, others ...interface{}) template.HTML {
681-
size, class := parseOthers(avatars.DefaultAvatarPixelSize, "ui avatar", others...)
642+
size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, "ui avatar", others...)
682643

683644
src := repo.RelAvatarLink()
684645
if src != "" {
@@ -689,7 +650,7 @@ func RepoAvatar(repo *repo_model.Repository, others ...interface{}) template.HTM
689650

690651
// AvatarByEmail renders avatars by email address. args: email, name, size (int), class (string)
691652
func AvatarByEmail(email, name string, others ...interface{}) template.HTML {
692-
size, class := parseOthers(avatars.DefaultAvatarPixelSize, "ui avatar", others...)
653+
size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, "ui avatar", others...)
693654
src := avatars.GenerateEmailAvatarFastLink(email, size*setting.Avatar.RenderedSizeFactor)
694655

695656
if src != "" {

‎web_src/less/_base.less

+14
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,20 @@ a.ui.card:hover,
17321732
border-radius: .15em;
17331733
}
17341734

1735+
.attention-icon {
1736+
vertical-align: text-top;
1737+
}
1738+
1739+
.attention-note {
1740+
font-weight: unset;
1741+
color: var(--color-info-text);
1742+
}
1743+
1744+
.attention-warning {
1745+
font-weight: unset;
1746+
color: var(--color-warning-text);
1747+
}
1748+
17351749
footer {
17361750
background-color: var(--color-footer);
17371751
border-top: 1px solid var(--color-secondary);

0 commit comments

Comments
 (0)
Please sign in to comment.