2014-04-10 18:20:58 +00:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2018-10-30 22:26:28 +00:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2014-04-10 18:20:58 +00:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-02-20 22:10:05 +00:00
|
|
|
package markdown
|
2014-04-10 18:20:58 +00:00
|
|
|
|
|
|
|
import (
|
2021-03-15 23:20:05 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-08-04 19:56:37 +00:00
|
|
|
"strings"
|
2019-12-31 01:53:28 +00:00
|
|
|
"sync"
|
2014-04-10 18:20:58 +00:00
|
|
|
|
2019-12-31 01:53:28 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2017-04-21 07:01:08 +00:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2019-12-31 01:53:28 +00:00
|
|
|
"code.gitea.io/gitea/modules/markup/common"
|
2016-11-10 16:24:48 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-12-31 01:53:28 +00:00
|
|
|
giteautil "code.gitea.io/gitea/modules/util"
|
|
|
|
|
2020-06-30 21:34:03 +00:00
|
|
|
chromahtml "github.com/alecthomas/chroma/formatters/html"
|
2019-12-31 01:53:28 +00:00
|
|
|
"github.com/yuin/goldmark"
|
2021-03-15 23:20:05 +00:00
|
|
|
highlighting "github.com/yuin/goldmark-highlighting"
|
2020-04-09 10:54:50 +00:00
|
|
|
meta "github.com/yuin/goldmark-meta"
|
2019-12-31 01:53:28 +00:00
|
|
|
"github.com/yuin/goldmark/extension"
|
|
|
|
"github.com/yuin/goldmark/parser"
|
|
|
|
"github.com/yuin/goldmark/renderer"
|
|
|
|
"github.com/yuin/goldmark/renderer/html"
|
|
|
|
"github.com/yuin/goldmark/util"
|
2014-04-10 18:20:58 +00:00
|
|
|
)
|
|
|
|
|
2019-12-31 01:53:28 +00:00
|
|
|
var converter goldmark.Markdown
|
|
|
|
var once = sync.Once{}
|
2014-04-10 18:20:58 +00:00
|
|
|
|
2019-12-31 01:53:28 +00:00
|
|
|
var urlPrefixKey = parser.NewContextKey()
|
|
|
|
var isWikiKey = parser.NewContextKey()
|
2020-05-24 08:14:26 +00:00
|
|
|
var renderMetasKey = parser.NewContextKey()
|
2018-02-27 07:09:18 +00:00
|
|
|
|
2021-03-15 23:20:05 +00:00
|
|
|
type closesWithError interface {
|
|
|
|
io.WriteCloser
|
|
|
|
CloseWithError(err error) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type limitWriter struct {
|
|
|
|
w closesWithError
|
|
|
|
sum int64
|
|
|
|
limit int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements the standard Write interface:
|
|
|
|
func (l *limitWriter) Write(data []byte) (int, error) {
|
|
|
|
leftToWrite := l.limit - l.sum
|
|
|
|
if leftToWrite < int64(len(data)) {
|
|
|
|
n, err := l.w.Write(data[:leftToWrite])
|
|
|
|
l.sum += int64(n)
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
_ = l.w.Close()
|
|
|
|
return n, fmt.Errorf("Rendered content too large - truncating render")
|
|
|
|
}
|
|
|
|
n, err := l.w.Write(data)
|
|
|
|
l.sum += int64(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the writer
|
|
|
|
func (l *limitWriter) Close() error {
|
|
|
|
return l.w.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseWithError closes the writer
|
|
|
|
func (l *limitWriter) CloseWithError(err error) error {
|
|
|
|
return l.w.CloseWithError(err)
|
|
|
|
}
|
|
|
|
|
2019-12-31 01:53:28 +00:00
|
|
|
// NewGiteaParseContext creates a parser.Context with the gitea context set
|
2020-05-24 08:14:26 +00:00
|
|
|
func NewGiteaParseContext(urlPrefix string, metas map[string]string, isWiki bool) parser.Context {
|
2019-12-31 01:53:28 +00:00
|
|
|
pc := parser.NewContext(parser.WithIDs(newPrefixedIDs()))
|
|
|
|
pc.Set(urlPrefixKey, urlPrefix)
|
|
|
|
pc.Set(isWikiKey, isWiki)
|
2020-05-24 08:14:26 +00:00
|
|
|
pc.Set(renderMetasKey, metas)
|
2019-12-31 01:53:28 +00:00
|
|
|
return pc
|
2016-01-09 02:59:04 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 23:20:05 +00:00
|
|
|
// actualRender renders Markdown to HTML without handling special links.
|
|
|
|
func actualRender(body []byte, urlPrefix string, metas map[string]string, wikiMarkdown bool) []byte {
|
2019-12-31 01:53:28 +00:00
|
|
|
once.Do(func() {
|
|
|
|
converter = goldmark.New(
|
|
|
|
goldmark.WithExtensions(extension.Table,
|
|
|
|
extension.Strikethrough,
|
|
|
|
extension.TaskList,
|
|
|
|
extension.DefinitionList,
|
|
|
|
common.FootnoteExtension,
|
2020-06-30 21:34:03 +00:00
|
|
|
highlighting.NewHighlighting(
|
|
|
|
highlighting.WithFormatOptions(
|
|
|
|
chromahtml.WithClasses(true),
|
|
|
|
chromahtml.PreventSurroundingPre(true),
|
|
|
|
),
|
|
|
|
highlighting.WithWrapperRenderer(func(w util.BufWriter, c highlighting.CodeBlockContext, entering bool) {
|
|
|
|
if entering {
|
2020-08-04 19:56:37 +00:00
|
|
|
language, _ := c.Language()
|
|
|
|
if language == nil {
|
|
|
|
language = []byte("text")
|
|
|
|
}
|
|
|
|
|
|
|
|
languageStr := string(language)
|
|
|
|
|
|
|
|
preClasses := []string{}
|
|
|
|
if languageStr == "mermaid" {
|
|
|
|
preClasses = append(preClasses, "is-loading")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(preClasses) > 0 {
|
|
|
|
_, err := w.WriteString(`<pre class="` + strings.Join(preClasses, " ") + `">`)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, err := w.WriteString(`<pre>`)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 21:34:03 +00:00
|
|
|
// include language-x class as part of commonmark spec
|
2020-08-04 19:56:37 +00:00
|
|
|
_, err := w.WriteString(`<code class="chroma language-` + string(language) + `">`)
|
2020-06-30 21:34:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, err := w.WriteString("</code></pre>")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
),
|
2020-04-24 13:22:36 +00:00
|
|
|
meta.Meta,
|
2019-12-31 01:53:28 +00:00
|
|
|
),
|
|
|
|
goldmark.WithParserOptions(
|
|
|
|
parser.WithAttribute(),
|
|
|
|
parser.WithAutoHeadingID(),
|
|
|
|
parser.WithASTTransformers(
|
2020-04-24 13:22:36 +00:00
|
|
|
util.Prioritized(&ASTTransformer{}, 10000),
|
2019-12-31 01:53:28 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
goldmark.WithRendererOptions(
|
|
|
|
html.WithUnsafe(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Override the original Tasklist renderer!
|
|
|
|
converter.Renderer().AddOptions(
|
|
|
|
renderer.WithNodeRenderers(
|
2020-04-24 13:22:36 +00:00
|
|
|
util.Prioritized(NewHTMLRenderer(), 10),
|
2019-12-31 01:53:28 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
})
|
2014-04-10 18:20:58 +00:00
|
|
|
|
2021-03-15 23:20:05 +00:00
|
|
|
rd, wr := io.Pipe()
|
|
|
|
defer func() {
|
|
|
|
_ = rd.Close()
|
|
|
|
_ = wr.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
lw := &limitWriter{
|
|
|
|
w: wr,
|
|
|
|
limit: setting.UI.MaxDisplayFileSize * 3,
|
2015-09-01 12:32:02 +00:00
|
|
|
}
|
2021-03-15 23:20:05 +00:00
|
|
|
|
|
|
|
// FIXME: should we include a timeout that closes the pipe to abort the parser and sanitizer if it takes too long?
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warn("Unable to render markdown due to panic in goldmark: %v", err)
|
|
|
|
if log.IsDebug() {
|
|
|
|
log.Debug("Panic in markdown: %v\n%s", err, string(log.Stack(2)))
|
|
|
|
}
|
|
|
|
_ = lw.CloseWithError(fmt.Errorf("%v", err))
|
|
|
|
}()
|
|
|
|
|
|
|
|
pc := NewGiteaParseContext(urlPrefix, metas, wikiMarkdown)
|
|
|
|
if err := converter.Convert(giteautil.NormalizeEOL(body), lw, parser.WithContext(pc)); err != nil {
|
|
|
|
log.Error("Unable to render: %v", err)
|
|
|
|
_ = lw.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = lw.Close()
|
|
|
|
}()
|
|
|
|
return markup.SanitizeReader(rd).Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func render(body []byte, urlPrefix string, metas map[string]string, wikiMarkdown bool) (ret []byte) {
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warn("Unable to render markdown due to panic in goldmark - will return sanitized raw bytes")
|
|
|
|
if log.IsDebug() {
|
|
|
|
log.Debug("Panic in markdown: %v\n%s", err, string(log.Stack(2)))
|
|
|
|
}
|
|
|
|
ret = markup.SanitizeBytes(body)
|
|
|
|
}()
|
|
|
|
return actualRender(body, urlPrefix, metas, wikiMarkdown)
|
2014-05-05 17:08:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 07:01:08 +00:00
|
|
|
var (
|
|
|
|
// MarkupName describes markup's name
|
|
|
|
MarkupName = "markdown"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
markup.RegisterParser(Parser{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser implements markup.Parser
|
2019-12-31 01:53:28 +00:00
|
|
|
type Parser struct{}
|
2017-04-21 07:01:08 +00:00
|
|
|
|
|
|
|
// 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 {
|
2020-05-24 08:14:26 +00:00
|
|
|
return render(rawBytes, urlPrefix, metas, isWiki)
|
2017-09-16 17:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-05-24 08:14:26 +00:00
|
|
|
// RenderRaw renders Markdown to HTML without handling special links.
|
|
|
|
func RenderRaw(body []byte, urlPrefix string, wikiMarkdown bool) []byte {
|
|
|
|
return render(body, urlPrefix, map[string]string{}, wikiMarkdown)
|
|
|
|
}
|
|
|
|
|
2017-09-16 17:17:57 +00:00
|
|
|
// 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 07:01:08 +00:00
|
|
|
}
|