Spaces:
Paused
Paused
File size: 3,767 Bytes
83a59ca | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | package emails
import (
"bytes"
"encoding/json"
"html"
"html/template"
"strings"
)
const (
eventPayloadCodeBlockStyle = "margin:0;padding:12px;border:1px solid #D0D7DE;border-radius:6px;background:#F6F8FA;color:#24292F;font-family:Consolas,Monaco,'Courier New',monospace;font-size:13px;line-height:1.5;white-space:pre-wrap;word-break:break-word;overflow-wrap:anywhere;"
jsonKeyStyle = "color:#0550AE;font-weight:600;"
jsonStringStyle = "color:#0A3069;"
jsonNumberStyle = "color:#953800;"
jsonLiteralStyle = "color:#8250DF;"
)
func formatEventPayload(payload string) (string, template.HTML) {
formattedPayload, isJSON := indentEventPayloadJSON(payload)
content := html.EscapeString(formattedPayload)
if isJSON {
content = highlightEventPayloadJSON(formattedPayload)
}
// #nosec G203 -- every dynamic payload token is escaped before the static wrapper is added.
richPayload := template.HTML(`<pre style="` + eventPayloadCodeBlockStyle + `">` + content + `</pre>`)
return formattedPayload, richPayload
}
func indentEventPayloadJSON(payload string) (string, bool) {
var formatted bytes.Buffer
if err := json.Indent(&formatted, []byte(payload), "", " "); err != nil {
return payload, false
}
return formatted.String(), true
}
func highlightEventPayloadJSON(payload string) string {
var highlighted strings.Builder
highlighted.Grow(len(payload))
for index := 0; index < len(payload); {
switch {
case payload[index] == '"':
end := eventPayloadJSONStringEnd(payload, index)
style := jsonStringStyle
if eventPayloadNextNonSpace(payload, end) == ':' {
style = jsonKeyStyle
}
writeEventPayloadToken(&highlighted, style, payload[index:end])
index = end
case payload[index] == '-' || isEventPayloadDigit(payload[index]):
end := index + 1
// json.Indent already validated this as JSON, so this continuation set only sees JSON number bytes.
for end < len(payload) && isEventPayloadNumberCharacter(payload[end]) {
end++
}
writeEventPayloadToken(&highlighted, jsonNumberStyle, payload[index:end])
index = end
case strings.HasPrefix(payload[index:], "true"):
writeEventPayloadToken(&highlighted, jsonLiteralStyle, "true")
index += len("true")
case strings.HasPrefix(payload[index:], "false"):
writeEventPayloadToken(&highlighted, jsonLiteralStyle, "false")
index += len("false")
case strings.HasPrefix(payload[index:], "null"):
writeEventPayloadToken(&highlighted, jsonLiteralStyle, "null")
index += len("null")
default:
highlighted.WriteString(html.EscapeString(payload[index : index+1]))
index++
}
}
return highlighted.String()
}
func eventPayloadJSONStringEnd(payload string, start int) int {
escaped := false
for index := start + 1; index < len(payload); index++ {
switch {
case escaped:
escaped = false
case payload[index] == '\\':
escaped = true
case payload[index] == '"':
return index + 1
}
}
return len(payload)
}
func eventPayloadNextNonSpace(payload string, start int) byte {
for index := start; index < len(payload); index++ {
switch payload[index] {
case ' ', '\n', '\r', '\t':
continue
default:
return payload[index]
}
}
return 0
}
func isEventPayloadDigit(value byte) bool {
return value >= '0' && value <= '9'
}
func isEventPayloadNumberCharacter(value byte) bool {
return isEventPayloadDigit(value) ||
value == '-' ||
value == '+' ||
value == '.' ||
value == 'e' ||
value == 'E'
}
func writeEventPayloadToken(builder *strings.Builder, style string, token string) {
builder.WriteString(`<span style="`)
builder.WriteString(style)
builder.WriteString(`">`)
builder.WriteString(html.EscapeString(token))
builder.WriteString(`</span>`)
}
|