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