From 2c46280d971c6478095808b96e000cffe030d420 Mon Sep 17 00:00:00 2001 From: lstahlman Date: Fri, 23 Dec 2016 16:49:53 -0700 Subject: [PATCH 1/4] Improve issue references in markdown. (#3436) --- modules/markdown/markdown.go | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go index 6bf29db07ae48..c718638e3eb50 100644 --- a/modules/markdown/markdown.go +++ b/modules/markdown/markdown.go @@ -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 difference 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(`#%s`, 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(`#%s`, 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(`%s#%s`, 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(`%s`, 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 } From e59cb4a6d83a2b0cabde2e20efd187cd342cafd1 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Sat, 24 Dec 2016 09:51:57 +0100 Subject: [PATCH 2/4] Fix build --- modules/markdown/markdown.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go index c718638e3eb50..b561b86867f24 100644 --- a/modules/markdown/markdown.go +++ b/modules/markdown/markdown.go @@ -161,14 +161,14 @@ func (r *Renderer) AutoLink(out *bytes.Buffer, link []byte, kind int) { } issue := string(m[i+7 : j]) - fullRepoUrl := setting.AppUrl + strings.TrimPrefix(r.urlPrefix, "/") + 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(`#%s`, 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]) + repo := string(m[len(setting.AppURL) : i-1]) link = fmt.Sprintf(`%s#%s`, m, repo, issue) } out.WriteString(link) @@ -289,7 +289,7 @@ func RenderCrossReferenceIssueIndexPattern(rawBytes []byte, urlPrefix string, me repo := string(bytes.Split(m, []byte("#"))[0]) issue := string(bytes.Split(m, []byte("#"))[1]) - link := fmt.Sprintf(`%s`, setting.AppUrl, repo, issue, m) + link := fmt.Sprintf(`%s`, setting.AppURL, repo, issue, m) rawBytes = bytes.Replace(rawBytes, m, []byte(link), 1) } return rawBytes From 845891d102baf6176685ab86cba7f7f4ec520542 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Sat, 24 Dec 2016 10:06:53 +0100 Subject: [PATCH 3/4] Fix lint --- modules/markdown/markdown.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go index b561b86867f24..56263ae0a5237 100644 --- a/modules/markdown/markdown.go +++ b/modules/markdown/markdown.go @@ -161,9 +161,9 @@ func (r *Renderer) AutoLink(out *bytes.Buffer, link []byte, kind int) { } issue := string(m[i+7 : j]) - fullRepoUrl := setting.AppURL + strings.TrimPrefix(r.urlPrefix, "/") + fullRepoURL := setting.AppURL + strings.TrimPrefix(r.urlPrefix, "/") var link string - if strings.HasPrefix(string(m), fullRepoUrl) { + if strings.HasPrefix(string(m), fullRepoURL) { // Use a short issue reference if the URL refers to this repository link = fmt.Sprintf(`#%s`, m, issue) } else { From 05b3f4c9217135ea2207d5a5ab68bd805c3421ea Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Sat, 24 Dec 2016 22:10:41 +0100 Subject: [PATCH 4/4] Fix comment typo --- modules/markdown/markdown.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go index 56263ae0a5237..b973b7c836594 100644 --- a/modules/markdown/markdown.go +++ b/modules/markdown/markdown.go @@ -91,7 +91,7 @@ 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 difference repository + // 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`)