markdown.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 markup
  5. import (
  6. "bytes"
  7. "fmt"
  8. "path"
  9. "path/filepath"
  10. "regexp"
  11. "strings"
  12. "github.com/russross/blackfriday"
  13. "github.com/gogits/gogs/pkg/setting"
  14. "github.com/gogits/gogs/pkg/tool"
  15. )
  16. // IsMarkdownFile reports whether name looks like a Markdown file based on its extension.
  17. func IsMarkdownFile(name string) bool {
  18. extension := strings.ToLower(filepath.Ext(name))
  19. for _, ext := range setting.Markdown.FileExtensions {
  20. if strings.ToLower(ext) == extension {
  21. return true
  22. }
  23. }
  24. return false
  25. }
  26. // MarkdownRenderer is a extended version of underlying Markdown render object.
  27. type MarkdownRenderer struct {
  28. blackfriday.Renderer
  29. urlPrefix string
  30. }
  31. var validLinksPattern = regexp.MustCompile(`^[a-z][\w-]+://|^mailto:`)
  32. // isLink reports whether link fits valid format.
  33. func isLink(link []byte) bool {
  34. return validLinksPattern.Match(link)
  35. }
  36. // Link defines how formal links should be processed to produce corresponding HTML elements.
  37. func (r *MarkdownRenderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
  38. if len(link) > 0 && !isLink(link) {
  39. if link[0] != '#' {
  40. link = []byte(path.Join(r.urlPrefix, string(link)))
  41. }
  42. }
  43. r.Renderer.Link(out, link, title, content)
  44. }
  45. // AutoLink defines how auto-detected links should be processed to produce corresponding HTML elements.
  46. // Reference for kind: https://github.com/russross/blackfriday/blob/master/markdown.go#L69-L76
  47. func (r *MarkdownRenderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {
  48. if kind != blackfriday.LINK_TYPE_NORMAL {
  49. r.Renderer.AutoLink(out, link, kind)
  50. return
  51. }
  52. // Since this method could only possibly serve one link at a time,
  53. // we do not need to find all.
  54. if bytes.HasPrefix(link, []byte(setting.AppURL)) {
  55. m := CommitPattern.Find(link)
  56. if m != nil {
  57. m = bytes.TrimSpace(m)
  58. i := strings.Index(string(m), "commit/")
  59. j := strings.Index(string(m), "#")
  60. if j == -1 {
  61. j = len(m)
  62. }
  63. out.WriteString(fmt.Sprintf(` <code><a href="%s">%s</a></code>`, m, tool.ShortSHA1(string(m[i+7:j]))))
  64. return
  65. }
  66. m = IssueFullPattern.Find(link)
  67. if m != nil {
  68. m = bytes.TrimSpace(m)
  69. i := strings.Index(string(m), "issues/")
  70. j := strings.Index(string(m), "#")
  71. if j == -1 {
  72. j = len(m)
  73. }
  74. index := string(m[i+7 : j])
  75. fullRepoURL := setting.AppURL + strings.TrimPrefix(r.urlPrefix, "/")
  76. var link string
  77. if strings.HasPrefix(string(m), fullRepoURL) {
  78. // Use a short issue reference if the URL refers to this repository
  79. link = fmt.Sprintf(`<a href="%s">#%s</a>`, m, index)
  80. } else {
  81. // Use a cross-repository issue reference if the URL refers to a different repository
  82. repo := string(m[len(setting.AppURL) : i-1])
  83. link = fmt.Sprintf(`<a href="%s">%s#%s</a>`, m, repo, index)
  84. }
  85. out.WriteString(link)
  86. return
  87. }
  88. }
  89. r.Renderer.AutoLink(out, link, kind)
  90. }
  91. // ListItem defines how list items should be processed to produce corresponding HTML elements.
  92. func (options *MarkdownRenderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
  93. // Detect procedures to draw checkboxes.
  94. switch {
  95. case bytes.HasPrefix(text, []byte("[ ] ")):
  96. text = append([]byte(`<input type="checkbox" disabled="" />`), text[3:]...)
  97. case bytes.HasPrefix(text, []byte("[x] ")):
  98. text = append([]byte(`<input type="checkbox" disabled="" checked="" />`), text[3:]...)
  99. }
  100. options.Renderer.ListItem(out, text, flags)
  101. }
  102. // RawMarkdown renders content in Markdown syntax to HTML without handling special links.
  103. func RawMarkdown(body []byte, urlPrefix string) []byte {
  104. htmlFlags := 0
  105. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  106. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  107. if setting.Smartypants.Enabled {
  108. htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
  109. if setting.Smartypants.Fractions {
  110. htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
  111. }
  112. if setting.Smartypants.Dashes {
  113. htmlFlags |= blackfriday.HTML_SMARTYPANTS_DASHES
  114. }
  115. if setting.Smartypants.LatexDashes {
  116. htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
  117. }
  118. if setting.Smartypants.AngledQuotes {
  119. htmlFlags |= blackfriday.HTML_SMARTYPANTS_ANGLED_QUOTES
  120. }
  121. }
  122. renderer := &MarkdownRenderer{
  123. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  124. urlPrefix: urlPrefix,
  125. }
  126. // set up the parser
  127. extensions := 0
  128. extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
  129. extensions |= blackfriday.EXTENSION_TABLES
  130. extensions |= blackfriday.EXTENSION_FENCED_CODE
  131. extensions |= blackfriday.EXTENSION_AUTOLINK
  132. extensions |= blackfriday.EXTENSION_STRIKETHROUGH
  133. extensions |= blackfriday.EXTENSION_SPACE_HEADERS
  134. extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
  135. if setting.Markdown.EnableHardLineBreak {
  136. extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
  137. }
  138. body = blackfriday.Markdown(body, renderer, extensions)
  139. return body
  140. }
  141. // Markdown takes a string or []byte and renders to HTML in Markdown syntax with special links.
  142. func Markdown(input interface{}, urlPrefix string, metas map[string]string) []byte {
  143. return Render(MARKDOWN, input, urlPrefix, metas)
  144. }