-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinkext.go
More file actions
89 lines (75 loc) · 2.58 KB
/
linkext.go
File metadata and controls
89 lines (75 loc) · 2.58 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
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 pageNameRE = regexp.MustCompile(`^([A-ZÄÖÜ][a-zäöüß]+){2,}\b`)
// wikiLinkExt is a goldmark extension for recognizing WikiLinks.
// If destFunc is non-nil, it is called with the page name to produce
// the link destination. If nil, the destination defaults to "/"+pageName.
type wikiLinkExt struct {
destFunc func(pageName string) string
}
func (e *wikiLinkExt) 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(&wikiLinkParser{destFunc: e.destFunc}, 998),
),
)
}
// A parser for WikiLinks (resolving to /WikiLinks)
type wikiLinkParser struct {
destFunc func(pageName string) string
}
func (w *wikiLinkParser) Trigger() []byte {
return []byte{' ', '('}
}
func (w *wikiLinkParser) 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 "WikiLink..."
// as well as lines starting with " WikiLink..." (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 := pageNameRE.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])))
dest := "/" + string(linkText)
if w.destFunc != nil {
dest = w.destFunc(string(linkText))
}
link.Destination = []byte(dest)
return link
}