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