2014-04-10 14:20:58 -04:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2018-10-30 18:26:28 -04:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2014-04-10 14:20:58 -04:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-02-20 17:10:05 -05:00
|
|
|
package markdown
|
2014-04-10 14:20:58 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
|
2017-04-21 03:01:08 -04:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2016-11-10 11:24:48 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2018-02-20 07:50:42 -05:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2017-07-06 16:38:38 -04:00
|
|
|
|
|
|
|
"github.com/russross/blackfriday"
|
2014-04-10 14:20:58 -04:00
|
|
|
)
|
|
|
|
|
2016-02-20 17:10:05 -05:00
|
|
|
// Renderer is a extended version of underlying render object.
|
|
|
|
type Renderer struct {
|
2014-10-04 17:15:22 -04:00
|
|
|
blackfriday.Renderer
|
2017-09-21 01:20:14 -04:00
|
|
|
URLPrefix string
|
|
|
|
IsWiki bool
|
2014-04-10 14:20:58 -04:00
|
|
|
}
|
|
|
|
|
2018-02-27 02:09:18 -05:00
|
|
|
var byteMailto = []byte("mailto:")
|
|
|
|
|
2016-02-20 17:10:05 -05:00
|
|
|
// Link defines how formal links should be processed to produce corresponding HTML elements.
|
|
|
|
func (r *Renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
|
2018-02-27 02:09:18 -05:00
|
|
|
// special case: this is not a link, a hash link or a mailto:, so it's a
|
|
|
|
// relative URL
|
|
|
|
if len(link) > 0 && !markup.IsLink(link) &&
|
|
|
|
link[0] != '#' && !bytes.HasPrefix(link, byteMailto) {
|
|
|
|
lnk := string(link)
|
|
|
|
if r.IsWiki {
|
|
|
|
lnk = util.URLJoin("wiki", lnk)
|
2016-01-08 21:59:04 -05:00
|
|
|
}
|
2018-02-27 02:09:18 -05:00
|
|
|
mLink := util.URLJoin(r.URLPrefix, lnk)
|
|
|
|
link = []byte(mLink)
|
2016-01-08 21:59:04 -05:00
|
|
|
}
|
|
|
|
|
2018-10-30 18:26:28 -04:00
|
|
|
if len(content) > 10 && string(content[0:9]) == "<a href=\"" && bytes.Contains(content[9:], []byte("<img")) {
|
|
|
|
// Image with link case: markdown `[![]()]()`
|
|
|
|
// If the content is an image, then we change the original href around it
|
|
|
|
// which points to itself to a new address "link"
|
|
|
|
rightQuote := bytes.Index(content[9:], []byte("\""))
|
|
|
|
content = bytes.Replace(content, content[9:9+rightQuote], link, 1)
|
|
|
|
out.Write(content)
|
|
|
|
} else {
|
|
|
|
r.Renderer.Link(out, link, title, content)
|
|
|
|
}
|
2016-01-08 21:59:04 -05:00
|
|
|
}
|
|
|
|
|
2017-02-13 20:13:59 -05:00
|
|
|
// List renders markdown bullet or digit lists to HTML
|
|
|
|
func (r *Renderer) List(out *bytes.Buffer, text func() bool, flags int) {
|
|
|
|
marker := out.Len()
|
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteByte('\n')
|
2016-01-08 21:59:04 -05:00
|
|
|
}
|
|
|
|
|
2017-02-13 20:13:59 -05:00
|
|
|
if flags&blackfriday.LIST_TYPE_DEFINITION != 0 {
|
|
|
|
out.WriteString("<dl>")
|
|
|
|
} else if flags&blackfriday.LIST_TYPE_ORDERED != 0 {
|
|
|
|
out.WriteString("<ol class='ui list'>")
|
|
|
|
} else {
|
|
|
|
out.WriteString("<ul class='ui list'>")
|
|
|
|
}
|
|
|
|
if !text() {
|
|
|
|
out.Truncate(marker)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if flags&blackfriday.LIST_TYPE_DEFINITION != 0 {
|
|
|
|
out.WriteString("</dl>\n")
|
|
|
|
} else if flags&blackfriday.LIST_TYPE_ORDERED != 0 {
|
|
|
|
out.WriteString("</ol>\n")
|
|
|
|
} else {
|
|
|
|
out.WriteString("</ul>\n")
|
2014-04-10 14:20:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 17:10:05 -05:00
|
|
|
// ListItem defines how list items should be processed to produce corresponding HTML elements.
|
2016-11-24 20:58:05 -05:00
|
|
|
func (r *Renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
2016-02-20 17:10:05 -05:00
|
|
|
// Detect procedures to draw checkboxes.
|
2017-02-13 20:13:59 -05:00
|
|
|
prefix := ""
|
|
|
|
if bytes.HasPrefix(text, []byte("<p>")) {
|
|
|
|
prefix = "<p>"
|
|
|
|
}
|
2016-01-13 07:25:52 -05:00
|
|
|
switch {
|
2017-02-13 20:13:59 -05:00
|
|
|
case bytes.HasPrefix(text, []byte(prefix+"[ ] ")):
|
2017-05-12 03:52:45 -04:00
|
|
|
text = append([]byte(`<span class="ui fitted disabled checkbox"><input type="checkbox" disabled="disabled" /><label /></span>`), text[3+len(prefix):]...)
|
2017-04-24 00:18:36 -04:00
|
|
|
if prefix != "" {
|
|
|
|
text = bytes.Replace(text, []byte(prefix), []byte{}, 1)
|
|
|
|
}
|
2017-02-13 20:13:59 -05:00
|
|
|
case bytes.HasPrefix(text, []byte(prefix+"[x] ")):
|
2017-05-12 03:52:45 -04:00
|
|
|
text = append([]byte(`<span class="ui checked fitted disabled checkbox"><input type="checkbox" checked="" disabled="disabled" /><label /></span>`), text[3+len(prefix):]...)
|
2017-04-24 00:18:36 -04:00
|
|
|
if prefix != "" {
|
|
|
|
text = bytes.Replace(text, []byte(prefix), []byte{}, 1)
|
|
|
|
}
|
2016-01-13 07:25:52 -05:00
|
|
|
}
|
2016-11-24 20:58:05 -05:00
|
|
|
r.Renderer.ListItem(out, text, flags)
|
2016-01-13 07:25:52 -05:00
|
|
|
}
|
|
|
|
|
2016-02-20 17:10:05 -05:00
|
|
|
// Image defines how images should be processed to produce corresponding HTML elements.
|
|
|
|
func (r *Renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
|
2017-09-21 01:20:14 -04:00
|
|
|
prefix := r.URLPrefix
|
|
|
|
if r.IsWiki {
|
2018-07-05 16:36:45 -04:00
|
|
|
prefix = util.URLJoin(prefix, "wiki", "raw")
|
2017-02-13 20:13:59 -05:00
|
|
|
}
|
2019-02-12 10:09:43 -05:00
|
|
|
prefix = strings.Replace(prefix, "/src/", "/media/", 1)
|
2018-10-30 18:26:28 -04:00
|
|
|
if len(link) > 0 && !markup.IsLink(link) {
|
|
|
|
lnk := string(link)
|
|
|
|
lnk = util.URLJoin(prefix, lnk)
|
|
|
|
lnk = strings.Replace(lnk, " ", "+", -1)
|
|
|
|
link = []byte(lnk)
|
2014-10-14 23:44:34 -04:00
|
|
|
}
|
|
|
|
|
2018-10-30 18:26:28 -04:00
|
|
|
// Put a link around it pointing to itself by default
|
2015-11-06 11:10:27 -05:00
|
|
|
out.WriteString(`<a href="`)
|
|
|
|
out.Write(link)
|
|
|
|
out.WriteString(`">`)
|
2016-01-08 21:59:04 -05:00
|
|
|
r.Renderer.Image(out, link, title, alt)
|
2015-11-06 11:10:27 -05:00
|
|
|
out.WriteString("</a>")
|
2014-10-14 23:44:34 -04:00
|
|
|
}
|
|
|
|
|
2018-02-27 02:09:18 -05:00
|
|
|
const (
|
|
|
|
blackfridayExtensions = 0 |
|
|
|
|
blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
|
|
|
|
blackfriday.EXTENSION_TABLES |
|
|
|
|
blackfriday.EXTENSION_FENCED_CODE |
|
|
|
|
blackfriday.EXTENSION_STRIKETHROUGH |
|
2019-03-21 09:53:06 -04:00
|
|
|
blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK |
|
|
|
|
blackfriday.EXTENSION_DEFINITION_LISTS |
|
|
|
|
blackfriday.EXTENSION_FOOTNOTES |
|
|
|
|
blackfriday.EXTENSION_HEADER_IDS |
|
|
|
|
blackfriday.EXTENSION_AUTO_HEADER_IDS
|
2018-02-27 02:09:18 -05:00
|
|
|
blackfridayHTMLFlags = 0 |
|
|
|
|
blackfriday.HTML_SKIP_STYLE |
|
|
|
|
blackfriday.HTML_OMIT_CONTENTS |
|
|
|
|
blackfriday.HTML_USE_SMARTYPANTS
|
|
|
|
)
|
|
|
|
|
2016-02-20 17:10:05 -05:00
|
|
|
// RenderRaw renders Markdown to HTML without handling special links.
|
2017-02-13 20:13:59 -05:00
|
|
|
func RenderRaw(body []byte, urlPrefix string, wikiMarkdown bool) []byte {
|
2016-02-20 17:10:05 -05:00
|
|
|
renderer := &Renderer{
|
2018-02-27 02:09:18 -05:00
|
|
|
Renderer: blackfriday.HtmlRenderer(blackfridayHTMLFlags, "", ""),
|
2017-09-21 01:20:14 -04:00
|
|
|
URLPrefix: urlPrefix,
|
|
|
|
IsWiki: wikiMarkdown,
|
2014-04-10 14:20:58 -04:00
|
|
|
}
|
|
|
|
|
2018-02-27 02:09:18 -05:00
|
|
|
exts := blackfridayExtensions
|
2015-09-01 08:32:02 -04:00
|
|
|
if setting.Markdown.EnableHardLineBreak {
|
2018-02-27 02:09:18 -05:00
|
|
|
exts |= blackfriday.EXTENSION_HARD_LINE_BREAK
|
2015-09-01 08:32:02 -04:00
|
|
|
}
|
|
|
|
|
2018-02-27 02:09:18 -05:00
|
|
|
body = blackfriday.Markdown(body, renderer, exts)
|
2019-07-18 16:23:27 -04:00
|
|
|
return markup.SanitizeBytes(body)
|
2014-05-05 13:08:01 -04:00
|
|
|
}
|
|
|
|
|
2017-04-21 03:01:08 -04:00
|
|
|
var (
|
|
|
|
// MarkupName describes markup's name
|
|
|
|
MarkupName = "markdown"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
markup.RegisterParser(Parser{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser implements markup.Parser
|
|
|
|
type Parser struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements markup.Parser
|
|
|
|
func (Parser) Name() string {
|
|
|
|
return MarkupName
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extensions implements markup.Parser
|
|
|
|
func (Parser) Extensions() []string {
|
|
|
|
return setting.Markdown.FileExtensions
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render implements markup.Parser
|
|
|
|
func (Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
|
2017-09-16 13:17:57 -04:00
|
|
|
return RenderRaw(rawBytes, urlPrefix, isWiki)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render renders Markdown to HTML with all specific handling stuff.
|
|
|
|
func Render(rawBytes []byte, urlPrefix string, metas map[string]string) []byte {
|
|
|
|
return markup.Render("a.md", rawBytes, urlPrefix, metas)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenderString renders Markdown to HTML with special links and returns string type.
|
|
|
|
func RenderString(raw, urlPrefix string, metas map[string]string) string {
|
|
|
|
return markup.RenderString("a.md", raw, urlPrefix, metas)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenderWiki renders markdown wiki page to HTML and return HTML string
|
|
|
|
func RenderWiki(rawBytes []byte, urlPrefix string, metas map[string]string) string {
|
|
|
|
return markup.RenderWiki("a.md", rawBytes, urlPrefix, metas)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsMarkdownFile reports whether name looks like a Markdown file
|
|
|
|
// based on its extension.
|
|
|
|
func IsMarkdownFile(name string) bool {
|
|
|
|
return markup.IsMarkupFile(name, MarkupName)
|
2017-04-21 03:01:08 -04:00
|
|
|
}
|