-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinline_code.go
More file actions
108 lines (86 loc) · 2.02 KB
/
inline_code.go
File metadata and controls
108 lines (86 loc) · 2.02 KB
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
package hype
import (
"bytes"
"encoding/json"
"fmt"
"html"
"strings"
)
type InlineCode struct {
*Element
}
func (code *InlineCode) MarshalJSON() ([]byte, error) {
if code == nil {
return nil, ErrIsNil("inline code")
}
code.RLock()
defer code.RUnlock()
m, err := code.JSONMap()
if err != nil {
return nil, err
}
m["type"] = toType(code)
return json.MarshalIndent(m, "", " ")
}
func (code *InlineCode) String() string {
if code == nil || code.Element == nil {
return "<code></code>"
}
bb := &bytes.Buffer{}
fmt.Fprint(bb, code.StartTag())
body := code.Nodes.String()
body = html.EscapeString(body)
fmt.Fprint(bb, body)
fmt.Fprint(bb, code.EndTag())
return bb.String()
}
func (code *InlineCode) MD() string {
if code == nil || code.Element == nil {
return ""
}
content := code.Nodes.String()
// Content with backticks needs more backticks to escape
if strings.Contains(content, "`") {
maxTicks := countMaxConsecutiveBackticks(content)
fence := strings.Repeat("`", maxTicks+1)
// Add spaces when content starts/ends with backtick
if strings.HasPrefix(content, "`") || strings.HasSuffix(content, "`") {
return fmt.Sprintf("%s %s %s", fence, content, fence)
}
return fmt.Sprintf("%s%s%s", fence, content, fence)
}
return fmt.Sprintf("`%s`", content)
}
// countMaxConsecutiveBackticks counts the maximum number of consecutive
// backticks in a string. This is used to determine how many backticks
// are needed to fence inline code that contains backticks.
func countMaxConsecutiveBackticks(s string) int {
max, current := 0, 0
for _, r := range s {
if r == '`' {
current++
if current > max {
max = current
}
} else {
current = 0
}
}
return max
}
func NewInlineCode(el *Element) (*InlineCode, error) {
if el == nil {
return nil, ErrIsNil("element")
}
code := &InlineCode{
Element: el,
}
return code, nil
}
func NewInlineCodeNodes(p *Parser, el *Element) (Nodes, error) {
code, err := NewInlineCode(el)
if err != nil {
return nil, err
}
return Nodes{code}, nil
}