mirror of https://github.com/go-gitea/gitea.git
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.
This commit is contained in:
parent
1262ff6734
commit
66902d89e5
|
@ -175,13 +175,6 @@ func NewColorPreview(color []byte) *ColorPreview {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsColorPreview returns true if the given node implements the ColorPreview interface,
|
|
||||||
// otherwise false.
|
|
||||||
func IsColorPreview(node ast.Node) bool {
|
|
||||||
_, ok := node.(*ColorPreview)
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attention is an inline for an attention
|
// Attention is an inline for an attention
|
||||||
type Attention struct {
|
type Attention struct {
|
||||||
ast.BaseInline
|
ast.BaseInline
|
||||||
|
|
|
@ -216,7 +216,7 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
|
||||||
attentionType := strings.ToLower(strings.TrimPrefix(string(secondTextNode.Segment.Value(reader.Source())), "!"))
|
attentionType := strings.ToLower(strings.TrimPrefix(string(secondTextNode.Segment.Value(reader.Source())), "!"))
|
||||||
|
|
||||||
// color the blockquote
|
// color the blockquote
|
||||||
v.SetAttributeString("class", []byte("gt-py-3 attention attention-"+attentionType))
|
v.SetAttributeString("class", []byte("attention-header attention-"+attentionType))
|
||||||
|
|
||||||
// create an emphasis to make it bold
|
// create an emphasis to make it bold
|
||||||
attentionParagraph := ast.NewParagraph()
|
attentionParagraph := ast.NewParagraph()
|
||||||
|
@ -364,27 +364,21 @@ func (r *HTMLRenderer) renderCodeSpan(w util.BufWriter, source []byte, n ast.Nod
|
||||||
// renderAttention renders a quote marked with i.e. "> **Note**" or "> **Warning**" with a corresponding svg
|
// renderAttention renders a quote marked with i.e. "> **Note**" or "> **Warning**" with a corresponding svg
|
||||||
func (r *HTMLRenderer) renderAttention(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
func (r *HTMLRenderer) renderAttention(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
||||||
if entering {
|
if entering {
|
||||||
_, _ = w.WriteString(`<span class="gt-mr-2 gt-vm attention-`)
|
|
||||||
n := node.(*Attention)
|
n := node.(*Attention)
|
||||||
_, _ = w.WriteString(strings.ToLower(n.AttentionType))
|
var octiconName string
|
||||||
_, _ = w.WriteString(`">`)
|
|
||||||
|
|
||||||
var octiconType string
|
|
||||||
switch n.AttentionType {
|
switch n.AttentionType {
|
||||||
case "note":
|
|
||||||
octiconType = "info"
|
|
||||||
case "tip":
|
case "tip":
|
||||||
octiconType = "light-bulb"
|
octiconName = "light-bulb"
|
||||||
case "important":
|
case "important":
|
||||||
octiconType = "report"
|
octiconName = "report"
|
||||||
case "warning":
|
case "warning":
|
||||||
octiconType = "alert"
|
octiconName = "alert"
|
||||||
case "caution":
|
case "caution":
|
||||||
octiconType = "stop"
|
octiconName = "stop"
|
||||||
|
default: // including "note"
|
||||||
|
octiconName = "info"
|
||||||
}
|
}
|
||||||
_, _ = w.WriteString(string(svg.RenderHTML("octicon-" + octiconType)))
|
_, _ = w.WriteString(string(svg.RenderHTML("octicon-"+octiconName, 16, "attention-icon attention-"+n.AttentionType)))
|
||||||
} else {
|
|
||||||
_, _ = w.WriteString("</span>\n")
|
|
||||||
}
|
}
|
||||||
return ast.WalkContinue, nil
|
return ast.WalkContinue, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,10 +64,9 @@ func createDefaultPolicy() *bluemonday.Policy {
|
||||||
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^color-preview$`)).OnElements("span")
|
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^color-preview$`)).OnElements("span")
|
||||||
|
|
||||||
// For attention
|
// For attention
|
||||||
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^gt-py-3 attention attention-\w+$`)).OnElements("blockquote")
|
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^attention-header attention-\w+$`)).OnElements("blockquote")
|
||||||
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^attention-\w+$`)).OnElements("strong")
|
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^attention-\w+$`)).OnElements("strong")
|
||||||
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^gt-mr-2 gt-vm attention-\w+$`)).OnElements("span", "strong")
|
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^attention-icon attention-\w+ svg octicon-[\w-]+$`)).OnElements("svg")
|
||||||
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^svg octicon-(\w|-)+$`)).OnElements("svg")
|
|
||||||
policy.AllowAttrs("viewBox", "width", "height", "aria-hidden").OnElements("svg")
|
policy.AllowAttrs("viewBox", "width", "height", "aria-hidden").OnElements("svg")
|
||||||
policy.AllowAttrs("fill-rule", "d").OnElements("path")
|
policy.AllowAttrs("fill-rule", "d").OnElements("path")
|
||||||
|
|
||||||
|
@ -105,18 +104,12 @@ func createDefaultPolicy() *bluemonday.Policy {
|
||||||
// Allow icons
|
// Allow icons
|
||||||
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^icon(\s+[\p{L}\p{N}_-]+)+$`)).OnElements("i")
|
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^icon(\s+[\p{L}\p{N}_-]+)+$`)).OnElements("i")
|
||||||
|
|
||||||
// Allow unlabelled labels
|
|
||||||
policy.AllowNoAttrs().OnElements("label")
|
|
||||||
|
|
||||||
// Allow classes for emojis
|
// Allow classes for emojis
|
||||||
policy.AllowAttrs("class").Matching(regexp.MustCompile(`emoji`)).OnElements("img")
|
policy.AllowAttrs("class").Matching(regexp.MustCompile(`emoji`)).OnElements("img")
|
||||||
|
|
||||||
// Allow icons, emojis, chroma syntax and keyword markup on span
|
// Allow icons, emojis, chroma syntax and keyword markup on span
|
||||||
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")
|
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")
|
||||||
|
|
||||||
// Allow 'style' attribute on text elements.
|
|
||||||
policy.AllowAttrs("style").OnElements("span", "p")
|
|
||||||
|
|
||||||
// Allow 'color' and 'background-color' properties for the style attribute on text elements.
|
// Allow 'color' and 'background-color' properties for the style attribute on text elements.
|
||||||
policy.AllowStyles("color", "background-color").OnElements("span", "p")
|
policy.AllowStyles("color", "background-color").OnElements("span", "p")
|
||||||
|
|
||||||
|
@ -144,7 +137,7 @@ func createDefaultPolicy() *bluemonday.Policy {
|
||||||
|
|
||||||
generalSafeElements := []string{
|
generalSafeElements := []string{
|
||||||
"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "br", "b", "i", "strong", "em", "a", "pre", "code", "img", "tt",
|
"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "br", "b", "i", "strong", "em", "a", "pre", "code", "img", "tt",
|
||||||
"div", "ins", "del", "sup", "sub", "p", "ol", "ul", "table", "thead", "tbody", "tfoot", "blockquote",
|
"div", "ins", "del", "sup", "sub", "p", "ol", "ul", "table", "thead", "tbody", "tfoot", "blockquote", "label",
|
||||||
"dl", "dt", "dd", "kbd", "q", "samp", "var", "hr", "ruby", "rt", "rp", "li", "tr", "td", "th", "s", "strike", "summary",
|
"dl", "dt", "dd", "kbd", "q", "samp", "var", "hr", "ruby", "rt", "rp", "li", "tr", "td", "th", "s", "strike", "summary",
|
||||||
"details", "caption", "figure", "figcaption",
|
"details", "caption", "figure", "figcaption",
|
||||||
"abbr", "bdo", "cite", "dfn", "mark", "small", "span", "time", "video", "wbr",
|
"abbr", "bdo", "cite", "dfn", "mark", "small", "span", "time", "video", "wbr",
|
||||||
|
|
|
@ -1279,42 +1279,47 @@ input:-webkit-autofill:active,
|
||||||
border-radius: var(--border-radius);
|
border-radius: var(--border-radius);
|
||||||
}
|
}
|
||||||
|
|
||||||
.attention {
|
.attention-header {
|
||||||
|
padding: 0.5em 0.75em !important;
|
||||||
color: var(--color-text) !important;
|
color: var(--color-text) !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.attention-icon {
|
||||||
|
margin: 2px 6px 0 0;
|
||||||
|
}
|
||||||
|
|
||||||
blockquote.attention-note {
|
blockquote.attention-note {
|
||||||
border-left-color: var(--color-blue-dark-1);
|
border-left-color: var(--color-blue-dark-1);
|
||||||
}
|
}
|
||||||
strong.attention-note, span.attention-note {
|
strong.attention-note, svg.attention-note {
|
||||||
color: var(--color-blue-dark-1);
|
color: var(--color-blue-dark-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
blockquote.attention-tip {
|
blockquote.attention-tip {
|
||||||
border-left-color: var(--color-success-text);
|
border-left-color: var(--color-success-text);
|
||||||
}
|
}
|
||||||
strong.attention-tip, span.attention-tip {
|
strong.attention-tip, svg.attention-tip {
|
||||||
color: var(--color-success-text);
|
color: var(--color-success-text);
|
||||||
}
|
}
|
||||||
|
|
||||||
blockquote.attention-important {
|
blockquote.attention-important {
|
||||||
border-left-color: var(--color-violet-dark-1);
|
border-left-color: var(--color-violet-dark-1);
|
||||||
}
|
}
|
||||||
strong.attention-important, span.attention-important {
|
strong.attention-important, svg.attention-important {
|
||||||
color: var(--color-violet-dark-1);
|
color: var(--color-violet-dark-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
blockquote.attention-warning {
|
blockquote.attention-warning {
|
||||||
border-left-color: var(--color-warning-text);
|
border-left-color: var(--color-warning-text);
|
||||||
}
|
}
|
||||||
strong.attention-warning, span.attention-warning {
|
strong.attention-warning, svg.attention-warning {
|
||||||
color: var(--color-warning-text);
|
color: var(--color-warning-text);
|
||||||
}
|
}
|
||||||
|
|
||||||
blockquote.attention-caution {
|
blockquote.attention-caution {
|
||||||
border-left-color: var(--color-red-dark-1);
|
border-left-color: var(--color-red-dark-1);
|
||||||
}
|
}
|
||||||
strong.attention-caution, span.attention-caution {
|
strong.attention-caution, svg.attention-caution {
|
||||||
color: var(--color-red-dark-1);
|
color: var(--color-red-dark-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue