@@ -240,30 +240,67 @@ func FindAllIssueReferences(content string) []IssueReference {
240
240
// Need to convert fully qualified html references to local system to #/! short codes
241
241
contentBytes := []byte (content )
242
242
if re := getGiteaIssuePullPattern (); re != nil {
243
+ // We will iterate through the content, rewrite and simplify full references.
244
+ //
245
+ // We want to transform something like:
246
+ //
247
+ // 0 1 2 3 4 5 6
248
+ // 012345678901234567890123456789012345678901234567890123456789012345789
249
+ // this is a https://ourgitea.com/git/owner/repo/issues/123456789, foo
250
+ // https://ourgitea.com/git/owner/repo/pulls/123456789
251
+ //
252
+ // Into something like:
253
+ //
254
+ // this is a #123456789, foo
255
+ // !123456789
256
+
243
257
pos := 0
244
258
for {
259
+ // re looks for something like: (\s|^|\(|\[)https://ourgitea.com/git/(owner/repo)/(issues)/(123456789)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)
245
260
match := re .FindSubmatchIndex (contentBytes [pos :])
246
261
if match == nil {
247
262
break
248
263
}
264
+ // match is a bunch of indices into the content from pos onwards so if our content is:
265
+
266
+ // To simplify things let's just add pos to all of the indices in match
267
+ for i := range match {
268
+ match [i ] += pos
269
+ }
270
+
249
271
// match[0]-match[1] is whole string
250
272
// match[2]-match[3] is preamble
251
- pos += match [3 ]
273
+
274
+ // move the position to the end of the preamble
275
+ pos = match [3 ]
276
+
252
277
// match[4]-match[5] is owner/repo
278
+ // now copy the owner/repo to end of the preamble
253
279
endPos := pos + match [5 ] - match [4 ]
254
280
copy (contentBytes [pos :endPos ], contentBytes [match [4 ]:match [5 ]])
281
+
282
+ // move the current position to the end of the newly copied owner/repo
255
283
pos = endPos
284
+
285
+ // Now set the issue/pull marker:
286
+ //
256
287
// match[6]-match[7] == 'issues'
257
288
contentBytes [pos ] = '#'
258
289
if string (contentBytes [match [6 ]:match [7 ]]) == "pulls" {
259
290
contentBytes [pos ] = '!'
260
291
}
261
292
pos ++
293
+
294
+ // Then add the issue/pull number
295
+ //
262
296
// match[8]-match[9] is the number
263
297
endPos = pos + match [9 ] - match [8 ]
264
298
copy (contentBytes [pos :endPos ], contentBytes [match [8 ]:match [9 ]])
299
+
300
+ // Now copy what's left at the end of the string to the new end position
265
301
copy (contentBytes [endPos :], contentBytes [match [9 ]:])
266
302
// now we reset the length
303
+
267
304
// our new section has length endPos - match[3]
268
305
// our old section has length match[9] - match[3]
269
306
contentBytes = contentBytes [:len (contentBytes )- match [9 ]+ endPos ]
0 commit comments