You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

markdown.go 22 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
Squashed commit of the following: commit 0afcb843d7ffd596991c4885cab768273a6eb42c Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 17:13:29 2016 -0600 Removed Upload stats as the upload table is just a temporary table commit 7ecd73ff5535612d79d471409173ee7f1fcfa157 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:42:41 2016 -0600 Fix for CodeMirror mode commit c29b9ab531e2e7af0fb5db24dc17e51027dd1174 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:03:33 2016 -0600 Made tabbing in editor use spaces commit 23af384c53206a8a40e11e45bf49d7a149c4adcd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:56:46 2016 -0600 Fix for data-url commit cfb8a97591cb6fc0a92e49563b7b764c524db0e9 Merge: 7fc8a89 991ce42 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:42:53 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit 7fc8a89cb495478225b02d613e647f99a1489634 Merge: fd3d86c c03d040 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:40:00 2016 -0600 Merge branch 'feature-create-and-edit-repo-file' of github.com:richmahn/gogs into feature-create-and-edit-repo-file commit fd3d86ca6bbc02cfda566a504ffd6b03db4f75ef Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:39:44 2016 -0600 Code cleanup commit c03d0401c1049eeeccc32ab1f9c3303c130be5ee Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 29 15:38:23 2016 -0600 Code cleanup commit 98e1206ccf9f9a4503c020e3a7830cf9f861dfae Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:36:01 2016 -0600 Code cleanup and fixes commit c2895dc742f25f8412879c9fa15e18f27f42f194 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:24:04 2016 -0600 Fixes per Unknwon's requests commit 6aa7e46b21ad4c96e562daa2eac26a8fb408f8ef Merge: 889e9fa ad7ea88 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 17:13:43 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go modules/setting/setting.go commit 889e9faf1bd8559a4979c8f46005d488c1a234d4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:09:18 2016 -0600 Fix in gogs.js commit 47603edf223f147b114be65f3bd27bc1e88827a5 Merge: bb57912 cf85e9e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:07:36 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit bb5791255867a71c11a77b639db050ad09c597a4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:02:18 2016 -0600 Update for using CodeMirror mode addon commit d10d128c51039be19e2af9c66c63db66a9f2ec6d Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 16:12:57 2016 -0600 Update for Edit commit 34a34982025144e3225e389f7849eb6273c1d576 Merge: fa1b752 1c7dcdd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 11:52:02 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go commit fa1b752be29cd455c5184ddac2ffe80b3489763e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 15 18:35:42 2016 -0600 Feature for editing, creating, uploading and deleting files
8 years ago
Squashed commit of the following: commit 0afcb843d7ffd596991c4885cab768273a6eb42c Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 17:13:29 2016 -0600 Removed Upload stats as the upload table is just a temporary table commit 7ecd73ff5535612d79d471409173ee7f1fcfa157 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:42:41 2016 -0600 Fix for CodeMirror mode commit c29b9ab531e2e7af0fb5db24dc17e51027dd1174 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:03:33 2016 -0600 Made tabbing in editor use spaces commit 23af384c53206a8a40e11e45bf49d7a149c4adcd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:56:46 2016 -0600 Fix for data-url commit cfb8a97591cb6fc0a92e49563b7b764c524db0e9 Merge: 7fc8a89 991ce42 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:42:53 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit 7fc8a89cb495478225b02d613e647f99a1489634 Merge: fd3d86c c03d040 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:40:00 2016 -0600 Merge branch 'feature-create-and-edit-repo-file' of github.com:richmahn/gogs into feature-create-and-edit-repo-file commit fd3d86ca6bbc02cfda566a504ffd6b03db4f75ef Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:39:44 2016 -0600 Code cleanup commit c03d0401c1049eeeccc32ab1f9c3303c130be5ee Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 29 15:38:23 2016 -0600 Code cleanup commit 98e1206ccf9f9a4503c020e3a7830cf9f861dfae Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:36:01 2016 -0600 Code cleanup and fixes commit c2895dc742f25f8412879c9fa15e18f27f42f194 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:24:04 2016 -0600 Fixes per Unknwon's requests commit 6aa7e46b21ad4c96e562daa2eac26a8fb408f8ef Merge: 889e9fa ad7ea88 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 17:13:43 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go modules/setting/setting.go commit 889e9faf1bd8559a4979c8f46005d488c1a234d4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:09:18 2016 -0600 Fix in gogs.js commit 47603edf223f147b114be65f3bd27bc1e88827a5 Merge: bb57912 cf85e9e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:07:36 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit bb5791255867a71c11a77b639db050ad09c597a4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:02:18 2016 -0600 Update for using CodeMirror mode addon commit d10d128c51039be19e2af9c66c63db66a9f2ec6d Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 16:12:57 2016 -0600 Update for Edit commit 34a34982025144e3225e389f7849eb6273c1d576 Merge: fa1b752 1c7dcdd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 11:52:02 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go commit fa1b752be29cd455c5184ddac2ffe80b3489763e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 15 18:35:42 2016 -0600 Feature for editing, creating, uploading and deleting files
8 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
9 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package markdown
  5. import (
  6. "bytes"
  7. "fmt"
  8. "io"
  9. "net/url"
  10. "path"
  11. "path/filepath"
  12. "regexp"
  13. "strings"
  14. "code.gitea.io/gitea/modules/base"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/markup"
  17. "code.gitea.io/gitea/modules/setting"
  18. "github.com/Unknwon/com"
  19. "github.com/russross/blackfriday"
  20. "golang.org/x/net/html"
  21. )
  22. // Issue name styles
  23. const (
  24. IssueNameStyleNumeric = "numeric"
  25. IssueNameStyleAlphanumeric = "alphanumeric"
  26. )
  27. // IsMarkdownFile reports whether name looks like a Markdown file
  28. // based on its extension.
  29. func IsMarkdownFile(name string) bool {
  30. extension := strings.ToLower(filepath.Ext(name))
  31. for _, ext := range setting.Markdown.FileExtensions {
  32. if strings.ToLower(ext) == extension {
  33. return true
  34. }
  35. }
  36. return false
  37. }
  38. var (
  39. // NOTE: All below regex matching do not perform any extra validation.
  40. // Thus a link is produced even if the user does not exist, the issue does not exist, the commit does not exist, etc.
  41. // While fast, this is also incorrect and lead to false positives.
  42. // MentionPattern matches string that mentions someone, e.g. @Unknwon
  43. MentionPattern = regexp.MustCompile(`(\s|^|\W)@[0-9a-zA-Z-_\.]+`)
  44. // IssueNumericPattern matches string that references to a numeric issue, e.g. #1287
  45. IssueNumericPattern = regexp.MustCompile(`( |^|\()#[0-9]+\b`)
  46. // IssueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234
  47. IssueAlphanumericPattern = regexp.MustCompile(`( |^|\()[A-Z]{1,10}-[1-9][0-9]*\b`)
  48. // CrossReferenceIssueNumericPattern matches string that references a numeric issue in a different repository
  49. // e.g. gogits/gogs#12345
  50. CrossReferenceIssueNumericPattern = regexp.MustCompile(`( |^)[0-9a-zA-Z]+/[0-9a-zA-Z]+#[0-9]+\b`)
  51. // Sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae
  52. // Although SHA1 hashes are 40 chars long, the regex matches the hash from 7 to 40 chars in length
  53. // so that abbreviated hash links can be used as well. This matches git and github useability.
  54. Sha1CurrentPattern = regexp.MustCompile(`(?:^|\s|\()([0-9a-f]{7,40})\b`)
  55. // ShortLinkPattern matches short but difficult to parse [[name|link|arg=test]] syntax
  56. ShortLinkPattern = regexp.MustCompile(`(\[\[.*\]\]\w*)`)
  57. // AnySHA1Pattern allows to split url containing SHA into parts
  58. AnySHA1Pattern = regexp.MustCompile(`(http\S*)://(\S+)/(\S+)/(\S+)/(\S+)/([0-9a-f]{40})(?:/?([^#\s]+)?(?:#(\S+))?)?`)
  59. // IssueFullPattern allows to split issue (and pull) URLs into parts
  60. IssueFullPattern = regexp.MustCompile(`(?:^|\s|\()(http\S*)://((?:[^\s/]+/)+)((?:\w{1,10}-)?[1-9][0-9]*)([\?|#]\S+.(\S+)?)?\b`)
  61. validLinksPattern = regexp.MustCompile(`^[a-z][\w-]+://`)
  62. )
  63. // isLink reports whether link fits valid format.
  64. func isLink(link []byte) bool {
  65. return validLinksPattern.Match(link)
  66. }
  67. // FindAllMentions matches mention patterns in given content
  68. // and returns a list of found user names without @ prefix.
  69. func FindAllMentions(content string) []string {
  70. mentions := MentionPattern.FindAllString(content, -1)
  71. for i := range mentions {
  72. mentions[i] = mentions[i][strings.Index(mentions[i], "@")+1:] // Strip @ character
  73. }
  74. return mentions
  75. }
  76. // Renderer is a extended version of underlying render object.
  77. type Renderer struct {
  78. blackfriday.Renderer
  79. urlPrefix string
  80. isWikiMarkdown bool
  81. }
  82. // Link defines how formal links should be processed to produce corresponding HTML elements.
  83. func (r *Renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
  84. if len(link) > 0 && !isLink(link) {
  85. if link[0] != '#' {
  86. lnk := string(link)
  87. if r.isWikiMarkdown {
  88. lnk = URLJoin("wiki", lnk)
  89. }
  90. mLink := URLJoin(r.urlPrefix, lnk)
  91. link = []byte(mLink)
  92. }
  93. }
  94. r.Renderer.Link(out, link, title, content)
  95. }
  96. // List renders markdown bullet or digit lists to HTML
  97. func (r *Renderer) List(out *bytes.Buffer, text func() bool, flags int) {
  98. marker := out.Len()
  99. if out.Len() > 0 {
  100. out.WriteByte('\n')
  101. }
  102. if flags&blackfriday.LIST_TYPE_DEFINITION != 0 {
  103. out.WriteString("<dl>")
  104. } else if flags&blackfriday.LIST_TYPE_ORDERED != 0 {
  105. out.WriteString("<ol class='ui list'>")
  106. } else {
  107. out.WriteString("<ul class='ui list'>")
  108. }
  109. if !text() {
  110. out.Truncate(marker)
  111. return
  112. }
  113. if flags&blackfriday.LIST_TYPE_DEFINITION != 0 {
  114. out.WriteString("</dl>\n")
  115. } else if flags&blackfriday.LIST_TYPE_ORDERED != 0 {
  116. out.WriteString("</ol>\n")
  117. } else {
  118. out.WriteString("</ul>\n")
  119. }
  120. }
  121. // ListItem defines how list items should be processed to produce corresponding HTML elements.
  122. func (r *Renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
  123. // Detect procedures to draw checkboxes.
  124. prefix := ""
  125. if bytes.HasPrefix(text, []byte("<p>")) {
  126. prefix = "<p>"
  127. }
  128. switch {
  129. case bytes.HasPrefix(text, []byte(prefix+"[ ] ")):
  130. text = append([]byte(`<span class="ui fitted disabled checkbox"><input type="checkbox" disabled="disabled" /><label /></span>`), text[3+len(prefix):]...)
  131. if prefix != "" {
  132. text = bytes.Replace(text, []byte(prefix), []byte{}, 1)
  133. }
  134. case bytes.HasPrefix(text, []byte(prefix+"[x] ")):
  135. text = append([]byte(`<span class="ui checked fitted disabled checkbox"><input type="checkbox" checked="" disabled="disabled" /><label /></span>`), text[3+len(prefix):]...)
  136. if prefix != "" {
  137. text = bytes.Replace(text, []byte(prefix), []byte{}, 1)
  138. }
  139. }
  140. r.Renderer.ListItem(out, text, flags)
  141. }
  142. // Note: this section is for purpose of increase performance and
  143. // reduce memory allocation at runtime since they are constant literals.
  144. var (
  145. svgSuffix = []byte(".svg")
  146. svgSuffixWithMark = []byte(".svg?")
  147. )
  148. // Image defines how images should be processed to produce corresponding HTML elements.
  149. func (r *Renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
  150. prefix := r.urlPrefix
  151. if r.isWikiMarkdown {
  152. prefix = URLJoin(prefix, "wiki", "src")
  153. }
  154. prefix = strings.Replace(prefix, "/src/", "/raw/", 1)
  155. if len(link) > 0 {
  156. if isLink(link) {
  157. // External link with .svg suffix usually means CI status.
  158. // TODO: define a keyword to allow non-svg images render as external link.
  159. if bytes.HasSuffix(link, svgSuffix) || bytes.Contains(link, svgSuffixWithMark) {
  160. r.Renderer.Image(out, link, title, alt)
  161. return
  162. }
  163. } else {
  164. lnk := string(link)
  165. lnk = URLJoin(prefix, lnk)
  166. lnk = strings.Replace(lnk, " ", "+", -1)
  167. link = []byte(lnk)
  168. }
  169. }
  170. out.WriteString(`<a href="`)
  171. out.Write(link)
  172. out.WriteString(`">`)
  173. r.Renderer.Image(out, link, title, alt)
  174. out.WriteString("</a>")
  175. }
  176. // cutoutVerbosePrefix cutouts URL prefix including sub-path to
  177. // return a clean unified string of request URL path.
  178. func cutoutVerbosePrefix(prefix string) string {
  179. if len(prefix) == 0 || prefix[0] != '/' {
  180. return prefix
  181. }
  182. count := 0
  183. for i := 0; i < len(prefix); i++ {
  184. if prefix[i] == '/' {
  185. count++
  186. }
  187. if count >= 3+setting.AppSubURLDepth {
  188. return prefix[:i]
  189. }
  190. }
  191. return prefix
  192. }
  193. // URLJoin joins url components, like path.Join, but preserving contents
  194. func URLJoin(base string, elems ...string) string {
  195. u, err := url.Parse(base)
  196. if err != nil {
  197. log.Error(4, "URLJoin: Invalid base URL %s", base)
  198. return ""
  199. }
  200. joinArgs := make([]string, 0, len(elems)+1)
  201. joinArgs = append(joinArgs, u.Path)
  202. joinArgs = append(joinArgs, elems...)
  203. u.Path = path.Join(joinArgs...)
  204. return u.String()
  205. }
  206. // RenderIssueIndexPattern renders issue indexes to corresponding links.
  207. func RenderIssueIndexPattern(rawBytes []byte, urlPrefix string, metas map[string]string) []byte {
  208. urlPrefix = cutoutVerbosePrefix(urlPrefix)
  209. pattern := IssueNumericPattern
  210. if metas["style"] == IssueNameStyleAlphanumeric {
  211. pattern = IssueAlphanumericPattern
  212. }
  213. ms := pattern.FindAll(rawBytes, -1)
  214. for _, m := range ms {
  215. if m[0] == ' ' || m[0] == '(' {
  216. m = m[1:] // ignore leading space or opening parentheses
  217. }
  218. var link string
  219. if metas == nil {
  220. link = fmt.Sprintf(`<a href="%s">%s</a>`, URLJoin(urlPrefix, "issues", string(m[1:])), m)
  221. } else {
  222. // Support for external issue tracker
  223. if metas["style"] == IssueNameStyleAlphanumeric {
  224. metas["index"] = string(m)
  225. } else {
  226. metas["index"] = string(m[1:])
  227. }
  228. link = fmt.Sprintf(`<a href="%s">%s</a>`, com.Expand(metas["format"], metas), m)
  229. }
  230. rawBytes = bytes.Replace(rawBytes, m, []byte(link), 1)
  231. }
  232. return rawBytes
  233. }
  234. // IsSameDomain checks if given url string has the same hostname as current Gitea instance
  235. func IsSameDomain(s string) bool {
  236. if strings.HasPrefix(s, "/") {
  237. return true
  238. }
  239. if uapp, err := url.Parse(setting.AppURL); err == nil {
  240. if u, err := url.Parse(s); err == nil {
  241. return u.Host == uapp.Host
  242. }
  243. return false
  244. }
  245. return false
  246. }
  247. // renderFullSha1Pattern renders SHA containing URLs
  248. func renderFullSha1Pattern(rawBytes []byte, urlPrefix string) []byte {
  249. ms := AnySHA1Pattern.FindAllSubmatch(rawBytes, -1)
  250. for _, m := range ms {
  251. all := m[0]
  252. protocol := string(m[1])
  253. paths := string(m[2])
  254. path := protocol + "://" + paths
  255. author := string(m[3])
  256. repoName := string(m[4])
  257. path = URLJoin(path, author, repoName)
  258. ltype := "src"
  259. itemType := m[5]
  260. if IsSameDomain(paths) {
  261. ltype = string(itemType)
  262. } else if string(itemType) == "commit" {
  263. ltype = "commit"
  264. }
  265. sha := m[6]
  266. var subtree string
  267. if len(m) > 7 && len(m[7]) > 0 {
  268. subtree = string(m[7])
  269. }
  270. var line []byte
  271. if len(m) > 8 && len(m[8]) > 0 {
  272. line = m[8]
  273. }
  274. urlSuffix := ""
  275. text := base.ShortSha(string(sha))
  276. if subtree != "" {
  277. urlSuffix = "/" + subtree
  278. text += urlSuffix
  279. }
  280. if line != nil {
  281. value := string(line)
  282. urlSuffix += "#"
  283. urlSuffix += value
  284. text += " ("
  285. text += value
  286. text += ")"
  287. }
  288. rawBytes = bytes.Replace(rawBytes, all, []byte(fmt.Sprintf(
  289. `<a href="%s">%s</a>`, URLJoin(path, ltype, string(sha))+urlSuffix, text)), -1)
  290. }
  291. return rawBytes
  292. }
  293. // renderFullIssuePattern renders issues-like URLs
  294. func renderFullIssuePattern(rawBytes []byte, urlPrefix string) []byte {
  295. ms := IssueFullPattern.FindAllSubmatch(rawBytes, -1)
  296. for _, m := range ms {
  297. all := m[0]
  298. protocol := string(m[1])
  299. paths := bytes.Split(m[2], []byte("/"))
  300. paths = paths[:len(paths)-1]
  301. if bytes.HasPrefix(paths[0], []byte("gist.")) {
  302. continue
  303. }
  304. path := protocol + "://" + string(m[2])
  305. id := string(m[3])
  306. path = URLJoin(path, id)
  307. var comment []byte
  308. if len(m) > 3 {
  309. comment = m[4]
  310. }
  311. urlSuffix := ""
  312. text := "#" + id
  313. if comment != nil {
  314. urlSuffix += string(comment)
  315. text += " <i class='comment icon'></i>"
  316. }
  317. rawBytes = bytes.Replace(rawBytes, all, []byte(fmt.Sprintf(
  318. `<a href="%s%s">%s</a>`, path, urlSuffix, text)), -1)
  319. }
  320. return rawBytes
  321. }
  322. func firstIndexOfByte(sl []byte, target byte) int {
  323. for i := 0; i < len(sl); i++ {
  324. if sl[i] == target {
  325. return i
  326. }
  327. }
  328. return -1
  329. }
  330. func lastIndexOfByte(sl []byte, target byte) int {
  331. for i := len(sl) - 1; i >= 0; i-- {
  332. if sl[i] == target {
  333. return i
  334. }
  335. }
  336. return -1
  337. }
  338. // RenderShortLinks processes [[syntax]]
  339. //
  340. // noLink flag disables making link tags when set to true
  341. // so this function just replaces the whole [[...]] with the content text
  342. //
  343. // isWikiMarkdown is a flag to choose linking url prefix
  344. func RenderShortLinks(rawBytes []byte, urlPrefix string, noLink bool, isWikiMarkdown bool) []byte {
  345. ms := ShortLinkPattern.FindAll(rawBytes, -1)
  346. for _, m := range ms {
  347. orig := bytes.TrimSpace(m)
  348. m = orig[2:]
  349. tailPos := lastIndexOfByte(m, ']') + 1
  350. tail := []byte{}
  351. if tailPos < len(m) {
  352. tail = m[tailPos:]
  353. m = m[:tailPos-1]
  354. }
  355. m = m[:len(m)-2]
  356. props := map[string]string{}
  357. // MediaWiki uses [[link|text]], while GitHub uses [[text|link]]
  358. // It makes page handling terrible, but we prefer GitHub syntax
  359. // And fall back to MediaWiki only when it is obvious from the look
  360. // Of text and link contents
  361. sl := bytes.Split(m, []byte("|"))
  362. for _, v := range sl {
  363. switch bytes.Count(v, []byte("=")) {
  364. // Piped args without = sign, these are mandatory arguments
  365. case 0:
  366. {
  367. sv := string(v)
  368. if props["name"] == "" {
  369. if isLink(v) {
  370. // If we clearly see it is a link, we save it so
  371. // But first we need to ensure, that if both mandatory args provided
  372. // look like links, we stick to GitHub syntax
  373. if props["link"] != "" {
  374. props["name"] = props["link"]
  375. }
  376. props["link"] = strings.TrimSpace(sv)
  377. } else {
  378. props["name"] = sv
  379. }
  380. } else {
  381. props["link"] = strings.TrimSpace(sv)
  382. }
  383. }
  384. // Piped args with = sign, these are optional arguments
  385. case 1:
  386. {
  387. sep := firstIndexOfByte(v, '=')
  388. key, val := string(v[:sep]), html.UnescapeString(string(v[sep+1:]))
  389. lastCharIndex := len(val) - 1
  390. if (val[0] == '"' || val[0] == '\'') && (val[lastCharIndex] == '"' || val[lastCharIndex] == '\'') {
  391. val = val[1:lastCharIndex]
  392. }
  393. props[key] = val
  394. }
  395. }
  396. }
  397. var name string
  398. var link string
  399. if props["link"] != "" {
  400. link = props["link"]
  401. } else if props["name"] != "" {
  402. link = props["name"]
  403. }
  404. if props["title"] != "" {
  405. name = props["title"]
  406. } else if props["name"] != "" {
  407. name = props["name"]
  408. } else {
  409. name = link
  410. }
  411. name += string(tail)
  412. image := false
  413. ext := filepath.Ext(string(link))
  414. if ext != "" {
  415. switch ext {
  416. case ".jpg", ".jpeg", ".png", ".tif", ".tiff", ".webp", ".gif", ".bmp", ".ico", ".svg":
  417. {
  418. image = true
  419. }
  420. }
  421. }
  422. absoluteLink := isLink([]byte(link))
  423. if !absoluteLink {
  424. link = strings.Replace(link, " ", "+", -1)
  425. }
  426. if image {
  427. if !absoluteLink {
  428. if IsSameDomain(urlPrefix) {
  429. urlPrefix = strings.Replace(urlPrefix, "/src/", "/raw/", 1)
  430. }
  431. if isWikiMarkdown {
  432. link = URLJoin("wiki", "raw", link)
  433. }
  434. link = URLJoin(urlPrefix, link)
  435. }
  436. title := props["title"]
  437. if title == "" {
  438. title = props["alt"]
  439. }
  440. if title == "" {
  441. title = path.Base(string(name))
  442. }
  443. alt := props["alt"]
  444. if alt == "" {
  445. alt = name
  446. }
  447. if alt != "" {
  448. alt = `alt="` + alt + `"`
  449. }
  450. name = fmt.Sprintf(`<img src="%s" %s title="%s" />`, link, alt, title)
  451. } else if !absoluteLink {
  452. if isWikiMarkdown {
  453. link = URLJoin("wiki", link)
  454. }
  455. link = URLJoin(urlPrefix, link)
  456. }
  457. if noLink {
  458. rawBytes = bytes.Replace(rawBytes, orig, []byte(name), -1)
  459. } else {
  460. rawBytes = bytes.Replace(rawBytes, orig,
  461. []byte(fmt.Sprintf(`<a href="%s">%s</a>`, link, name)), -1)
  462. }
  463. }
  464. return rawBytes
  465. }
  466. // RenderCrossReferenceIssueIndexPattern renders issue indexes from other repositories to corresponding links.
  467. func RenderCrossReferenceIssueIndexPattern(rawBytes []byte, urlPrefix string, metas map[string]string) []byte {
  468. ms := CrossReferenceIssueNumericPattern.FindAll(rawBytes, -1)
  469. for _, m := range ms {
  470. if m[0] == ' ' || m[0] == '(' {
  471. m = m[1:] // ignore leading space or opening parentheses
  472. }
  473. repo := string(bytes.Split(m, []byte("#"))[0])
  474. issue := string(bytes.Split(m, []byte("#"))[1])
  475. link := fmt.Sprintf(`<a href="%s">%s</a>`, URLJoin(setting.AppURL, repo, "issues", issue), m)
  476. rawBytes = bytes.Replace(rawBytes, m, []byte(link), 1)
  477. }
  478. return rawBytes
  479. }
  480. // renderSha1CurrentPattern renders SHA1 strings to corresponding links that assumes in the same repository.
  481. func renderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte {
  482. ms := Sha1CurrentPattern.FindAllSubmatch(rawBytes, -1)
  483. for _, m := range ms {
  484. hash := m[1]
  485. // The regex does not lie, it matches the hash pattern.
  486. // However, a regex cannot know if a hash actually exists or not.
  487. // We could assume that a SHA1 hash should probably contain alphas AND numerics
  488. // but that is not always the case.
  489. // Although unlikely, deadbeef and 1234567 are valid short forms of SHA1 hash
  490. // as used by git and github for linking and thus we have to do similar.
  491. rawBytes = bytes.Replace(rawBytes, hash, []byte(fmt.Sprintf(
  492. `<a href="%s">%s</a>`, URLJoin(urlPrefix, "commit", string(hash)), base.ShortSha(string(hash)))), -1)
  493. }
  494. return rawBytes
  495. }
  496. // RenderSpecialLink renders mentions, indexes and SHA1 strings to corresponding links.
  497. func RenderSpecialLink(rawBytes []byte, urlPrefix string, metas map[string]string, isWikiMarkdown bool) []byte {
  498. ms := MentionPattern.FindAll(rawBytes, -1)
  499. for _, m := range ms {
  500. m = m[bytes.Index(m, []byte("@")):]
  501. rawBytes = bytes.Replace(rawBytes, m,
  502. []byte(fmt.Sprintf(`<a href="%s">%s</a>`, URLJoin(setting.AppURL, string(m[1:])), m)), -1)
  503. }
  504. rawBytes = RenderShortLinks(rawBytes, urlPrefix, false, isWikiMarkdown)
  505. rawBytes = RenderIssueIndexPattern(rawBytes, urlPrefix, metas)
  506. rawBytes = RenderCrossReferenceIssueIndexPattern(rawBytes, urlPrefix, metas)
  507. rawBytes = renderFullSha1Pattern(rawBytes, urlPrefix)
  508. rawBytes = renderSha1CurrentPattern(rawBytes, urlPrefix)
  509. rawBytes = renderFullIssuePattern(rawBytes, urlPrefix)
  510. return rawBytes
  511. }
  512. // RenderRaw renders Markdown to HTML without handling special links.
  513. func RenderRaw(body []byte, urlPrefix string, wikiMarkdown bool) []byte {
  514. htmlFlags := 0
  515. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  516. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  517. renderer := &Renderer{
  518. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  519. urlPrefix: urlPrefix,
  520. isWikiMarkdown: wikiMarkdown,
  521. }
  522. // set up the parser
  523. extensions := 0
  524. extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
  525. extensions |= blackfriday.EXTENSION_TABLES
  526. extensions |= blackfriday.EXTENSION_FENCED_CODE
  527. extensions |= blackfriday.EXTENSION_STRIKETHROUGH
  528. extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
  529. if setting.Markdown.EnableHardLineBreak {
  530. extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
  531. }
  532. body = blackfriday.Markdown(body, renderer, extensions)
  533. return body
  534. }
  535. var (
  536. leftAngleBracket = []byte("</")
  537. rightAngleBracket = []byte(">")
  538. )
  539. var noEndTags = []string{"img", "input", "br", "hr"}
  540. // PostProcess treats different types of HTML differently,
  541. // and only renders special links for plain text blocks.
  542. func PostProcess(rawHTML []byte, urlPrefix string, metas map[string]string, isWikiMarkdown bool) []byte {
  543. startTags := make([]string, 0, 5)
  544. var buf bytes.Buffer
  545. tokenizer := html.NewTokenizer(bytes.NewReader(rawHTML))
  546. OUTER_LOOP:
  547. for html.ErrorToken != tokenizer.Next() {
  548. token := tokenizer.Token()
  549. switch token.Type {
  550. case html.TextToken:
  551. buf.Write(RenderSpecialLink([]byte(token.String()), urlPrefix, metas, isWikiMarkdown))
  552. case html.StartTagToken:
  553. buf.WriteString(token.String())
  554. tagName := token.Data
  555. // If this is an excluded tag, we skip processing all output until a close tag is encountered.
  556. if strings.EqualFold("a", tagName) || strings.EqualFold("code", tagName) || strings.EqualFold("pre", tagName) {
  557. stackNum := 1
  558. for html.ErrorToken != tokenizer.Next() {
  559. token = tokenizer.Token()
  560. // Copy the token to the output verbatim
  561. buf.Write(RenderShortLinks([]byte(token.String()), urlPrefix, true, isWikiMarkdown))
  562. if token.Type == html.StartTagToken && !com.IsSliceContainsStr(noEndTags, token.Data) {
  563. stackNum++
  564. }
  565. // If this is the close tag to the outer-most, we are done
  566. if token.Type == html.EndTagToken {
  567. stackNum--
  568. if stackNum <= 0 && strings.EqualFold(tagName, token.Data) {
  569. break
  570. }
  571. }
  572. }
  573. continue OUTER_LOOP
  574. }
  575. if !com.IsSliceContainsStr(noEndTags, tagName) {
  576. startTags = append(startTags, tagName)
  577. }
  578. case html.EndTagToken:
  579. if len(startTags) == 0 {
  580. buf.WriteString(token.String())
  581. break
  582. }
  583. buf.Write(leftAngleBracket)
  584. buf.WriteString(startTags[len(startTags)-1])
  585. buf.Write(rightAngleBracket)
  586. startTags = startTags[:len(startTags)-1]
  587. default:
  588. buf.WriteString(token.String())
  589. }
  590. }
  591. if io.EOF == tokenizer.Err() {
  592. return buf.Bytes()
  593. }
  594. // If we are not at the end of the input, then some other parsing error has occurred,
  595. // so return the input verbatim.
  596. return rawHTML
  597. }
  598. // Render renders Markdown to HTML with all specific handling stuff.
  599. func render(rawBytes []byte, urlPrefix string, metas map[string]string, isWikiMarkdown bool) []byte {
  600. urlPrefix = strings.Replace(urlPrefix, " ", "+", -1)
  601. result := RenderRaw(rawBytes, urlPrefix, isWikiMarkdown)
  602. result = PostProcess(result, urlPrefix, metas, isWikiMarkdown)
  603. result = SanitizeBytes(result)
  604. return result
  605. }
  606. // Render renders Markdown to HTML with all specific handling stuff.
  607. func Render(rawBytes []byte, urlPrefix string, metas map[string]string) []byte {
  608. return render(rawBytes, urlPrefix, metas, false)
  609. }
  610. // RenderString renders Markdown to HTML with special links and returns string type.
  611. func RenderString(raw, urlPrefix string, metas map[string]string) string {
  612. return string(render([]byte(raw), urlPrefix, metas, false))
  613. }
  614. // RenderWiki renders markdown wiki page to HTML and return HTML string
  615. func RenderWiki(rawBytes []byte, urlPrefix string, metas map[string]string) string {
  616. return string(render(rawBytes, urlPrefix, metas, true))
  617. }
  618. var (
  619. // MarkupName describes markup's name
  620. MarkupName = "markdown"
  621. )
  622. func init() {
  623. markup.RegisterParser(Parser{})
  624. }
  625. // Parser implements markup.Parser
  626. type Parser struct {
  627. }
  628. // Name implements markup.Parser
  629. func (Parser) Name() string {
  630. return MarkupName
  631. }
  632. // Extensions implements markup.Parser
  633. func (Parser) Extensions() []string {
  634. return setting.Markdown.FileExtensions
  635. }
  636. // Render implements markup.Parser
  637. func (Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
  638. return render(rawBytes, urlPrefix, metas, isWiki)
  639. }