Commit 71634452 authored by Sandro Santilli's avatar Sandro Santilli Committed by Lunny Xiao
Browse files

Improve issue references in markdown (#471)

* Improve issue references in markdown. (#3436)

* Fix build

* Fix lint

* Fix comment typo
parent 2342df18
main release/v1.10 release/v1.11 release/v1.12 release/v1.13 release/v1.14 release/v1.15 release/v1.16 release/v1.17 release/v1.18 release/v1.8 release/v1.9 v1.19.0-dev v1.18.1 v1.18.0 v1.18.0-rc1 v1.18.0-rc0 v1.18.0-dev v1.17.4 v1.17.3 v1.17.2 v1.17.1 v1.17.0 v1.17.0-rc2 v1.17.0-rc1 v1.17.0-dev v1.16.9 v1.16.8 v1.16.7 v1.16.6 v1.16.5 v1.16.4 v1.16.3 v1.16.2 v1.16.1 v1.16.0 v1.16.0-rc1 v1.16.0-dev v1.15.11 v1.15.10 v1.15.9 v1.15.8 v1.15.7 v1.15.6 v1.15.5 v1.15.4 v1.15.3 v1.15.2 v1.15.1 v1.15.0 v1.15.0-rc3 v1.15.0-rc2 v1.15.0-rc1 v1.15.0-dev v1.14.7 v1.14.6 v1.14.5 v1.14.4 v1.14.3 v1.14.2 v1.14.1 v1.14.0 v1.14.0-rc2 v1.14.0-rc1 v1.14.0-dev v1.13.7 v1.13.6 v1.13.5 v1.13.4 v1.13.3 v1.13.2 v1.13.1 v1.13.0 v1.13.0-rc2 v1.13.0-rc1 v1.13.0-dev v1.12.6 v1.12.5 v1.12.4 v1.12.3 v1.12.2 v1.12.1 v1.12.0 v1.12.0-rc2 v1.12.0-rc1 v1.12.0-dev v1.11.8 v1.11.7 v1.11.6 v1.11.5 v1.11.4 v1.11.3 v1.11.2 v1.11.1 v1.11.0 v1.11.0-rc2 v1.11.0-rc1 v1.11.0-dev v1.10.6 v1.10.5 v1.10.4 v1.10.3 v1.10.2 v1.10.1 v1.10.0 v1.10.0-rc2 v1.10.0-rc1 v1.10.0-dev v1.9.6 v1.9.5 v1.9.4 v1.9.3 v1.9.2 v1.9.1 v1.9.0 v1.9.0-rc2 v1.9.0-rc1 v1.9.0-dev v1.8.3 v1.8.2 v1.8.1 v1.8.0 v1.8.0-rc3 v1.8.0-rc2 v1.8.0-rc1 v1.7.6 v1.7.5 v1.7.4 v1.7.3 v1.7.2 v1.7.1 v1.7.0 v1.7.0-rc3 v1.7.0-rc2 v1.7.0-rc1 v1.7.0-dev v1.6.4 v1.6.3 v1.6.2 v1.6.1 v1.6.0 v1.6.0-rc2 v1.6.0-rc1 v1.6.0-dev v1.5.3 v1.5.2 v1.5.1 v1.5.0 v1.5.0-rc2 v1.5.0-rc1 v1.5.0-dev v1.4.3 v1.4.2 v1.4.1 v1.4.0 v1.4.0-rc3 v1.4.0-rc2 v1.4.0-rc1 v1.3.3 v1.3.2 v1.3.1 v1.3.0 v1.3.0-rc2 v1.3.0-rc1 v1.2.3 v1.2.2 v1.2.1 v1.2.0 v1.2.0-rc3 v1.2.0-rc2 v1.2.0-rc1 v1.1.4 v1.1.3 v1.1.2 v1.1.1 v1.1.0
No related merge requests found
Showing with 34 additions and 1 deletion
+34 -1
......@@ -91,6 +91,9 @@ var (
IssueNumericPattern = regexp.MustCompile(`( |^|\()#[0-9]+\b`)
// IssueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234
IssueAlphanumericPattern = regexp.MustCompile(`( |^|\()[A-Z]{1,10}-[1-9][0-9]*\b`)
// CrossReferenceIssueNumericPattern matches string that references a numeric issue in a different repository
// e.g. gogits/gogs#12345
CrossReferenceIssueNumericPattern = regexp.MustCompile(`( |^)[0-9a-zA-Z]+/[0-9a-zA-Z]+#[0-9]+\b`)
// Sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae
// FIXME: this pattern matches pure numbers as well, right now we do a hack to check in RenderSha1CurrentPattern
......@@ -156,7 +159,19 @@ func (r *Renderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {
if j == -1 {
j = len(m)
}
out.WriteString(fmt.Sprintf(`<a href="%s">#%s</a>`, m, base.ShortSha(string(m[i+7:j]))))
issue := string(m[i+7 : j])
fullRepoURL := setting.AppURL + strings.TrimPrefix(r.urlPrefix, "/")
var link string
if strings.HasPrefix(string(m), fullRepoURL) {
// Use a short issue reference if the URL refers to this repository
link = fmt.Sprintf(`<a href="%s">#%s</a>`, m, issue)
} else {
// Use a cross-repository issue reference if the URL refers to a different repository
repo := string(m[len(setting.AppURL) : i-1])
link = fmt.Sprintf(`<a href="%s">%s#%s</a>`, m, repo, issue)
}
out.WriteString(link)
return
}
}
......@@ -263,6 +278,23 @@ func RenderIssueIndexPattern(rawBytes []byte, urlPrefix string, metas map[string
return rawBytes
}
// RenderCrossReferenceIssueIndexPattern renders issue indexes from other repositories to corresponding links.
func RenderCrossReferenceIssueIndexPattern(rawBytes []byte, urlPrefix string, metas map[string]string) []byte {
ms := CrossReferenceIssueNumericPattern.FindAll(rawBytes, -1)
for _, m := range ms {
if m[0] == ' ' || m[0] == '(' {
m = m[1:] // ignore leading space or opening parentheses
}
repo := string(bytes.Split(m, []byte("#"))[0])
issue := string(bytes.Split(m, []byte("#"))[1])
link := fmt.Sprintf(`<a href="%s%s/issues/%s">%s</a>`, setting.AppURL, repo, issue, m)
rawBytes = bytes.Replace(rawBytes, m, []byte(link), 1)
}
return rawBytes
}
// RenderSha1CurrentPattern renders SHA1 strings to corresponding links that assumes in the same repository.
func RenderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte {
return []byte(Sha1CurrentPattern.ReplaceAllStringFunc(string(rawBytes[:]), func(m string) string {
......@@ -283,6 +315,7 @@ func RenderSpecialLink(rawBytes []byte, urlPrefix string, metas map[string]strin
}
rawBytes = RenderIssueIndexPattern(rawBytes, urlPrefix, metas)
rawBytes = RenderCrossReferenceIssueIndexPattern(rawBytes, urlPrefix, metas)
rawBytes = RenderSha1CurrentPattern(rawBytes, urlPrefix)
return rawBytes
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment