Skip to content

Commit f013209

Browse files
authored
Shorter default text link for attachments, comments and milestones (#22)
Signed-off-by: Benoit Donneaux <[email protected]>
1 parent 563125e commit f013209

File tree

2 files changed

+84
-30
lines changed

2 files changed

+84
-30
lines changed

markdown/link.go

+37-13
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ var (
3636
localFileLinkRegexp = regexp.MustCompile(`^[[:alnum:]-._,]+\.[[:alpha:]]+$`)
3737

3838
// regexp for a trac 'comment:<commentNum>' and 'comment:<commentNum>:ticket:<ticketID>' link: $1=commentNum, $2=ticketID
39-
ticketCommentLinkRegexp = regexp.MustCompile(`comment:([[:digit:]]+)(?::ticket:([[:digit:]]+))?`)
39+
ticketCommentLinkRegexp = regexp.MustCompile(`(.{0,1})comment:([[:digit:]]+)(?::ticket:([[:digit:]]+))?`)
4040

4141
// regexp for a trac 'milestone:<milestoneName>' link: $1=milestoneName
42-
milestoneLinkRegexp = regexp.MustCompile(`milestone:([[:alnum:]\-._~:/?#@!$&'"()*+,;%=]+)`)
42+
milestoneLinkRegexp = regexp.MustCompile(`(.{0,1})milestone:([[:alnum:]\-._~:/?#@!$&'"()*+,;%=]+)`)
4343

4444
// regexp for a trac 'attachment:<file>', 'attachment:<file>:wiki:<pageName>' and 'attachment:<file>:ticket:<ticketID>' links: $1=file, $2=pageName, $3=ticketID
4545
attachmentLinkRegexp = regexp.MustCompile(
46-
`attachment:([[:alnum:]\-._~/?#@!$&'"()*+,;%=]+)` +
46+
`(.{0,1})attachment:([[:alnum:]\-._~/?#@!$&'"()*+,;%=]+)` +
4747
`(?:` +
4848
`(?::wiki:((?:[[:upper:]][[:lower:]]*)+))|` +
4949
`(?::ticket:([[:digit:]]+))` +
@@ -100,15 +100,16 @@ func (converter *DefaultConverter) resolveHtdocsLink(link string) string {
100100
}
101101

102102
func (converter *DefaultConverter) resolveTicketCommentLink(ticketID int64, link string) string {
103-
commentNumStr := ticketCommentLinkRegexp.ReplaceAllString(link, `$1`)
103+
leadingChar := ticketCommentLinkRegexp.ReplaceAllString(link, `$1`)
104+
commentNumStr := ticketCommentLinkRegexp.ReplaceAllString(link, `$2`)
104105
var commentNum int64
105106
commentNum, err := strconv.ParseInt(commentNumStr, 10, 64)
106107
if err != nil {
107108
log.Warn("found invalid Trac ticket comment number %s", commentNum)
108109
return link
109110
}
110111

111-
commentTicketIDStr := ticketCommentLinkRegexp.ReplaceAllString(link, `$2`)
112+
commentTicketIDStr := ticketCommentLinkRegexp.ReplaceAllString(link, `$3`)
112113
var commentTicketID int64
113114
if commentTicketIDStr != "" {
114115
commentTicketID, err = strconv.ParseInt(commentTicketIDStr, 10, 64)
@@ -145,11 +146,17 @@ func (converter *DefaultConverter) resolveTicketCommentLink(ticketID int64, link
145146
}
146147

147148
commentURL := converter.giteaAccessor.GetIssueCommentURL(commentTicketID, commentID)
148-
return markLink(commentURL)
149+
150+
// unless already defined before, use a short link text instead of the full URL
151+
if leadingChar != "]" {
152+
return leadingChar + "[comment:" + strconv.FormatInt(commentID, 10) + "]" + markLink(commentURL)
153+
}
154+
return leadingChar + markLink(commentURL)
149155
}
150156

151157
func (converter *DefaultConverter) resolveMilestoneLink(link string) string {
152-
milestoneName := milestoneLinkRegexp.ReplaceAllString(link, `$1`)
158+
leadingChar := milestoneLinkRegexp.ReplaceAllString(link, `$1`)
159+
milestoneName := milestoneLinkRegexp.ReplaceAllString(link, `$2`)
153160
milestoneID, err := converter.giteaAccessor.GetMilestoneID(milestoneName)
154161
if err != nil {
155162
return link // not a recognised link - do not mark (error should already be logged)
@@ -160,10 +167,16 @@ func (converter *DefaultConverter) resolveMilestoneLink(link string) string {
160167
}
161168

162169
milestoneURL := converter.giteaAccessor.GetMilestoneURL(milestoneID)
163-
return markLink(milestoneURL)
170+
171+
// unless already defined before, use a short link text instead of the full URL
172+
if leadingChar != "]" {
173+
return leadingChar + "[milestone:" + milestoneName + "]" + markLink(milestoneURL)
174+
}
175+
return leadingChar + markLink(milestoneURL)
164176
}
165177

166178
func (converter *DefaultConverter) resolveTicketAttachmentLink(ticketID int64, attachmentName string, link string) string {
179+
leadingChar := attachmentLinkRegexp.ReplaceAllString(link, `$1`)
167180
issueID, err := converter.giteaAccessor.GetIssueID(ticketID)
168181
if err != nil {
169182
return link // not a recognised link - do not mark
@@ -183,19 +196,30 @@ func (converter *DefaultConverter) resolveTicketAttachmentLink(ticketID int64, a
183196
}
184197

185198
attachmentURL := converter.giteaAccessor.GetIssueAttachmentURL(issueID, uuid)
186-
return markLink(attachmentURL)
199+
200+
// Keep original text unless already defined before
201+
if leadingChar != "]" {
202+
return leadingChar + "[attachment:" + attachmentName + "]" + markLink(attachmentURL)
203+
}
204+
return leadingChar + markLink(attachmentURL)
187205
}
188206

189207
func (converter *DefaultConverter) resolveWikiAttachmentLink(wikiPage string, attachmentName string, link string) string {
208+
leadingChar := attachmentLinkRegexp.ReplaceAllString(link, `$1`)
190209
attachmentWikiRelPath := converter.giteaAccessor.GetWikiAttachmentRelPath(wikiPage, attachmentName)
191210
attachmentURL := converter.giteaAccessor.GetWikiFileURL(attachmentWikiRelPath)
192-
return markLink(attachmentURL)
211+
212+
// Keep original text unless already defined before
213+
if leadingChar != "]" {
214+
return leadingChar + "[attachment:" + attachmentName + "]" + markLink(attachmentURL)
215+
}
216+
return leadingChar + markLink(attachmentURL)
193217
}
194218

195219
func (converter *DefaultConverter) resolveAttachmentLink(ticketID int64, wikiPage string, link string) string {
196-
attachmentName := attachmentLinkRegexp.ReplaceAllString(link, `$1`)
197-
attachmentWikiPage := attachmentLinkRegexp.ReplaceAllString(link, `$2`)
198-
attachmentTicketIDStr := attachmentLinkRegexp.ReplaceAllString(link, `$3`)
220+
attachmentName := attachmentLinkRegexp.ReplaceAllString(link, `$2`)
221+
attachmentWikiPage := attachmentLinkRegexp.ReplaceAllString(link, `$3`)
222+
attachmentTicketIDStr := attachmentLinkRegexp.ReplaceAllString(link, `$4`)
199223

200224
// there are two types of attachment: ticket attachments and wiki attachments...
201225
if attachmentTicketIDStr != "" {

markdown/link_test.go

+47-17
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,27 @@ func verifyAllLinkTypes(
9898
convertFn func(tracText string) string,
9999
tracLinkStr string,
100100
markdownLinkStr string,
101-
testAutomaticLinks ...bool) {
101+
extraOptions ...string) {
102102

103-
// use testAutomaticLinks when the tested link should work without accompanying text
104-
if len(testAutomaticLinks) > 0 {
103+
// the markdown link text can be specified with the first extra option
104+
markdownLinkText := markdownLinkStr
105+
if len(extraOptions) > 0 && extraOptions[0] != "" {
106+
markdownLinkText = extraOptions[0]
107+
}
108+
109+
// if a second extra option is passed, the tested link should work without accompanying text
110+
if len(extraOptions) > 1 && extraOptions[1] == "true" {
105111
verifyLink(t, setUpFn, tearDownFn, convertFn, tracPlainLink(tracLinkStr), markdownAutomaticLink(markdownLinkStr), false)
106112
verifyLink(t, setUpFn, tearDownFn, convertFn, tracSingleBracketLink(tracLinkStr), markdownAutomaticLink(markdownLinkStr), true)
107113
verifyLink(t, setUpFn, tearDownFn, convertFn, tracSingleBracketLink(tracLinkStr), markdownAutomaticLink(markdownLinkStr), false)
108114
verifyLink(t, setUpFn, tearDownFn, convertFn, tracDoubleBracketLink(tracLinkStr), markdownAutomaticLink(markdownLinkStr), true)
109115
verifyLink(t, setUpFn, tearDownFn, convertFn, tracDoubleBracketLink(tracLinkStr), markdownAutomaticLink(markdownLinkStr), false)
110116
} else {
111-
verifyLink(t, setUpFn, tearDownFn, convertFn, tracPlainLink(tracLinkStr), markdownLinkWithText(markdownLinkStr, markdownLinkStr), false)
112-
verifyLink(t, setUpFn, tearDownFn, convertFn, tracSingleBracketLink(tracLinkStr), markdownLinkWithText(markdownLinkStr, markdownLinkStr), true)
113-
verifyLink(t, setUpFn, tearDownFn, convertFn, tracSingleBracketLink(tracLinkStr), markdownLinkWithText(markdownLinkStr, markdownLinkStr), false)
114-
verifyLink(t, setUpFn, tearDownFn, convertFn, tracDoubleBracketLink(tracLinkStr), markdownLinkWithText(markdownLinkStr, markdownLinkStr), true)
115-
verifyLink(t, setUpFn, tearDownFn, convertFn, tracDoubleBracketLink(tracLinkStr), markdownLinkWithText(markdownLinkStr, markdownLinkStr), false)
117+
verifyLink(t, setUpFn, tearDownFn, convertFn, tracPlainLink(tracLinkStr), markdownLinkWithText(markdownLinkStr, markdownLinkText), false)
118+
verifyLink(t, setUpFn, tearDownFn, convertFn, tracSingleBracketLink(tracLinkStr), markdownLinkWithText(markdownLinkStr, markdownLinkText), true)
119+
verifyLink(t, setUpFn, tearDownFn, convertFn, tracSingleBracketLink(tracLinkStr), markdownLinkWithText(markdownLinkStr, markdownLinkText), false)
120+
verifyLink(t, setUpFn, tearDownFn, convertFn, tracDoubleBracketLink(tracLinkStr), markdownLinkWithText(markdownLinkStr, markdownLinkText), true)
121+
verifyLink(t, setUpFn, tearDownFn, convertFn, tracDoubleBracketLink(tracLinkStr), markdownLinkWithText(markdownLinkStr, markdownLinkText), false)
116122
}
117123

118124
verifyLink(t, setUpFn, tearDownFn, convertFn, tracSingleBracketLinkWithText(tracLinkStr, linkText), markdownLinkWithText(markdownLinkStr, linkText), true)
@@ -130,13 +136,29 @@ func verifyAllLinkTypes(
130136
const httpLink = "http://www.example.com"
131137

132138
func TestHttpLinks(t *testing.T) {
133-
verifyAllLinkTypes(t, setUp, tearDown, wikiConvert, httpLink, httpLink, true)
139+
verifyAllLinkTypes(
140+
t,
141+
setUp,
142+
tearDown,
143+
wikiConvert,
144+
httpLink,
145+
httpLink,
146+
"",
147+
"true")
134148
}
135149

136150
const httpsLink = "https://www.example.com"
137151

138152
func TestHttpsLink(t *testing.T) {
139-
verifyAllLinkTypes(t, setUp, tearDown, wikiConvert, httpsLink, httpsLink, true)
153+
verifyAllLinkTypes(
154+
t,
155+
setUp,
156+
tearDown,
157+
wikiConvert,
158+
httpsLink,
159+
httpsLink,
160+
"",
161+
"true")
140162
}
141163

142164
const (
@@ -282,6 +304,7 @@ const (
282304
tracCommentNumStr = "12"
283305
commentTime int64 = 112233
284306
commentID int64 = 54321
307+
commentIDStr string = "54321"
285308
commentURL string = "url-of-comment-54321"
286309
)
287310

@@ -318,7 +341,8 @@ func TestImplicitTicketCommentLink(t *testing.T) {
318341
tearDown,
319342
ticketConvert,
320343
"comment:"+tracCommentNumStr,
321-
commentURL)
344+
commentURL,
345+
"comment:"+commentIDStr)
322346
}
323347

324348
const (
@@ -337,7 +361,8 @@ func TestExplicitTicketCommentLink(t *testing.T) {
337361
tearDown,
338362
ticketConvert,
339363
"comment:"+tracCommentNumStr+":ticket:"+otherTicketIDStr,
340-
commentURL)
364+
commentURL,
365+
"comment:"+commentIDStr)
341366
}
342367

343368
const (
@@ -369,7 +394,8 @@ func TestMilestoneLink(t *testing.T) {
369394
tearDown,
370395
wikiConvert,
371396
"milestone:"+milestoneName,
372-
milestoneURL)
397+
milestoneURL,
398+
"milestone:"+milestoneName)
373399
}
374400

375401
const (
@@ -405,7 +431,8 @@ func TestImplicitWikiAttachmentLink(t *testing.T) {
405431
tearDown,
406432
wikiConvert,
407433
"attachment:"+attachmentName,
408-
attachmentWikiURL)
434+
attachmentWikiURL,
435+
"attachment:"+attachmentName)
409436
}
410437

411438
const (
@@ -423,7 +450,8 @@ func TestExplicitWikiAttachmentLink(t *testing.T) {
423450
tearDown,
424451
wikiConvert,
425452
"attachment:"+attachmentName+":wiki:"+otherWikiPage,
426-
attachmentWikiURL)
453+
attachmentWikiURL,
454+
"attachment:"+attachmentName)
427455
}
428456

429457
const (
@@ -458,7 +486,8 @@ func TestImplicitTicketAttachmentLink(t *testing.T) {
458486
tearDown,
459487
ticketConvert,
460488
"attachment:"+attachmentName,
461-
ticketAttachmentURL)
489+
ticketAttachmentURL,
490+
"attachment:"+attachmentName)
462491
}
463492

464493
func setUpExplicitTicketAttachmentLink(t *testing.T) {
@@ -472,7 +501,8 @@ func TestExplicitTicketAttachmentLink(t *testing.T) {
472501
tearDown,
473502
ticketConvert,
474503
"attachment:"+attachmentName+":ticket:"+otherTicketIDStr,
475-
ticketAttachmentURL)
504+
ticketAttachmentURL,
505+
"attachment:"+attachmentName)
476506
}
477507

478508
const (

0 commit comments

Comments
 (0)