Skip to content

Commit 5feb68a

Browse files
lstahlmanunknwon
authored andcommitted
Improve issue references in markdown. (#3436)
1 parent 03f7f3e commit 5feb68a

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

modules/markdown/markdown.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ var (
8989
IssueNumericPattern = regexp.MustCompile(`( |^|\()#[0-9]+\b`)
9090
// IssueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234
9191
IssueAlphanumericPattern = regexp.MustCompile(`( |^|\()[A-Z]{1,10}-[1-9][0-9]*\b`)
92+
// CrossReferenceIssueNumericPattern matches string that references a numeric issue in a difference repository
93+
// e.g. gogits/gogs#12345
94+
CrossReferenceIssueNumericPattern = regexp.MustCompile(`( |^)[0-9a-zA-Z]+/[0-9a-zA-Z]+#[0-9]+\b`)
9295

9396
// Sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae
9497
// FIXME: this pattern matches pure numbers as well, right now we do a hack to check in RenderSha1CurrentPattern
@@ -154,7 +157,19 @@ func (r *Renderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {
154157
if j == -1 {
155158
j = len(m)
156159
}
157-
out.WriteString(fmt.Sprintf(`<a href="%s">#%s</a>`, m, base.ShortSha(string(m[i+7:j]))))
160+
161+
issue := string(m[i+7 : j])
162+
fullRepoUrl := setting.AppUrl + strings.TrimPrefix(r.urlPrefix, "/")
163+
var link string
164+
if strings.HasPrefix(string(m), fullRepoUrl) {
165+
// Use a short issue reference if the URL refers to this repository
166+
link = fmt.Sprintf(`<a href="%s">#%s</a>`, m, issue)
167+
} else {
168+
// Use a cross-repository issue reference if the URL refers to a different repository
169+
repo := string(m[len(setting.AppUrl) : i-1])
170+
link = fmt.Sprintf(`<a href="%s">%s#%s</a>`, m, repo, issue)
171+
}
172+
out.WriteString(link)
158173
return
159174
}
160175
}
@@ -261,6 +276,23 @@ func RenderIssueIndexPattern(rawBytes []byte, urlPrefix string, metas map[string
261276
return rawBytes
262277
}
263278

279+
// RenderCrossReferenceIssueIndexPattern renders issue indexes from other repositories to corresponding links.
280+
func RenderCrossReferenceIssueIndexPattern(rawBytes []byte, urlPrefix string, metas map[string]string) []byte {
281+
ms := CrossReferenceIssueNumericPattern.FindAll(rawBytes, -1)
282+
for _, m := range ms {
283+
if m[0] == ' ' || m[0] == '(' {
284+
m = m[1:] // ignore leading space or opening parentheses
285+
}
286+
287+
repo := string(bytes.Split(m, []byte("#"))[0])
288+
issue := string(bytes.Split(m, []byte("#"))[1])
289+
290+
link := fmt.Sprintf(`<a href="%s%s/issues/%s">%s</a>`, setting.AppUrl, repo, issue, m)
291+
rawBytes = bytes.Replace(rawBytes, m, []byte(link), 1)
292+
}
293+
return rawBytes
294+
}
295+
264296
// RenderSha1CurrentPattern renders SHA1 strings to corresponding links that assumes in the same repository.
265297
func RenderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte {
266298
return []byte(Sha1CurrentPattern.ReplaceAllStringFunc(string(rawBytes[:]), func(m string) string {
@@ -281,6 +313,7 @@ func RenderSpecialLink(rawBytes []byte, urlPrefix string, metas map[string]strin
281313
}
282314

283315
rawBytes = RenderIssueIndexPattern(rawBytes, urlPrefix, metas)
316+
rawBytes = RenderCrossReferenceIssueIndexPattern(rawBytes, urlPrefix, metas)
284317
rawBytes = RenderSha1CurrentPattern(rawBytes, urlPrefix)
285318
return rawBytes
286319
}

0 commit comments

Comments
 (0)