-
Notifications
You must be signed in to change notification settings - Fork 13
/
reflink.go
267 lines (227 loc) · 6.16 KB
/
reflink.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
// Note: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/autolinked-references-and-urls
import (
"bytes"
"fmt"
"net/url"
"strings"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/text"
)
type refLink struct {
start int
end int
text string
}
// Note: '_' is actually not boundary. But it's hard to check if the '_' is a part of italic/bold
// syntax.
// For example, _#123_ should be linked because '_'s are part of italic syntax. But _#123 and #123_
// should not be linked because '_'s are NOT part of italic syntax.
// Checking if the parent node is Italic/Bold or not does not help to solve this issue. For example,
// _foo_#1 should be linked. However #1 itself is not an italic text though the neighbor node is
// Italic.
// Fortunately this is very edge case. To keep our implementation simple, we compromise to treat '_'
// as a boundary. For example, _#1 and #1_ are linked incorrectly, but I believe they are OK for our
// use cases.
func isBoundary(b byte) bool {
if '0' <= b && b <= '9' || 'a' <= b && b <= 'z' || 'A' <= b && b <= 'Z' {
return false
}
return true
}
func isUserNameChar(b byte) bool {
return '0' <= b && b <= '9' || 'a' <= b && b <= 'z' || 'A' <= b && b <= 'Z' || b == '-'
}
// Reflinker detects all references in markdown text and replaces them with links.
type Reflinker struct {
repo string
home string
src []byte
links []refLink
}
// NewReflinker creates Reflinker instance. repoURL is a repository URL of the service like
// https://github.com/user/repo.
func NewReflinker(repoURL string, src []byte) *Reflinker {
u, err := url.Parse(repoURL)
if err != nil {
panic(err)
}
u.Path = ""
return &Reflinker{
repo: repoURL,
home: u.String(),
src: src,
links: nil,
}
}
func (l *Reflinker) isBoundaryAt(idx int) bool {
if idx < 0 || len(l.src) <= idx {
return true
}
return isBoundary(l.src[idx])
}
func (l *Reflinker) lastIndexIssueRef(begin, end int) int {
if !l.isBoundaryAt(begin - 1) {
return -1 // Issue ref must follow a boundary (e.g. 'foo#bar')
}
for i := 1; begin+i < end; i++ {
b := l.src[begin+i]
if '0' <= b && b <= '9' {
continue
}
if i == 1 || !isBoundary(b) {
return -1
}
return begin + i
}
if !l.isBoundaryAt(end) {
return -1
}
return end // The text ends with issue number
}
func (l *Reflinker) linkIssue(begin, end int) int {
e := l.lastIndexIssueRef(begin, end)
if e < 0 {
return begin + 1
}
r := l.src[begin:e]
l.links = append(l.links, refLink{
start: begin,
end: e,
text: fmt.Sprintf("[%s](%s/issues/%s)", r, l.repo, r[1:]),
})
return e
}
func (l *Reflinker) lastIndexUserRef(begin, end int) int {
if !l.isBoundaryAt(begin - 1) {
return -1 // e.g. foo@bar, _@foo (-@foo is ok)
}
// Note: Username may only contain alphanumeric characters or single hyphens, and cannot begin
// or end with a hyphen: @foo-, @-foo
// Note: '/' just after user name like @foo/ is not allowed
if b := l.src[begin+1]; !isUserNameChar(b) || b == '-' {
return -1
}
for i := 2; begin+i < end; i++ {
b := l.src[begin+i]
if isUserNameChar(b) {
continue
}
if !isBoundary(b) || b == '/' || l.src[begin+i-1] == '-' {
return -1
}
return begin + i
}
if l.src[end-1] == '-' {
return -1
}
if end < len(l.src) {
if b := l.src[end]; !isBoundary(b) || b == '/' {
return -1
}
}
return end
}
func (l *Reflinker) linkUser(begin, end int) int {
e := l.lastIndexUserRef(begin, end)
if e < 0 {
return begin + 1
}
u := l.src[begin:e]
l.links = append(l.links, refLink{
start: begin,
end: e,
text: fmt.Sprintf("[%s](%s/%s)", u, l.home, u[1:]),
})
return e
}
const hashLen int = 40
func (l *Reflinker) linkCommitSHA(begin, end int) int {
for i := 1; i < hashLen; i++ { // Since l.src[begin] was already checked, i starts from 1
if begin+i >= end {
return begin + i
}
b := l.src[begin+i]
if '0' <= b && b <= '9' || 'a' <= b && b <= 'f' {
continue
}
return begin + i
}
if l.isBoundaryAt(begin-1) && l.isBoundaryAt(begin+hashLen) {
h := l.src[begin : begin+hashLen]
l.links = append(l.links, refLink{
start: begin,
end: begin + hashLen,
text: fmt.Sprintf("[`%s`](%s/commit/%s)", h[:10], l.repo, h),
})
}
return begin + hashLen
}
// DetectLinks detects reference links in given markdown text and remembers them to replace all
// references later.
func (l *Reflinker) DetectLinks(t *ast.Text) {
o := t.Segment.Start // start offset
for o < t.Segment.Stop-1 { // `-1` means the last character is not checked
s := l.src[o:t.Segment.Stop]
i := bytes.IndexAny(s, "#@1234567890abcdef")
if i < 0 || len(s)-1 <= i {
return
}
switch s[i] {
case '#':
o = l.linkIssue(o+i, t.Segment.Stop)
case '@':
o = l.linkUser(o+i, t.Segment.Stop)
default:
// hex character [0-9a-f]
o = l.linkCommitSHA(o+i, t.Segment.Stop)
}
}
}
// BuildLinkedText builds a markdown text where all references are replaced with links. The links were
// detected by DetectLinks() method calls.
func (l *Reflinker) BuildLinkedText() string {
if len(l.links) == 0 {
return string(l.src)
}
var b strings.Builder
i := 0
for _, r := range l.links {
b.Write(l.src[i:r.start])
b.WriteString(r.text)
i = r.end
}
b.Write(l.src[i:])
return b.String()
}
// IsLinkDetected returns whether some link was detected by DetectLinks() method calls.
func (l *Reflinker) IsLinkDetected() bool {
return len(l.links) > 0
}
// LinkRefs replaces all references in the given markdown text with actual links.
func LinkRefs(input string, repoURL string) string {
src := []byte(input)
md := goldmark.New(goldmark.WithExtensions(extension.GFM))
t := md.Parser().Parse(text.NewReader(src))
l := NewReflinker(repoURL, src)
ast.Walk(t, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
switch n := n.(type) {
case *ast.CodeSpan, *ast.Link, *ast.AutoLink:
return ast.WalkSkipChildren, nil
case *ast.Text:
l.DetectLinks(n)
return ast.WalkContinue, nil
default:
return ast.WalkContinue, nil
}
})
if !l.IsLinkDetected() {
return input
}
return l.BuildLinkedText()
}