2014-04-10 23:50:58 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-02-21 03:40:05 +05:30
|
|
|
package markdown
|
2014-04-10 23:50:58 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
|
2017-04-21 12:31:08 +05:30
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-07-07 02:08:38 +05:30
|
|
|
|
|
|
|
"github.com/russross/blackfriday"
|
2014-04-10 23:50:58 +05:30
|
|
|
)
|
|
|
|
|
2016-02-21 03:40:05 +05:30
|
|
|
// Renderer is a extended version of underlying render object.
|
|
|
|
type Renderer struct {
|
2014-10-05 02:45:22 +05:30
|
|
|
blackfriday.Renderer
|
2017-02-14 06:43:59 +05:30
|
|
|
urlPrefix string
|
|
|
|
isWikiMarkdown bool
|
2014-04-10 23:50:58 +05:30
|
|
|
}
|
|
|
|
|
2016-02-21 03:40:05 +05:30
|
|
|
// 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) {
|
2017-09-16 22:47:57 +05:30
|
|
|
if len(link) > 0 && !markup.IsLink(link) {
|
2016-02-21 03:40:05 +05:30
|
|
|
if link[0] != '#' {
|
2017-02-24 20:29:56 +05:30
|
|
|
lnk := string(link)
|
2017-02-14 06:43:59 +05:30
|
|
|
if r.isWikiMarkdown {
|
2017-09-16 22:47:57 +05:30
|
|
|
lnk = markup.URLJoin("wiki", lnk)
|
2017-02-14 06:43:59 +05:30
|
|
|
}
|
2017-09-16 22:47:57 +05:30
|
|
|
mLink := markup.URLJoin(r.urlPrefix, lnk)
|
2017-02-14 06:43:59 +05:30
|
|
|
link = []byte(mLink)
|
2016-01-09 08:29:04 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Renderer.Link(out, link, title, content)
|
|
|
|
}
|
|
|
|
|
2017-02-14 06:43:59 +05:30
|
|
|
// 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-09 08:29:04 +05:30
|
|
|
}
|
|
|
|
|
2017-02-14 06:43:59 +05:30
|
|
|
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 23:50:58 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-21 03:40:05 +05:30
|
|
|
// ListItem defines how list items should be processed to produce corresponding HTML elements.
|
2016-11-25 07:28:05 +05:30
|
|
|
func (r *Renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
2016-02-21 03:40:05 +05:30
|
|
|
// Detect procedures to draw checkboxes.
|
2017-02-14 06:43:59 +05:30
|
|
|
prefix := ""
|
|
|
|
if bytes.HasPrefix(text, []byte("<p>")) {
|
|
|
|
prefix = "<p>"
|
|
|
|
}
|
2016-01-13 17:55:52 +05:30
|
|
|
switch {
|
2017-02-14 06:43:59 +05:30
|
|
|
case bytes.HasPrefix(text, []byte(prefix+"[ ] ")):
|
2017-05-12 13:22:45 +05:30
|
|
|
text = append([]byte(`<span class="ui fitted disabled checkbox"><input type="checkbox" disabled="disabled" /><label /></span>`), text[3+len(prefix):]...)
|
2017-04-24 09:48:36 +05:30
|
|
|
if prefix != "" {
|
|
|
|
text = bytes.Replace(text, []byte(prefix), []byte{}, 1)
|
|
|
|
}
|
2017-02-14 06:43:59 +05:30
|
|
|
case bytes.HasPrefix(text, []byte(prefix+"[x] ")):
|
2017-05-12 13:22:45 +05:30
|
|
|
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 09:48:36 +05:30
|
|
|
if prefix != "" {
|
|
|
|
text = bytes.Replace(text, []byte(prefix), []byte{}, 1)
|
|
|
|
}
|
2016-01-13 17:55:52 +05:30
|
|
|
}
|
2016-11-25 07:28:05 +05:30
|
|
|
r.Renderer.ListItem(out, text, flags)
|
2016-01-13 17:55:52 +05:30
|
|
|
}
|
|
|
|
|
2016-02-21 03:40:05 +05:30
|
|
|
// Note: this section is for purpose of increase performance and
|
|
|
|
// reduce memory allocation at runtime since they are constant literals.
|
2015-11-20 16:07:51 +05:30
|
|
|
var (
|
|
|
|
svgSuffix = []byte(".svg")
|
|
|
|
svgSuffixWithMark = []byte(".svg?")
|
|
|
|
)
|
|
|
|
|
2016-02-21 03:40:05 +05:30
|
|
|
// 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-02-14 06:43:59 +05:30
|
|
|
prefix := r.urlPrefix
|
|
|
|
if r.isWikiMarkdown {
|
2017-09-16 22:47:57 +05:30
|
|
|
prefix = markup.URLJoin(prefix, "wiki", "src")
|
2017-02-14 06:43:59 +05:30
|
|
|
}
|
|
|
|
prefix = strings.Replace(prefix, "/src/", "/raw/", 1)
|
2015-11-20 16:07:51 +05:30
|
|
|
if len(link) > 0 {
|
2017-09-16 22:47:57 +05:30
|
|
|
if markup.IsLink(link) {
|
2015-11-20 16:07:51 +05:30
|
|
|
// External link with .svg suffix usually means CI status.
|
2016-02-21 03:40:05 +05:30
|
|
|
// TODO: define a keyword to allow non-svg images render as external link.
|
2015-11-20 16:07:51 +05:30
|
|
|
if bytes.HasSuffix(link, svgSuffix) || bytes.Contains(link, svgSuffixWithMark) {
|
2016-01-09 08:29:04 +05:30
|
|
|
r.Renderer.Image(out, link, title, alt)
|
2015-11-20 16:07:51 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2017-02-24 20:29:56 +05:30
|
|
|
lnk := string(link)
|
2017-09-16 22:47:57 +05:30
|
|
|
lnk = markup.URLJoin(prefix, lnk)
|
2017-02-24 20:29:56 +05:30
|
|
|
lnk = strings.Replace(lnk, " ", "+", -1)
|
|
|
|
link = []byte(lnk)
|
2015-11-06 21:40:27 +05:30
|
|
|
}
|
2014-10-15 09:14:34 +05:30
|
|
|
}
|
|
|
|
|
2015-11-06 21:40:27 +05:30
|
|
|
out.WriteString(`<a href="`)
|
|
|
|
out.Write(link)
|
|
|
|
out.WriteString(`">`)
|
2016-01-09 08:29:04 +05:30
|
|
|
r.Renderer.Image(out, link, title, alt)
|
2015-11-06 21:40:27 +05:30
|
|
|
out.WriteString("</a>")
|
2014-10-15 09:14:34 +05:30
|
|
|
}
|
|
|
|
|
2016-02-21 03:40:05 +05:30
|
|
|
// RenderRaw renders Markdown to HTML without handling special links.
|
2017-02-14 06:43:59 +05:30
|
|
|
func RenderRaw(body []byte, urlPrefix string, wikiMarkdown bool) []byte {
|
2014-04-10 23:50:58 +05:30
|
|
|
htmlFlags := 0
|
2014-10-05 02:45:22 +05:30
|
|
|
htmlFlags |= blackfriday.HTML_SKIP_STYLE
|
|
|
|
htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
|
2016-02-21 03:40:05 +05:30
|
|
|
renderer := &Renderer{
|
2017-02-14 06:43:59 +05:30
|
|
|
Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
|
|
|
|
urlPrefix: urlPrefix,
|
|
|
|
isWikiMarkdown: wikiMarkdown,
|
2014-04-10 23:50:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// set up the parser
|
|
|
|
extensions := 0
|
2014-10-05 02:45:22 +05:30
|
|
|
extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
|
|
|
|
extensions |= blackfriday.EXTENSION_TABLES
|
|
|
|
extensions |= blackfriday.EXTENSION_FENCED_CODE
|
|
|
|
extensions |= blackfriday.EXTENSION_STRIKETHROUGH
|
|
|
|
extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
|
|
|
|
|
2015-09-01 18:02:02 +05:30
|
|
|
if setting.Markdown.EnableHardLineBreak {
|
|
|
|
extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
|
|
|
|
}
|
|
|
|
|
2014-10-05 02:45:22 +05:30
|
|
|
body = blackfriday.Markdown(body, renderer, extensions)
|
2014-05-05 22:38:01 +05:30
|
|
|
return body
|
|
|
|
}
|
|
|
|
|
2017-04-21 12:31:08 +05:30
|
|
|
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 22:47:57 +05:30
|
|
|
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 12:31:08 +05:30
|
|
|
}
|