-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikilink.go
191 lines (158 loc) · 4.56 KB
/
wikilink.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package wikilink
import (
"fmt"
"net/url"
"strings"
"github.com/oliger/goldmark-wikilink/ast"
"github.com/yuin/goldmark"
gast "github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
const (
delimiterCount = 2
openDelimiter byte = '['
closeDelimiter byte = ']'
defaultAliasDelimiter byte = ':'
)
// ResolveDestinationFunc maps a raw destination to a file path and determines
// if the file exists.
type ResolveDestinationFunc func(rawDest []byte) (string, bool)
func defaultResolveDestinationFunc(rawDest []byte) (string, bool) {
dest := strings.TrimSpace(string(rawDest))
dest = strings.ToLower(dest)
dest = strings.ReplaceAll(dest, " ", "-")
dest = url.PathEscape(dest)
return dest, true
}
type wikiLinkParser struct {
aliasDelimiter byte
resolveDestinationFunc ResolveDestinationFunc
}
func (p *wikiLinkParser) Trigger() []byte {
return []byte{openDelimiter}
}
func (p *wikiLinkParser) Parse(parent gast.Node, block text.Reader, ctx parser.Context) gast.Node {
line, _ := block.PeekLine()
// Skip lines containing less than 4 characters as they cannot contains
// wiki links.
if len(line) <= 4 || line[1] != openDelimiter {
return nil
}
// Skip open delimiters.
openPos := delimiterCount
closePos := -1
aliasPos := -1
for i := openPos; i < len(line)-1; i++ {
if line[i] == closeDelimiter && line[i+1] == closeDelimiter {
closePos = i
break
}
if line[i] == p.aliasDelimiter {
aliasPos = i
}
}
// Ignore empty wiki links.
if openPos == closePos || closePos == -1 {
return nil
}
wl := &ast.WikiLink{}
if aliasPos == -1 || openPos == aliasPos || aliasPos+1 == closePos {
wl.RawDestination = line[openPos:closePos]
wl.Alias = wl.RawDestination
} else {
wl.RawDestination = line[openPos:aliasPos]
wl.Alias = line[aliasPos+1 : closePos]
}
dest, exists := p.resolveDestinationFunc(wl.RawDestination)
wl.Destination = dest
wl.Exists = exists
block.Advance(closePos + delimiterCount)
return wl
}
// RenderFunc renders a wiki link.
type RenderFunc func(wl *ast.WikiLink) string
func defaultRenderFunc(wl *ast.WikiLink) string {
return fmt.Sprintf(`<a href="%s">%s</a>`, wl.Destination, wl.Alias)
}
type wikilinkRenderer struct {
renderFunc RenderFunc
}
func (r *wikilinkRenderer) RegisterFuncs(register renderer.NodeRendererFuncRegisterer) {
register.Register(ast.KindWikiLink, r.render)
}
func (r *wikilinkRenderer) render(w util.BufWriter, source []byte, n gast.Node, entering bool) (gast.WalkStatus, error) {
if !entering {
return gast.WalkContinue, nil
}
wl := n.(*ast.WikiLink)
w.Write([]byte(r.renderFunc(wl)))
return gast.WalkContinue, nil
}
type wikiLinkExtension struct {
aliasDelimiter byte
resolveDestinationFunc ResolveDestinationFunc
renderFunc RenderFunc
}
func newWithDefaultOptions() *wikiLinkExtension {
return &wikiLinkExtension{
resolveDestinationFunc: defaultResolveDestinationFunc,
aliasDelimiter: defaultAliasDelimiter,
renderFunc: defaultRenderFunc,
}
}
// WikiLink is a goldmark extension configured with default options.
var WikiLink = newWithDefaultOptions()
// New returns a goldmark extension that can be configured with options.
func New(opts ...Option) goldmark.Extender {
ext := newWithDefaultOptions()
for _, opt := range opts {
opt(ext)
}
return ext
}
func (ext *wikiLinkExtension) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(
parser.WithInlineParsers(
util.Prioritized(
&wikiLinkParser{
aliasDelimiter: ext.aliasDelimiter,
resolveDestinationFunc: ext.resolveDestinationFunc,
},
150,
),
),
)
m.Renderer().AddOptions(
renderer.WithNodeRenderers(
util.Prioritized(
&wikilinkRenderer{
renderFunc: ext.renderFunc,
},
500,
),
),
)
}
// Option is used to configure the extension.
type Option func(*wikiLinkExtension)
// WithAliasDelimiter sets alias delimiter.
func WithAliasDelimiter(aliasDelimiter byte) Option {
return func(ext *wikiLinkExtension) {
ext.aliasDelimiter = aliasDelimiter
}
}
// WithResolveDestinationFunc sets the function used to resolve destination.
func WithResolveDestinationFunc(resolveDestinationFunc ResolveDestinationFunc) Option {
return func(ext *wikiLinkExtension) {
ext.resolveDestinationFunc = resolveDestinationFunc
}
}
// WithRenderFunc sets the function used to render wikilinks.
func WithRenderFunc(renderFunc RenderFunc) Option {
return func(ext *wikiLinkExtension) {
ext.renderFunc = renderFunc
}
}