Skip to content

Commit 66902d8

Browse files
authored
Refactor markdown attention render (#29833)
* Remove some deadcode * Use 2-word name for CSS class names * Remove "gt-*" rules for sanitizer The UI doesn't change much.
1 parent 1262ff6 commit 66902d8

File tree

4 files changed

+23
-38
lines changed

4 files changed

+23
-38
lines changed

modules/markup/markdown/ast.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,6 @@ func NewColorPreview(color []byte) *ColorPreview {
175175
}
176176
}
177177

178-
// IsColorPreview returns true if the given node implements the ColorPreview interface,
179-
// otherwise false.
180-
func IsColorPreview(node ast.Node) bool {
181-
_, ok := node.(*ColorPreview)
182-
return ok
183-
}
184-
185178
// Attention is an inline for an attention
186179
type Attention struct {
187180
ast.BaseInline

modules/markup/markdown/goldmark.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
216216
attentionType := strings.ToLower(strings.TrimPrefix(string(secondTextNode.Segment.Value(reader.Source())), "!"))
217217

218218
// color the blockquote
219-
v.SetAttributeString("class", []byte("gt-py-3 attention attention-"+attentionType))
219+
v.SetAttributeString("class", []byte("attention-header attention-"+attentionType))
220220

221221
// create an emphasis to make it bold
222222
attentionParagraph := ast.NewParagraph()
@@ -364,27 +364,21 @@ func (r *HTMLRenderer) renderCodeSpan(w util.BufWriter, source []byte, n ast.Nod
364364
// renderAttention renders a quote marked with i.e. "> **Note**" or "> **Warning**" with a corresponding svg
365365
func (r *HTMLRenderer) renderAttention(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
366366
if entering {
367-
_, _ = w.WriteString(`<span class="gt-mr-2 gt-vm attention-`)
368367
n := node.(*Attention)
369-
_, _ = w.WriteString(strings.ToLower(n.AttentionType))
370-
_, _ = w.WriteString(`">`)
371-
372-
var octiconType string
368+
var octiconName string
373369
switch n.AttentionType {
374-
case "note":
375-
octiconType = "info"
376370
case "tip":
377-
octiconType = "light-bulb"
371+
octiconName = "light-bulb"
378372
case "important":
379-
octiconType = "report"
373+
octiconName = "report"
380374
case "warning":
381-
octiconType = "alert"
375+
octiconName = "alert"
382376
case "caution":
383-
octiconType = "stop"
377+
octiconName = "stop"
378+
default: // including "note"
379+
octiconName = "info"
384380
}
385-
_, _ = w.WriteString(string(svg.RenderHTML("octicon-" + octiconType)))
386-
} else {
387-
_, _ = w.WriteString("</span>\n")
381+
_, _ = w.WriteString(string(svg.RenderHTML("octicon-"+octiconName, 16, "attention-icon attention-"+n.AttentionType)))
388382
}
389383
return ast.WalkContinue, nil
390384
}

modules/markup/sanitizer.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ func createDefaultPolicy() *bluemonday.Policy {
6464
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^color-preview$`)).OnElements("span")
6565

6666
// For attention
67-
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^gt-py-3 attention attention-\w+$`)).OnElements("blockquote")
67+
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^attention-header attention-\w+$`)).OnElements("blockquote")
6868
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^attention-\w+$`)).OnElements("strong")
69-
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^gt-mr-2 gt-vm attention-\w+$`)).OnElements("span", "strong")
70-
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^svg octicon-(\w|-)+$`)).OnElements("svg")
69+
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^attention-icon attention-\w+ svg octicon-[\w-]+$`)).OnElements("svg")
7170
policy.AllowAttrs("viewBox", "width", "height", "aria-hidden").OnElements("svg")
7271
policy.AllowAttrs("fill-rule", "d").OnElements("path")
7372

@@ -105,18 +104,12 @@ func createDefaultPolicy() *bluemonday.Policy {
105104
// Allow icons
106105
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^icon(\s+[\p{L}\p{N}_-]+)+$`)).OnElements("i")
107106

108-
// Allow unlabelled labels
109-
policy.AllowNoAttrs().OnElements("label")
110-
111107
// Allow classes for emojis
112108
policy.AllowAttrs("class").Matching(regexp.MustCompile(`emoji`)).OnElements("img")
113109

114110
// Allow icons, emojis, chroma syntax and keyword markup on span
115111
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^((icon(\s+[\p{L}\p{N}_-]+)+)|(emoji)|(language-math display)|(language-math inline))$|^([a-z][a-z0-9]{0,2})$|^` + keywordClass + `$`)).OnElements("span")
116112

117-
// Allow 'style' attribute on text elements.
118-
policy.AllowAttrs("style").OnElements("span", "p")
119-
120113
// Allow 'color' and 'background-color' properties for the style attribute on text elements.
121114
policy.AllowStyles("color", "background-color").OnElements("span", "p")
122115

@@ -144,7 +137,7 @@ func createDefaultPolicy() *bluemonday.Policy {
144137

145138
generalSafeElements := []string{
146139
"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "br", "b", "i", "strong", "em", "a", "pre", "code", "img", "tt",
147-
"div", "ins", "del", "sup", "sub", "p", "ol", "ul", "table", "thead", "tbody", "tfoot", "blockquote",
140+
"div", "ins", "del", "sup", "sub", "p", "ol", "ul", "table", "thead", "tbody", "tfoot", "blockquote", "label",
148141
"dl", "dt", "dd", "kbd", "q", "samp", "var", "hr", "ruby", "rt", "rp", "li", "tr", "td", "th", "s", "strike", "summary",
149142
"details", "caption", "figure", "figcaption",
150143
"abbr", "bdo", "cite", "dfn", "mark", "small", "span", "time", "video", "wbr",

web_src/css/base.css

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,42 +1279,47 @@ input:-webkit-autofill:active,
12791279
border-radius: var(--border-radius);
12801280
}
12811281

1282-
.attention {
1282+
.attention-header {
1283+
padding: 0.5em 0.75em !important;
12831284
color: var(--color-text) !important;
12841285
}
12851286

1287+
.attention-icon {
1288+
margin: 2px 6px 0 0;
1289+
}
1290+
12861291
blockquote.attention-note {
12871292
border-left-color: var(--color-blue-dark-1);
12881293
}
1289-
strong.attention-note, span.attention-note {
1294+
strong.attention-note, svg.attention-note {
12901295
color: var(--color-blue-dark-1);
12911296
}
12921297

12931298
blockquote.attention-tip {
12941299
border-left-color: var(--color-success-text);
12951300
}
1296-
strong.attention-tip, span.attention-tip {
1301+
strong.attention-tip, svg.attention-tip {
12971302
color: var(--color-success-text);
12981303
}
12991304

13001305
blockquote.attention-important {
13011306
border-left-color: var(--color-violet-dark-1);
13021307
}
1303-
strong.attention-important, span.attention-important {
1308+
strong.attention-important, svg.attention-important {
13041309
color: var(--color-violet-dark-1);
13051310
}
13061311

13071312
blockquote.attention-warning {
13081313
border-left-color: var(--color-warning-text);
13091314
}
1310-
strong.attention-warning, span.attention-warning {
1315+
strong.attention-warning, svg.attention-warning {
13111316
color: var(--color-warning-text);
13121317
}
13131318

13141319
blockquote.attention-caution {
13151320
border-left-color: var(--color-red-dark-1);
13161321
}
1317-
strong.attention-caution, span.attention-caution {
1322+
strong.attention-caution, svg.attention-caution {
13181323
color: var(--color-red-dark-1);
13191324
}
13201325

0 commit comments

Comments
 (0)