forked from Shiloh/githaven
Fix markdown color code detection (#30208)
When reviewing PRs, some color names might be mentioned, the `transformCodeSpan` (which calls `css.ColorHandler`) considered it as a valid color, but actually it shouldn't be rendered as a color codespan.
This commit is contained in:
parent
6d34ce25b1
commit
ab028356c7
@ -436,6 +436,10 @@ func TestColorPreview(t *testing.T) {
|
||||
testcase string
|
||||
expected string
|
||||
}{
|
||||
{ // do not render color names
|
||||
"The CSS class `red` is there",
|
||||
"<p>The CSS class <code>red</code> is there</p>\n",
|
||||
},
|
||||
{ // hex
|
||||
"`#FF0000`",
|
||||
`<p><code>#FF0000<span class="color-preview" style="background-color: #FF0000"></span></code></p>` + nl,
|
||||
@ -445,8 +449,8 @@ func TestColorPreview(t *testing.T) {
|
||||
`<p><code>rgb(16, 32, 64)<span class="color-preview" style="background-color: rgb(16, 32, 64)"></span></code></p>` + nl,
|
||||
},
|
||||
{ // short hex
|
||||
"This is the color white `#000`",
|
||||
`<p>This is the color white <code>#000<span class="color-preview" style="background-color: #000"></span></code></p>` + nl,
|
||||
"This is the color white `#0a0`",
|
||||
`<p>This is the color white <code>#0a0<span class="color-preview" style="background-color: #0a0"></span></code></p>` + nl,
|
||||
},
|
||||
{ // hsl
|
||||
"HSL stands for hue, saturation, and lightness. An example: `hsl(0, 100%, 50%)`.",
|
||||
|
@ -49,9 +49,28 @@ func (r *HTMLRenderer) renderCodeSpan(w util.BufWriter, source []byte, n ast.Nod
|
||||
return ast.WalkContinue, nil
|
||||
}
|
||||
|
||||
// cssColorHandler checks if a string is a render-able CSS color value.
|
||||
// The code is from "github.com/microcosm-cc/bluemonday/css.ColorHandler", except that it doesn't handle color words like "red".
|
||||
func cssColorHandler(value string) bool {
|
||||
value = strings.ToLower(value)
|
||||
if css.HexRGB.MatchString(value) {
|
||||
return true
|
||||
}
|
||||
if css.RGB.MatchString(value) {
|
||||
return true
|
||||
}
|
||||
if css.RGBA.MatchString(value) {
|
||||
return true
|
||||
}
|
||||
if css.HSL.MatchString(value) {
|
||||
return true
|
||||
}
|
||||
return css.HSLA.MatchString(value)
|
||||
}
|
||||
|
||||
func (g *ASTTransformer) transformCodeSpan(ctx *markup.RenderContext, v *ast.CodeSpan, reader text.Reader) {
|
||||
colorContent := v.Text(reader.Source())
|
||||
if css.ColorHandler(strings.ToLower(string(colorContent))) {
|
||||
if cssColorHandler(string(colorContent)) {
|
||||
v.AppendChild(v, NewColorPreview(colorContent))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user