markdown.go 4.9 KB

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