You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

markdown.go 6.2 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. "strings"
  8. "code.gitea.io/gitea/modules/markup"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/util"
  11. "github.com/russross/blackfriday"
  12. )
  13. // Renderer is a extended version of underlying render object.
  14. type Renderer struct {
  15. blackfriday.Renderer
  16. URLPrefix string
  17. IsWiki bool
  18. }
  19. var byteMailto = []byte("mailto:")
  20. // Link defines how formal links should be processed to produce corresponding HTML elements.
  21. func (r *Renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
  22. // special case: this is not a link, a hash link or a mailto:, so it's a
  23. // relative URL
  24. if len(link) > 0 && !markup.IsLink(link) &&
  25. link[0] != '#' && !bytes.HasPrefix(link, byteMailto) {
  26. lnk := string(link)
  27. if r.IsWiki {
  28. lnk = util.URLJoin("wiki", lnk)
  29. }
  30. mLink := util.URLJoin(r.URLPrefix, lnk)
  31. link = []byte(mLink)
  32. }
  33. r.Renderer.Link(out, link, title, content)
  34. }
  35. // List renders markdown bullet or digit lists to HTML
  36. func (r *Renderer) List(out *bytes.Buffer, text func() bool, flags int) {
  37. marker := out.Len()
  38. if out.Len() > 0 {
  39. out.WriteByte('\n')
  40. }
  41. if flags&blackfriday.LIST_TYPE_DEFINITION != 0 {
  42. out.WriteString("<dl>")
  43. } else if flags&blackfriday.LIST_TYPE_ORDERED != 0 {
  44. out.WriteString("<ol class='ui list'>")
  45. } else {
  46. out.WriteString("<ul class='ui list'>")
  47. }
  48. if !text() {
  49. out.Truncate(marker)
  50. return
  51. }
  52. if flags&blackfriday.LIST_TYPE_DEFINITION != 0 {
  53. out.WriteString("</dl>\n")
  54. } else if flags&blackfriday.LIST_TYPE_ORDERED != 0 {
  55. out.WriteString("</ol>\n")
  56. } else {
  57. out.WriteString("</ul>\n")
  58. }
  59. }
  60. // ListItem defines how list items should be processed to produce corresponding HTML elements.
  61. func (r *Renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
  62. // Detect procedures to draw checkboxes.
  63. prefix := ""
  64. if bytes.HasPrefix(text, []byte("<p>")) {
  65. prefix = "<p>"
  66. }
  67. switch {
  68. case bytes.HasPrefix(text, []byte(prefix+"[ ] ")):
  69. text = append([]byte(`<span class="ui fitted disabled checkbox"><input type="checkbox" disabled="disabled" /><label /></span>`), text[3+len(prefix):]...)
  70. if prefix != "" {
  71. text = bytes.Replace(text, []byte(prefix), []byte{}, 1)
  72. }
  73. case bytes.HasPrefix(text, []byte(prefix+"[x] ")):
  74. text = append([]byte(`<span class="ui checked fitted disabled checkbox"><input type="checkbox" checked="" disabled="disabled" /><label /></span>`), text[3+len(prefix):]...)
  75. if prefix != "" {
  76. text = bytes.Replace(text, []byte(prefix), []byte{}, 1)
  77. }
  78. }
  79. r.Renderer.ListItem(out, text, flags)
  80. }
  81. // Note: this section is for purpose of increase performance and
  82. // reduce memory allocation at runtime since they are constant literals.
  83. var (
  84. svgSuffix = []byte(".svg")
  85. svgSuffixWithMark = []byte(".svg?")
  86. )
  87. // Image defines how images should be processed to produce corresponding HTML elements.
  88. func (r *Renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
  89. prefix := r.URLPrefix
  90. if r.IsWiki {
  91. prefix = util.URLJoin(prefix, "wiki", "raw")
  92. }
  93. prefix = strings.Replace(prefix, "/src/", "/raw/", 1)
  94. if len(link) > 0 {
  95. if markup.IsLink(link) {
  96. // External link with .svg suffix usually means CI status.
  97. // TODO: define a keyword to allow non-svg images render as external link.
  98. if bytes.HasSuffix(link, svgSuffix) || bytes.Contains(link, svgSuffixWithMark) {
  99. r.Renderer.Image(out, link, title, alt)
  100. return
  101. }
  102. } else {
  103. lnk := string(link)
  104. lnk = util.URLJoin(prefix, lnk)
  105. lnk = strings.Replace(lnk, " ", "+", -1)
  106. link = []byte(lnk)
  107. }
  108. }
  109. out.WriteString(`<a href="`)
  110. out.Write(link)
  111. out.WriteString(`">`)
  112. r.Renderer.Image(out, link, title, alt)
  113. out.WriteString("</a>")
  114. }
  115. const (
  116. blackfridayExtensions = 0 |
  117. blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
  118. blackfriday.EXTENSION_TABLES |
  119. blackfriday.EXTENSION_FENCED_CODE |
  120. blackfriday.EXTENSION_STRIKETHROUGH |
  121. blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
  122. blackfridayHTMLFlags = 0 |
  123. blackfriday.HTML_SKIP_STYLE |
  124. blackfriday.HTML_OMIT_CONTENTS |
  125. blackfriday.HTML_USE_SMARTYPANTS
  126. )
  127. // RenderRaw renders Markdown to HTML without handling special links.
  128. func RenderRaw(body []byte, urlPrefix string, wikiMarkdown bool) []byte {
  129. renderer := &Renderer{
  130. Renderer: blackfriday.HtmlRenderer(blackfridayHTMLFlags, "", ""),
  131. URLPrefix: urlPrefix,
  132. IsWiki: wikiMarkdown,
  133. }
  134. exts := blackfridayExtensions
  135. if setting.Markdown.EnableHardLineBreak {
  136. exts |= blackfriday.EXTENSION_HARD_LINE_BREAK
  137. }
  138. body = blackfriday.Markdown(body, renderer, exts)
  139. return body
  140. }
  141. var (
  142. // MarkupName describes markup's name
  143. MarkupName = "markdown"
  144. )
  145. func init() {
  146. markup.RegisterParser(Parser{})
  147. }
  148. // Parser implements markup.Parser
  149. type Parser struct {
  150. }
  151. // Name implements markup.Parser
  152. func (Parser) Name() string {
  153. return MarkupName
  154. }
  155. // Extensions implements markup.Parser
  156. func (Parser) Extensions() []string {
  157. return setting.Markdown.FileExtensions
  158. }
  159. // Render implements markup.Parser
  160. func (Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
  161. return RenderRaw(rawBytes, urlPrefix, isWiki)
  162. }
  163. // Render renders Markdown to HTML with all specific handling stuff.
  164. func Render(rawBytes []byte, urlPrefix string, metas map[string]string) []byte {
  165. return markup.Render("a.md", rawBytes, urlPrefix, metas)
  166. }
  167. // RenderString renders Markdown to HTML with special links and returns string type.
  168. func RenderString(raw, urlPrefix string, metas map[string]string) string {
  169. return markup.RenderString("a.md", raw, urlPrefix, metas)
  170. }
  171. // RenderWiki renders markdown wiki page to HTML and return HTML string
  172. func RenderWiki(rawBytes []byte, urlPrefix string, metas map[string]string) string {
  173. return markup.RenderWiki("a.md", rawBytes, urlPrefix, metas)
  174. }
  175. // IsMarkdownFile reports whether name looks like a Markdown file
  176. // based on its extension.
  177. func IsMarkdownFile(name string) bool {
  178. return markup.IsMarkupFile(name, MarkupName)
  179. }