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

728 lines
21 KiB

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