-
Notifications
You must be signed in to change notification settings - Fork 1
/
autolink.go
64 lines (48 loc) · 1.39 KB
/
autolink.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
package discordmd
import (
"regexp"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
)
// ^ because we already parsed to the start of the URL.
var autolinkRegex = regexp.MustCompile(`^https?:\/\S+(?:\.|:)[^>\s]+`)
type autolink struct{}
func (s autolink) Trigger() []byte {
return []byte{' ', '<'}
}
func (s autolink) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
line, segment := block.PeekLine()
before := line[0]
switch before {
case '<':
// If there's an opener, consume it:
line = line[1:]
segment.Start++
// We defer once now for the opener and once after segmenting for the
// closure.
block.Advance(1)
defer block.Advance(1)
case ' ':
// Consume a space so FindURLIndex doesn't break:
line = line[1:]
block.Advance(1)
}
locs := autolinkRegex.FindIndex(line)
if len(locs) == 0 {
return nil
}
stop := locs[1]
// If we've consumed a space, we should restore the space before the
// URL as well.
if before == ' ' {
// Space? Fine, I'll do it myself. Prepend a space:
s := segment.WithStop(segment.Start + 1)
ast.MergeOrAppendTextSegment(parent, s)
// Consume a space in segment so NewTextSegment works properly:
segment.Start++
}
value := ast.NewTextSegment(text.NewSegment(segment.Start, segment.Start+stop))
block.Advance(stop)
return ast.NewAutoLink(ast.AutoLinkURL, value)
}