2014-04-10 13:20:58 -05:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2018-10-30 17:26:28 -05:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2014-04-10 13:20:58 -05: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 13:20:58 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-10-30 20:06:25 -05:00
|
|
|
"io"
|
2014-04-10 13:20:58 -05:00
|
|
|
"strings"
|
|
|
|
|
2017-04-21 02:01:08 -05: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 15:38:38 -05:00
|
|
|
|
2019-10-30 20:06:25 -05:00
|
|
|
"github.com/russross/blackfriday/v2"
|
2014-04-10 13:20:58 -05:00
|
|
|
)
|
|
|
|
|
2016-02-20 17:10:05 -05:00
|
|
|
// Renderer is a extended version of underlying render object.
|
|
|
|
type Renderer struct {
|
2014-10-04 16:15:22 -05:00
|
|
|
blackfriday.Renderer
|
2017-09-21 00:20:14 -05:00
|
|
|
URLPrefix string
|
|
|
|
IsWiki bool
|
2014-04-10 13:20:58 -05:00
|
|
|
}
|
|
|
|
|
2018-02-27 02:09:18 -05:00
|
|
|
var byteMailto = []byte("mailto:")
|
|
|
|
|
2019-10-30 20:06:25 -05:00
|
|
|
var htmlEscaper = [256][]byte{
|
|
|
|
'&': []byte("&"),
|
|
|
|
'<': []byte("<"),
|
|
|
|
'>': []byte(">"),
|
|
|
|
'"': []byte("""),
|
2016-01-08 21:59:04 -05:00
|
|
|
}
|
|
|
|
|
2019-10-30 20:06:25 -05:00
|
|
|
func escapeHTML(w io.Writer, s []byte) {
|
|
|
|
var start, end int
|
|
|
|
for end < len(s) {
|
|
|
|
escSeq := htmlEscaper[s[end]]
|
|
|
|
if escSeq != nil {
|
|
|
|
_, _ = w.Write(s[start:end])
|
|
|
|
_, _ = w.Write(escSeq)
|
|
|
|
start = end + 1
|
|
|
|
}
|
|
|
|
end++
|
2017-02-13 20:13:59 -05:00
|
|
|
}
|
2019-10-30 20:06:25 -05:00
|
|
|
if start < len(s) && end <= len(s) {
|
|
|
|
_, _ = w.Write(s[start:end])
|
2014-04-10 13:20:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 20:06:25 -05:00
|
|
|
// RenderNode is a default renderer of a single node of a syntax tree. For
|
|
|
|
// block nodes it will be called twice: first time with entering=true, second
|
|
|
|
// time with entering=false, so that it could know when it's working on an open
|
|
|
|
// tag and when on close. It writes the result to w.
|
|
|
|
//
|
|
|
|
// The return value is a way to tell the calling walker to adjust its walk
|
|
|
|
// pattern: e.g. it can terminate the traversal by returning Terminate. Or it
|
|
|
|
// can ask the walker to skip a subtree of this node by returning SkipChildren.
|
|
|
|
// The typical behavior is to return GoToNext, which asks for the usual
|
|
|
|
// traversal to the next node.
|
|
|
|
func (r *Renderer) RenderNode(w io.Writer, node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
|
|
|
|
switch node.Type {
|
|
|
|
case blackfriday.Image:
|
|
|
|
prefix := r.URLPrefix
|
|
|
|
if r.IsWiki {
|
|
|
|
prefix = util.URLJoin(prefix, "wiki", "raw")
|
2017-04-23 23:18:36 -05:00
|
|
|
}
|
2019-10-30 20:06:25 -05:00
|
|
|
prefix = strings.Replace(prefix, "/src/", "/media/", 1)
|
|
|
|
link := node.LinkData.Destination
|
|
|
|
if len(link) > 0 && !markup.IsLink(link) {
|
|
|
|
lnk := string(link)
|
|
|
|
lnk = util.URLJoin(prefix, lnk)
|
|
|
|
lnk = strings.Replace(lnk, " ", "+", -1)
|
|
|
|
link = []byte(lnk)
|
|
|
|
}
|
|
|
|
node.LinkData.Destination = link
|
|
|
|
// Render link around image only if parent is not link already
|
|
|
|
if node.Parent != nil && node.Parent.Type != blackfriday.Link {
|
|
|
|
if entering {
|
|
|
|
_, _ = w.Write([]byte(`<a href="`))
|
|
|
|
escapeHTML(w, link)
|
|
|
|
_, _ = w.Write([]byte(`">`))
|
|
|
|
return r.Renderer.RenderNode(w, node, entering)
|
|
|
|
}
|
|
|
|
s := r.Renderer.RenderNode(w, node, entering)
|
|
|
|
_, _ = w.Write([]byte(`</a>`))
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return r.Renderer.RenderNode(w, node, entering)
|
|
|
|
case blackfriday.Link:
|
|
|
|
// special case: this is not a link, a hash link or a mailto:, so it's a
|
|
|
|
// relative URL
|
|
|
|
link := node.LinkData.Destination
|
|
|
|
if len(link) > 0 && !markup.IsLink(link) &&
|
|
|
|
link[0] != '#' && !bytes.HasPrefix(link, byteMailto) &&
|
|
|
|
node.LinkData.Footnote == nil {
|
|
|
|
lnk := string(link)
|
|
|
|
if r.IsWiki {
|
|
|
|
lnk = util.URLJoin("wiki", lnk)
|
|
|
|
}
|
|
|
|
link = []byte(util.URLJoin(r.URLPrefix, lnk))
|
|
|
|
}
|
|
|
|
node.LinkData.Destination = link
|
|
|
|
return r.Renderer.RenderNode(w, node, entering)
|
|
|
|
case blackfriday.Text:
|
|
|
|
isListItem := false
|
|
|
|
for n := node.Parent; n != nil; n = n.Parent {
|
|
|
|
if n.Type == blackfriday.Item {
|
|
|
|
isListItem = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isListItem {
|
|
|
|
text := node.Literal
|
|
|
|
switch {
|
|
|
|
case bytes.HasPrefix(text, []byte("[ ] ")):
|
|
|
|
_, _ = w.Write([]byte(`<span class="ui fitted disabled checkbox"><input type="checkbox" disabled="disabled" /><label /></span>`))
|
|
|
|
text = text[3:]
|
|
|
|
case bytes.HasPrefix(text, []byte("[x] ")):
|
|
|
|
_, _ = w.Write([]byte(`<span class="ui checked fitted disabled checkbox"><input type="checkbox" checked="" disabled="disabled" /><label /></span>`))
|
|
|
|
text = text[3:]
|
|
|
|
}
|
|
|
|
node.Literal = text
|
2017-04-23 23:18:36 -05:00
|
|
|
}
|
2016-01-13 07:25:52 -05:00
|
|
|
}
|
2019-10-30 20:06:25 -05:00
|
|
|
return r.Renderer.RenderNode(w, node, entering)
|
2014-10-14 22:44:34 -05:00
|
|
|
}
|
|
|
|
|
2018-02-27 02:09:18 -05:00
|
|
|
const (
|
|
|
|
blackfridayExtensions = 0 |
|
2019-10-30 20:06:25 -05:00
|
|
|
blackfriday.NoIntraEmphasis |
|
|
|
|
blackfriday.Tables |
|
|
|
|
blackfriday.FencedCode |
|
|
|
|
blackfriday.Strikethrough |
|
|
|
|
blackfriday.NoEmptyLineBeforeBlock |
|
|
|
|
blackfriday.DefinitionLists |
|
|
|
|
blackfriday.Footnotes |
|
|
|
|
blackfriday.HeadingIDs |
|
|
|
|
blackfriday.AutoHeadingIDs
|
2018-02-27 02:09:18 -05:00
|
|
|
blackfridayHTMLFlags = 0 |
|
2019-10-30 20:06:25 -05:00
|
|
|
blackfriday.Smartypants
|
2018-02-27 02:09:18 -05:00
|
|
|
)
|
|
|
|
|
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{
|
2019-10-30 20:06:25 -05:00
|
|
|
Renderer: blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
|
|
|
|
Flags: blackfridayHTMLFlags,
|
|
|
|
}),
|
2017-09-21 00:20:14 -05:00
|
|
|
URLPrefix: urlPrefix,
|
|
|
|
IsWiki: wikiMarkdown,
|
2014-04-10 13:20:58 -05:00
|
|
|
}
|
|
|
|
|
2018-02-27 02:09:18 -05:00
|
|
|
exts := blackfridayExtensions
|
2015-09-01 07:32:02 -05:00
|
|
|
if setting.Markdown.EnableHardLineBreak {
|
2019-10-30 20:06:25 -05:00
|
|
|
exts |= blackfriday.HardLineBreak
|
2015-09-01 07:32:02 -05:00
|
|
|
}
|
|
|
|
|
2019-11-12 21:27:11 -05:00
|
|
|
// Need to normalize EOL to UNIX LF to have consistent results in rendering
|
|
|
|
body = blackfriday.Run(util.NormalizeEOL(body), blackfriday.WithRenderer(renderer), blackfriday.WithExtensions(exts))
|
2019-07-18 15:23:27 -05:00
|
|
|
return markup.SanitizeBytes(body)
|
2014-05-05 12:08:01 -05:00
|
|
|
}
|
|
|
|
|
2017-04-21 02:01:08 -05: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 12:17:57 -05: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 02:01:08 -05:00
|
|
|
}
|