本站源代码
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

207 lines
6.2KB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package markdown
  6. import (
  7. "bytes"
  8. "strings"
  9. "code.gitea.io/gitea/modules/markup"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/util"
  12. "github.com/russross/blackfriday"
  13. )
  14. // Renderer is a extended version of underlying render object.
  15. type Renderer struct {
  16. blackfriday.Renderer
  17. URLPrefix string
  18. IsWiki bool
  19. }
  20. var byteMailto = []byte("mailto:")
  21. // Link defines how formal links should be processed to produce corresponding HTML elements.
  22. func (r *Renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
  23. // special case: this is not a link, a hash link or a mailto:, so it's a
  24. // relative URL
  25. if len(link) > 0 && !markup.IsLink(link) &&
  26. link[0] != '#' && !bytes.HasPrefix(link, byteMailto) {
  27. lnk := string(link)
  28. if r.IsWiki {
  29. lnk = util.URLJoin("wiki", lnk)
  30. }
  31. mLink := util.URLJoin(r.URLPrefix, lnk)
  32. link = []byte(mLink)
  33. }
  34. if len(content) > 10 && string(content[0:9]) == "<a href=\"" && bytes.Contains(content[9:], []byte("<img")) {
  35. // Image with link case: markdown `[![]()]()`
  36. // If the content is an image, then we change the original href around it
  37. // which points to itself to a new address "link"
  38. rightQuote := bytes.Index(content[9:], []byte("\""))
  39. content = bytes.Replace(content, content[9:9+rightQuote], link, 1)
  40. out.Write(content)
  41. } else {
  42. r.Renderer.Link(out, link, title, content)
  43. }
  44. }
  45. // List renders markdown bullet or digit lists to HTML
  46. func (r *Renderer) List(out *bytes.Buffer, text func() bool, flags int) {
  47. marker := out.Len()
  48. if out.Len() > 0 {
  49. out.WriteByte('\n')
  50. }
  51. if flags&blackfriday.LIST_TYPE_DEFINITION != 0 {
  52. out.WriteString("<dl>")
  53. } else if flags&blackfriday.LIST_TYPE_ORDERED != 0 {
  54. out.WriteString("<ol class='ui list'>")
  55. } else {
  56. out.WriteString("<ul class='ui list'>")
  57. }
  58. if !text() {
  59. out.Truncate(marker)
  60. return
  61. }
  62. if flags&blackfriday.LIST_TYPE_DEFINITION != 0 {
  63. out.WriteString("</dl>\n")
  64. } else if flags&blackfriday.LIST_TYPE_ORDERED != 0 {
  65. out.WriteString("</ol>\n")
  66. } else {
  67. out.WriteString("</ul>\n")
  68. }
  69. }
  70. // ListItem defines how list items should be processed to produce corresponding HTML elements.
  71. func (r *Renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
  72. // Detect procedures to draw checkboxes.
  73. prefix := ""
  74. if bytes.HasPrefix(text, []byte("<p>")) {
  75. prefix = "<p>"
  76. }
  77. switch {
  78. case bytes.HasPrefix(text, []byte(prefix+"[ ] ")):
  79. text = append([]byte(`<span class="ui fitted disabled checkbox"><input type="checkbox" disabled="disabled" /><label /></span>`), text[3+len(prefix):]...)
  80. if prefix != "" {
  81. text = bytes.Replace(text, []byte(prefix), []byte{}, 1)
  82. }
  83. case bytes.HasPrefix(text, []byte(prefix+"[x] ")):
  84. text = append([]byte(`<span class="ui checked fitted disabled checkbox"><input type="checkbox" checked="" disabled="disabled" /><label /></span>`), text[3+len(prefix):]...)
  85. if prefix != "" {
  86. text = bytes.Replace(text, []byte(prefix), []byte{}, 1)
  87. }
  88. }
  89. r.Renderer.ListItem(out, text, flags)
  90. }
  91. // Image defines how images should be processed to produce corresponding HTML elements.
  92. func (r *Renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
  93. prefix := r.URLPrefix
  94. if r.IsWiki {
  95. prefix = util.URLJoin(prefix, "wiki", "raw")
  96. }
  97. prefix = strings.Replace(prefix, "/src/", "/media/", 1)
  98. if len(link) > 0 && !markup.IsLink(link) {
  99. lnk := string(link)
  100. lnk = util.URLJoin(prefix, lnk)
  101. lnk = strings.Replace(lnk, " ", "+", -1)
  102. link = []byte(lnk)
  103. }
  104. // Put a link around it pointing to itself by default
  105. out.WriteString(`<a href="`)
  106. out.Write(link)
  107. out.WriteString(`">`)
  108. r.Renderer.Image(out, link, title, alt)
  109. out.WriteString("</a>")
  110. }
  111. const (
  112. blackfridayExtensions = 0 |
  113. blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
  114. blackfriday.EXTENSION_TABLES |
  115. blackfriday.EXTENSION_FENCED_CODE |
  116. blackfriday.EXTENSION_STRIKETHROUGH |
  117. blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK |
  118. blackfriday.EXTENSION_DEFINITION_LISTS |
  119. blackfriday.EXTENSION_FOOTNOTES |
  120. blackfriday.EXTENSION_HEADER_IDS |
  121. blackfriday.EXTENSION_AUTO_HEADER_IDS
  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 markup.SanitizeBytes(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. }
上海开阖软件有限公司 沪ICP备12045867号-1