-
Notifications
You must be signed in to change notification settings - Fork 1
/
shortcode.go
113 lines (99 loc) · 2.46 KB
/
shortcode.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
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
package markdown
import (
"bytes"
"fmt"
"github.com/yuin/goldmark/ast"
gast "github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
)
var (
// EnableShortcodes context key for indicates if shortcodes enabled
EnableShortcodes = parser.NewContextKey()
// UsedShortcodesKey store list of used shortcodes on page
UsedShortcodesKey = parser.NewContextKey()
)
// ShortCodeNodeKinds mapping `keyword -> NodeKind`
var ShortCodeNodeKinds = map[string]gast.NodeKind{
tocKeyword: gast.NewNodeKind("TOCShortcode"),
}
// A ShortCodeNode struct represents a tweet ast node
type ShortCodeNode struct {
gast.BaseInline
kind ast.NodeKind
params [][]byte
context interface{}
}
// Kind implements Node.Kind.
func (n *ShortCodeNode) Kind() gast.NodeKind {
return n.kind
}
// Dump for ShortCodeNode
func (n *ShortCodeNode) Dump(source []byte, level int) {
gast.DumpHelper(n, source, level, nil, nil)
}
// --- parser ---
type shortCodeParser struct {
keyword string
minArgc int
parserContextKey *parser.ContextKey
}
func (s *shortCodeParser) Trigger() []byte {
return []byte{'{'}
}
func (s *shortCodeParser) Parse(parent gast.Node, block text.Reader, pc parser.Context) gast.Node {
if enabled, ok := pc.Get(EnableShortcodes).(bool); ok && !enabled {
return nil
}
ln, _ := block.PeekLine()
if !bytes.HasPrefix(ln, []byte("{{")) {
return nil
}
endPos := bytes.Index(ln, []byte("}}"))
if endPos < 0 {
return nil
}
body := bytes.TrimSpace(ln[2:endPos])
kwMatch := false
params := [][]byte{}
for _, w := range bytes.Split(body, []byte{' '}) {
if len(w) == 0 {
continue
}
if kwMatch {
params = append(params, w)
continue
}
if s.keyword == string(w) {
kwMatch = true
} else {
return nil
}
}
usedShortcodes, ok := pc.Get(UsedShortcodesKey).(map[string]bool)
if !ok {
usedShortcodes = map[string]bool{}
pc.Set(UsedShortcodesKey, usedShortcodes)
}
usedShortcodes[s.keyword] = true
if s.minArgc > 0 && len(params) < s.minArgc {
return nil
}
block.Advance(endPos + 2)
kind, ok := ShortCodeNodeKinds[s.keyword]
if !ok {
panic(fmt.Errorf("missing NodeKind for %s", s.keyword))
}
node := &ShortCodeNode{
kind: kind,
params: params,
}
if s.parserContextKey != nil {
var elems []ast.Node
if exitedElems, ok := pc.Get(*s.parserContextKey).([]gast.Node); ok && exitedElems != nil {
elems = exitedElems
}
pc.Set(*s.parserContextKey, append(elems, node))
}
return node
}