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.

727 lines
21 KiB

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