markdown.go 5.2 KB

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