-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshortlinkext.go
81 lines (67 loc) · 2.31 KB
/
shortlinkext.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
package ukuleleweb
import (
"regexp"
"slices"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
var goLinkRE = regexp.MustCompile(`^go/[A-Za-z0-9#/_+öäüÖÄÜß-]+\b`)
// ShortLinkExt is a goldmark extension for recognizing shortlinks
// like go/links.
type ShortLinkExt struct{}
func (s *ShortLinkExt) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(
parser.WithInlineParsers(
// One less than the linkify one - we don't want to mess up http links.
util.Prioritized(&shortLinkParser{}, 998),
),
)
}
// A parser for shortlinks like go/links (resolving to
// http://go/links)
type shortLinkParser struct{}
func (w *shortLinkParser) Trigger() []byte {
return []byte{' ', '('}
}
func (w *shortLinkParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) (res ast.Node) {
if pc.IsInLinkLabel() {
return nil
}
line, segment := block.PeekLine()
// Implementation note:
// The trigger above triggers for the given characters, as well as for newlines.
// Parse() below must be able to recognize both lines starting with "go/link..."
// as well as lines starting with " go/link..." (for any leading trigger character).
// If the line does start with a trigger, then *on a successful parse*,
// that trigger must be inserted into the parent node before returning.
if len(line) > 0 && slices.Contains(w.Trigger(), line[0]) {
prefixSeg := segment.WithStop(segment.Start + 1)
// Move line and segment one character further
// and continue the parsing as if we had not started with a space.
// e.g. line = "go/foo ..." instead of " go/foo ..."
block.Advance(1)
line = line[1:]
segment = segment.WithStart(segment.Start + 1)
// Insert the leading space into the parent AST, if parse was a success.
defer func() {
if res == nil {
return
}
ast.MergeOrAppendTextSegment(parent, prefixSeg)
}()
}
// Match must be at the beginning of the line either way.
m := goLinkRE.FindSubmatchIndex(line)
if m == nil || m[0] != 0 {
return nil
}
linkText := line[0:m[1]]
block.Advance(m[1])
link := ast.NewLink()
link.AppendChild(link, ast.NewTextSegment(text.NewSegment(segment.Start, segment.Start+m[1])))
link.Destination = append([]byte("http://"), linkText...)
return link
}