-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
57 lines (53 loc) · 1.65 KB
/
helper.go
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
package media
import (
"bytes"
"fmt"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/util"
"sort"
)
func dumpAttributes(n ast.Node, source []byte, level int) {
attrs := n.Attributes()
list := map[string]string{}
for _, attr := range attrs {
name := util.BytesToReadOnlyString(attr.Name)
// fallback for values that are not []byte
s := fmt.Sprintf("%v", attr.Value)
value := util.BytesToReadOnlyString(util.EscapeHTML([]byte(s)))
list[name] = value
}
ast.DumpHelper(n, source, level, list, nil)
}
// renderTagWithAttributesNoClosing will render a self-closing tag ending in />
func renderTagWithAttributesSelfClose(n ast.Node, tagName string) string {
return renderTagWithAttributesNoClosing(n, tagName) + " />"
}
// renderTagWithAttributesNoClosing will render a tag without closing it with >
func renderTagWithAttributesNoClosing(n ast.Node, tagName string) string {
attrs := n.Attributes()
sort.Slice(attrs, func(i, j int) bool {
return bytes.Compare(attrs[i].Name, attrs[j].Name) > 0
})
html := "<" + tagName
for _, e := range attrs {
var value string
if s, isStr := e.Value.(string); isStr {
if s == "" {
continue
}
} else if b, isBool := e.Value.(bool); isBool {
if !b {
continue
}
// The values “true” and “false” are not allowed on boolean attributes.
// To represent a false value, the attribute has to be omitted altogether.
html += " " + string(e.Name)
continue
}
value = fmt.Sprintf("%s", e.Value)
escaped := util.BytesToReadOnlyString(util.EscapeHTML([]byte(value)))
html += " " + string(e.Name)
html += "=\"" + string(util.EscapeHTML([]byte(escaped))) + "\""
}
return html
}