markdown.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. "path"
  10. "path/filepath"
  11. "regexp"
  12. "strings"
  13. "github.com/Unknwon/com"
  14. "github.com/microcosm-cc/bluemonday"
  15. "github.com/russross/blackfriday"
  16. "golang.org/x/net/html"
  17. "github.com/gogits/gogs/modules/base"
  18. "github.com/gogits/gogs/modules/setting"
  19. )
  20. const (
  21. ISSUE_NAME_STYLE_NUMERIC = "numeric"
  22. ISSUE_NAME_STYLE_ALPHANUMERIC = "alphanumeric"
  23. )
  24. var Sanitizer = bluemonday.UGCPolicy()
  25. // BuildSanitizer initializes sanitizer with allowed attributes based on settings.
  26. // This function should only be called once during entire application lifecycle.
  27. func BuildSanitizer() {
  28. // Normal markdown-stuff
  29. Sanitizer.AllowAttrs("class").Matching(regexp.MustCompile(`[\p{L}\p{N}\s\-_',:\[\]!\./\\\(\)&]*`)).OnElements("code")
  30. // Checkboxes
  31. Sanitizer.AllowAttrs("type").Matching(regexp.MustCompile(`^checkbox$`)).OnElements("input")
  32. Sanitizer.AllowAttrs("checked", "disabled").OnElements("input")
  33. // Custom URL-Schemes
  34. Sanitizer.AllowURLSchemes(setting.Markdown.CustomURLSchemes...)
  35. }
  36. var validLinksPattern = regexp.MustCompile(`^[a-z][\w-]+://`)
  37. // isLink reports whether link fits valid format.
  38. func isLink(link []byte) bool {
  39. return validLinksPattern.Match(link)
  40. }
  41. // IsMarkdownFile reports whether name looks like a Markdown file
  42. // based on its extension.
  43. func IsMarkdownFile(name string) bool {
  44. extension := strings.ToLower(filepath.Ext(name))
  45. for _, ext := range setting.Markdown.FileExtensions {
  46. if strings.ToLower(ext) == extension {
  47. return true
  48. }
  49. }
  50. return false
  51. }
  52. // IsReadmeFile reports whether name looks like a README file
  53. // based on its extension.
  54. func IsReadmeFile(name string) bool {
  55. name = strings.ToLower(name)
  56. if len(name) < 6 {
  57. return false
  58. } else if len(name) == 6 {
  59. return name == "readme"
  60. }
  61. return name[:7] == "readme."
  62. }
  63. var (
  64. // MentionPattern matches string that mentions someone, e.g. @Unknwon
  65. MentionPattern = regexp.MustCompile(`(\s|^)@[0-9a-zA-Z-_\.]+`)
  66. // CommitPattern matches link to certain commit with or without trailing hash,
  67. // e.g. https://try.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2
  68. CommitPattern = regexp.MustCompile(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`)
  69. // IssueFullPattern matches link to an issue with or without trailing hash,
  70. // e.g. https://try.gogs.io/gogs/gogs/issues/4#issue-685
  71. IssueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`)
  72. // IssueNumericPattern matches string that references to a numeric issue, e.g. #1287
  73. IssueNumericPattern = regexp.MustCompile(`( |^|\()#[0-9]+\b`)
  74. // IssueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234
  75. IssueAlphanumericPattern = regexp.MustCompile(`( |^|\()[A-Z]{1,10}-[1-9][0-9]*\b`)
  76. // Sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae
  77. // FIXME: this pattern matches pure numbers as well, right now we do a hack to check in RenderSha1CurrentPattern
  78. // by converting string to a number.
  79. Sha1CurrentPattern = regexp.MustCompile(`\b[0-9a-f]{40}\b`)
  80. )
  81. // FindAllMentions matches mention patterns in given content
  82. // and returns a list of found user names without @ prefix.
  83. func FindAllMentions(content string) []string {
  84. mentions := MentionPattern.FindAllString(content, -1)
  85. for i := range mentions {
  86. mentions[i] = strings.TrimSpace(mentions[i])[1:] // Strip @ character
  87. }
  88. return mentions
  89. }
  90. // Renderer is a extended version of underlying render object.
  91. type Renderer struct {
  92. blackfriday.Renderer
  93. urlPrefix string
  94. }
  95. // Link defines how formal links should be processed to produce corresponding HTML elements.
  96. func (r *Renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
  97. if len(link) > 0 && !isLink(link) {
  98. if link[0] != '#' {
  99. link = []byte(path.Join(r.urlPrefix, string(link)))
  100. }
  101. }
  102. r.Renderer.Link(out, link, title, content)
  103. }
  104. // AutoLink defines how auto-detected links should be processed to produce corresponding HTML elements.
  105. // Reference for kind: https://github.com/russross/blackfriday/blob/master/markdown.go#L69-L76
  106. func (r *Renderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {
  107. if kind != blackfriday.LINK_TYPE_NORMAL {
  108. r.Renderer.AutoLink(out, link, kind)
  109. return
  110. }
  111. // Since this method could only possibly serve one link at a time,
  112. // we do not need to find all.
  113. if bytes.HasPrefix(link, []byte(setting.AppUrl)) {
  114. m := CommitPattern.Find(link)
  115. if m != nil {
  116. m = bytes.TrimSpace(m)
  117. i := strings.Index(string(m), "commit/")
  118. j := strings.Index(string(m), "#")
  119. if j == -1 {
  120. j = len(m)
  121. }
  122. out.WriteString(fmt.Sprintf(` <code><a href="%s">%s</a></code>`, m, base.ShortSha(string(m[i+7:j]))))
  123. return
  124. }
  125. m = IssueFullPattern.Find(link)
  126. if m != nil {
  127. m = bytes.TrimSpace(m)
  128. i := strings.Index(string(m), "issues/")
  129. j := strings.Index(string(m), "#")
  130. if j == -1 {
  131. j = len(m)
  132. }
  133. out.WriteString(fmt.Sprintf(`<a href="%s">#%s</a>`, m, base.ShortSha(string(m[i+7:j]))))
  134. return
  135. }
  136. }
  137. r.Renderer.AutoLink(out, link, kind)
  138. }
  139. // ListItem defines how list items should be processed to produce corresponding HTML elements.
  140. func (options *Renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
  141. // Detect procedures to draw checkboxes.
  142. switch {
  143. case bytes.HasPrefix(text, []byte("[ ] ")):
  144. text = append([]byte(`<input type="checkbox" disabled="" />`), text[3:]...)
  145. case bytes.HasPrefix(text, []byte("[x] ")):
  146. text = append([]byte(`<input type="checkbox" disabled="" checked="" />`), text[3:]...)
  147. }
  148. options.Renderer.ListItem(out, text, flags)
  149. }
  150. // Note: this section is for purpose of increase performance and
  151. // reduce memory allocation at runtime since they are constant literals.
  152. var (
  153. svgSuffix = []byte(".svg")
  154. svgSuffixWithMark = []byte(".svg?")
  155. spaceBytes = []byte(" ")
  156. spaceEncodedBytes = []byte("%20")
  157. space = " "
  158. spaceEncoded = "%20"
  159. )
  160. // Image defines how images should be processed to produce corresponding HTML elements.
  161. func (r *Renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
  162. prefix := strings.Replace(r.urlPrefix, "/src/", "/raw/", 1)
  163. if len(link) > 0 {
  164. if isLink(link) {
  165. // External link with .svg suffix usually means CI status.
  166. // TODO: define a keyword to allow non-svg images render as external link.
  167. if bytes.HasSuffix(link, svgSuffix) || bytes.Contains(link, svgSuffixWithMark) {
  168. r.Renderer.Image(out, link, title, alt)
  169. return
  170. }
  171. } else {
  172. if link[0] != '/' {
  173. prefix += "/"
  174. }
  175. link = bytes.Replace([]byte((prefix + string(link))), spaceBytes, spaceEncodedBytes, -1)
  176. fmt.Println(333, string(link))
  177. }
  178. }
  179. out.WriteString(`<a href="`)
  180. out.Write(link)
  181. out.WriteString(`">`)
  182. r.Renderer.Image(out, link, title, alt)
  183. out.WriteString("</a>")
  184. }
  185. // cutoutVerbosePrefix cutouts URL prefix including sub-path to
  186. // return a clean unified string of request URL path.
  187. func cutoutVerbosePrefix(prefix string) string {
  188. if len(prefix) == 0 || prefix[0] != '/' {
  189. return prefix
  190. }
  191. count := 0
  192. for i := 0; i < len(prefix); i++ {
  193. if prefix[i] == '/' {
  194. count++
  195. }
  196. if count >= 3+setting.AppSubUrlDepth {
  197. return prefix[:i]
  198. }
  199. }
  200. return prefix
  201. }
  202. // RenderIssueIndexPattern renders issue indexes to corresponding links.
  203. func RenderIssueIndexPattern(rawBytes []byte, urlPrefix string, metas map[string]string) []byte {
  204. urlPrefix = cutoutVerbosePrefix(urlPrefix)
  205. pattern := IssueNumericPattern
  206. if metas["style"] == ISSUE_NAME_STYLE_ALPHANUMERIC {
  207. pattern = IssueAlphanumericPattern
  208. }
  209. ms := pattern.FindAll(rawBytes, -1)
  210. for _, m := range ms {
  211. if m[0] == ' ' || m[0] == '(' {
  212. m = m[1:] // ignore leading space or opening parentheses
  213. }
  214. var link string
  215. if metas == nil {
  216. link = fmt.Sprintf(`<a href="%s/issues/%s">%s</a>`, urlPrefix, m[1:], m)
  217. } else {
  218. // Support for external issue tracker
  219. if metas["style"] == ISSUE_NAME_STYLE_ALPHANUMERIC {
  220. metas["index"] = string(m)
  221. } else {
  222. metas["index"] = string(m[1:])
  223. }
  224. link = fmt.Sprintf(`<a href="%s">%s</a>`, com.Expand(metas["format"], metas), m)
  225. }
  226. rawBytes = bytes.Replace(rawBytes, m, []byte(link), 1)
  227. }
  228. return rawBytes
  229. }
  230. // RenderSha1CurrentPattern renders SHA1 strings to corresponding links that assumes in the same repository.
  231. func RenderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte {
  232. return []byte(Sha1CurrentPattern.ReplaceAllStringFunc(string(rawBytes[:]), func(m string) string {
  233. if com.StrTo(m).MustInt() > 0 {
  234. return m
  235. }
  236. return fmt.Sprintf(`<a href="%s/commit/%s"><code>%s</code></a>`, urlPrefix, m, base.ShortSha(string(m)))
  237. }))
  238. }
  239. // RenderSpecialLink renders mentions, indexes and SHA1 strings to corresponding links.
  240. func RenderSpecialLink(rawBytes []byte, urlPrefix string, metas map[string]string) []byte {
  241. ms := MentionPattern.FindAll(rawBytes, -1)
  242. for _, m := range ms {
  243. m = bytes.TrimSpace(m)
  244. rawBytes = bytes.Replace(rawBytes, m,
  245. []byte(fmt.Sprintf(`<a href="%s/%s">%s</a>`, setting.AppSubUrl, m[1:], m)), -1)
  246. }
  247. rawBytes = RenderIssueIndexPattern(rawBytes, urlPrefix, metas)
  248. rawBytes = RenderSha1CurrentPattern(rawBytes, urlPrefix)
  249. return rawBytes
  250. }
  251. // RenderRaw renders Markdown to HTML without handling special links.
  252. func RenderRaw(body []byte, urlPrefix string) []byte {
  253. htmlFlags := 0
  254. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  255. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  256. renderer := &Renderer{
  257. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  258. urlPrefix: urlPrefix,
  259. }
  260. // set up the parser
  261. extensions := 0
  262. extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
  263. extensions |= blackfriday.EXTENSION_TABLES
  264. extensions |= blackfriday.EXTENSION_FENCED_CODE
  265. extensions |= blackfriday.EXTENSION_AUTOLINK
  266. extensions |= blackfriday.EXTENSION_STRIKETHROUGH
  267. extensions |= blackfriday.EXTENSION_SPACE_HEADERS
  268. extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
  269. if setting.Markdown.EnableHardLineBreak {
  270. extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
  271. }
  272. body = blackfriday.Markdown(body, renderer, extensions)
  273. return body
  274. }
  275. var (
  276. leftAngleBracket = []byte("</")
  277. rightAngleBracket = []byte(">")
  278. )
  279. var noEndTags = []string{"img", "input", "br", "hr"}
  280. // PostProcess treats different types of HTML differently,
  281. // and only renders special links for plain text blocks.
  282. func PostProcess(rawHtml []byte, urlPrefix string, metas map[string]string) []byte {
  283. startTags := make([]string, 0, 5)
  284. var buf bytes.Buffer
  285. tokenizer := html.NewTokenizer(bytes.NewReader(rawHtml))
  286. OUTER_LOOP:
  287. for html.ErrorToken != tokenizer.Next() {
  288. token := tokenizer.Token()
  289. switch token.Type {
  290. case html.TextToken:
  291. buf.Write(RenderSpecialLink([]byte(token.String()), urlPrefix, metas))
  292. case html.StartTagToken:
  293. buf.WriteString(token.String())
  294. tagName := token.Data
  295. // If this is an excluded tag, we skip processing all output until a close tag is encountered.
  296. if strings.EqualFold("a", tagName) || strings.EqualFold("code", tagName) || strings.EqualFold("pre", tagName) {
  297. stackNum := 1
  298. for html.ErrorToken != tokenizer.Next() {
  299. token = tokenizer.Token()
  300. // Copy the token to the output verbatim
  301. buf.WriteString(token.String())
  302. if token.Type == html.StartTagToken {
  303. stackNum++
  304. }
  305. // If this is the close tag to the outer-most, we are done
  306. if token.Type == html.EndTagToken {
  307. stackNum--
  308. if stackNum <= 0 && strings.EqualFold(tagName, token.Data) {
  309. break
  310. }
  311. }
  312. }
  313. continue OUTER_LOOP
  314. }
  315. if !com.IsSliceContainsStr(noEndTags, token.Data) {
  316. startTags = append(startTags, token.Data)
  317. }
  318. case html.EndTagToken:
  319. if len(startTags) == 0 {
  320. buf.WriteString(token.String())
  321. break
  322. }
  323. buf.Write(leftAngleBracket)
  324. buf.WriteString(startTags[len(startTags)-1])
  325. buf.Write(rightAngleBracket)
  326. startTags = startTags[:len(startTags)-1]
  327. default:
  328. buf.WriteString(token.String())
  329. }
  330. }
  331. if io.EOF == tokenizer.Err() {
  332. return buf.Bytes()
  333. }
  334. // If we are not at the end of the input, then some other parsing error has occurred,
  335. // so return the input verbatim.
  336. return rawHtml
  337. }
  338. // Render renders Markdown to HTML with special links.
  339. func Render(rawBytes []byte, urlPrefix string, metas map[string]string) []byte {
  340. urlPrefix = strings.Replace(urlPrefix, space, spaceEncoded, -1)
  341. result := RenderRaw(rawBytes, urlPrefix)
  342. result = PostProcess(result, urlPrefix, metas)
  343. result = Sanitizer.SanitizeBytes(result)
  344. return result
  345. }
  346. // RenderString renders Markdown to HTML with special links and returns string type.
  347. func RenderString(raw, urlPrefix string, metas map[string]string) string {
  348. return string(Render([]byte(raw), urlPrefix, metas))
  349. }