From cc8c67ff29f85630c6e79cc9ea9e55b68a1a6805 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sat, 9 Jan 2016 10:59:04 +0800 Subject: [PATCH] fix markdown autolink error --- gogs.go | 2 +- modules/base/markdown.go | 94 +++++++++++++++++++----------------- templates/.VERSION | 2 +- templates/repo/diff_box.tmpl | 2 +- 4 files changed, 53 insertions(+), 47 deletions(-) diff --git a/gogs.go b/gogs.go index add16ae04d6..1d17923d607 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.8.17.0108" +const APP_VER = "0.8.17.0109" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/modules/base/markdown.go b/modules/base/markdown.go index 04547786409..a3d3a7ca809 100644 --- a/modules/base/markdown.go +++ b/modules/base/markdown.go @@ -83,21 +83,63 @@ func IsReadmeFile(name string) bool { return false } +var ( + MentionPattern = regexp.MustCompile(`(\s|^)@[0-9a-zA-Z_\.]+`) + commitPattern = regexp.MustCompile(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`) + issueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`) + issueIndexPattern = regexp.MustCompile(`( |^|\()#[0-9]+\b`) + sha1CurrentPattern = regexp.MustCompile(`\b[0-9a-f]{40}\b`) +) + type CustomRender struct { blackfriday.Renderer urlPrefix string } -func (options *CustomRender) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) { +func (r *CustomRender) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) { if len(link) > 0 && !isLink(link) { if link[0] == '#' { // link = append([]byte(options.urlPrefix), link...) } else { - link = []byte(path.Join(options.urlPrefix, string(link))) + link = []byte(path.Join(r.urlPrefix, string(link))) } } - options.Renderer.Link(out, link, title, content) + r.Renderer.Link(out, link, title, content) +} + +func (r *CustomRender) AutoLink(out *bytes.Buffer, link []byte, kind int) { + if kind != 1 { + r.Renderer.AutoLink(out, link, kind) + return + } + + // This method could only possibly serve one link at a time, no need to find all. + m := commitPattern.Find(link) + if m != nil { + m = bytes.TrimSpace(m) + i := strings.Index(string(m), "commit/") + j := strings.Index(string(m), "#") + if j == -1 { + j = len(m) + } + out.WriteString(fmt.Sprintf(` %s`, m, ShortSha(string(m[i+7:j])))) + return + } + + m = issueFullPattern.Find(link) + if m != nil { + m = bytes.TrimSpace(m) + i := strings.Index(string(m), "issues/") + j := strings.Index(string(m), "#") + if j == -1 { + j = len(m) + } + out.WriteString(fmt.Sprintf(` #%s`, m, ShortSha(string(m[i+7:j])))) + return + } + + r.Renderer.AutoLink(out, link, kind) } var ( @@ -105,13 +147,13 @@ var ( svgSuffixWithMark = []byte(".svg?") ) -func (options *CustomRender) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) { - prefix := strings.Replace(options.urlPrefix, "/src/", "/raw/", 1) +func (r *CustomRender) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) { + prefix := strings.Replace(r.urlPrefix, "/src/", "/raw/", 1) if len(link) > 0 { if isLink(link) { // External link with .svg suffix usually means CI status. if bytes.HasSuffix(link, svgSuffix) || bytes.Contains(link, svgSuffixWithMark) { - options.Renderer.Image(out, link, title, alt) + r.Renderer.Image(out, link, title, alt) return } } else { @@ -125,18 +167,10 @@ func (options *CustomRender) Image(out *bytes.Buffer, link []byte, title []byte, out.WriteString(``) - options.Renderer.Image(out, link, title, alt) + r.Renderer.Image(out, link, title, alt) out.WriteString("") } -var ( - MentionPattern = regexp.MustCompile(`(\s|^)@[0-9a-zA-Z_\.]+`) - commitPattern = regexp.MustCompile(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`) - issueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`) - issueIndexPattern = regexp.MustCompile(`( |^|\()#[0-9]+\b`) - sha1CurrentPattern = regexp.MustCompile(`\b[0-9a-f]{40}\b`) -) - func cutoutVerbosePrefix(prefix string) string { count := 0 for i := 0; i < len(prefix); i++ { @@ -229,33 +263,6 @@ var ( var noEndTags = []string{"img", "input", "br", "hr"} -// PreProcessMarkdown renders full links of commits, issues and pulls to shorter version. -func PreProcessMarkdown(rawHTML []byte, urlPrefix string) []byte { - ms := commitPattern.FindAll(rawHTML, -1) - for _, m := range ms { - m = bytes.TrimSpace(m) - i := strings.Index(string(m), "commit/") - j := strings.Index(string(m), "#") - if j == -1 { - j = len(m) - } - rawHTML = bytes.Replace(rawHTML, m, []byte(fmt.Sprintf( - ` %s`, m, ShortSha(string(m[i+7:j])))), -1) - } - ms = issueFullPattern.FindAll(rawHTML, -1) - for _, m := range ms { - m = bytes.TrimSpace(m) - i := strings.Index(string(m), "issues/") - j := strings.Index(string(m), "#") - if j == -1 { - j = len(m) - } - rawHTML = bytes.Replace(rawHTML, m, []byte(fmt.Sprintf( - ` #%s`, m, ShortSha(string(m[i+7:j])))), -1) - } - return rawHTML -} - // PostProcessMarkdown treats different types of HTML differently, // and only renders special links for plain text blocks. func PostProcessMarkdown(rawHtml []byte, urlPrefix string, metas map[string]string) []byte { @@ -327,8 +334,7 @@ OUTER_LOOP: } func RenderMarkdown(rawBytes []byte, urlPrefix string, metas map[string]string) []byte { - result := PreProcessMarkdown(rawBytes, urlPrefix) - result = RenderRawMarkdown(result, urlPrefix) + result := RenderRawMarkdown(rawBytes, urlPrefix) result = PostProcessMarkdown(result, urlPrefix, metas) result = Sanitizer.SanitizeBytes(result) return result diff --git a/templates/.VERSION b/templates/.VERSION index 383711930fa..35fe4c73bfa 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.8.17.0108 \ No newline at end of file +0.8.17.0109 \ No newline at end of file diff --git a/templates/repo/diff_box.tmpl b/templates/repo/diff_box.tmpl index a12f4de4ec6..3093210b484 100644 --- a/templates/repo/diff_box.tmpl +++ b/templates/repo/diff_box.tmpl @@ -93,7 +93,7 @@ {{if eq .Type 4}} - {{if gt $j 0}}{{end}} + {{/* {{if gt $j 0}}{{end}} */}} {{else}}