Skip to content

Commit cb83288

Browse files
yardenshohamdelvhsilverwind
authored
Add attention blocks within quote blocks for Note and Warning (#21711)
For each quote block, the first `**Note**` or `**Warning**` gets an icon prepended to it and its text is colored accordingly. GitHub does this (community/community#16925). [Initially requested on Discord.](https://discord.com/channels/322538954119184384/322538954119184384/1038816475638661181) ### Before ![image](https://user-images.githubusercontent.com/20454870/200408558-bd318302-6ff9-4d56-996f-9190e89013ec.png) ### After ![image](https://user-images.githubusercontent.com/20454870/200658863-1bac6461-dae7-4bf2-abd2-672d209574e4.png) Signed-off-by: Yarden Shoham <[email protected]> Co-authored-by: delvh <[email protected]> Co-authored-by: silverwind <[email protected]>
1 parent 2ebab42 commit cb83288

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

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

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)