2014-04-10 12:20:58 -06:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2018-10-30 16:26:28 -06:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2014-04-10 12:20:58 -06:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-02-20 15:10:05 -07:00
|
|
|
package markdown
|
2014-04-10 12:20:58 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-12-30 18:53:28 -07:00
|
|
|
"sync"
|
2014-04-10 12:20:58 -06:00
|
|
|
|
2019-12-30 18:53:28 -07:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2017-04-21 01:01:08 -06:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2019-12-30 18:53:28 -07:00
|
|
|
"code.gitea.io/gitea/modules/markup/common"
|
2016-11-10 09:24:48 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-12-30 18:53:28 -07:00
|
|
|
giteautil "code.gitea.io/gitea/modules/util"
|
|
|
|
|
|
|
|
"github.com/yuin/goldmark"
|
2020-04-09 04:54:50 -06:00
|
|
|
meta "github.com/yuin/goldmark-meta"
|
2019-12-30 18:53:28 -07: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 12:20:58 -06:00
|
|
|
)
|
|
|
|
|
2019-12-30 18:53:28 -07:00
|
|
|
var converter goldmark.Markdown
|
|
|
|
var once = sync.Once{}
|
2014-04-10 12:20:58 -06:00
|
|
|
|
2019-12-30 18:53:28 -07:00
|
|
|
var urlPrefixKey = parser.NewContextKey()
|
|
|
|
var isWikiKey = parser.NewContextKey()
|
2020-05-24 04:45:56 -06:00
|
|
|
var renderMetasKey = parser.NewContextKey()
|
2018-02-27 00:09:18 -07:00
|
|
|
|
2019-12-30 18:53:28 -07:00
|
|
|
// NewGiteaParseContext creates a parser.Context with the gitea context set
|
2020-05-24 04:45:56 -06:00
|
|
|
func NewGiteaParseContext(urlPrefix string, metas map[string]string, isWiki bool) parser.Context {
|
2019-12-30 18:53:28 -07:00
|
|
|
pc := parser.NewContext(parser.WithIDs(newPrefixedIDs()))
|
|
|
|
pc.Set(urlPrefixKey, urlPrefix)
|
|
|
|
pc.Set(isWikiKey, isWiki)
|
2020-05-24 04:45:56 -06:00
|
|
|
pc.Set(renderMetasKey, metas)
|
2019-12-30 18:53:28 -07:00
|
|
|
return pc
|
2016-01-08 19:59:04 -07:00
|
|
|
}
|
|
|
|
|
2020-05-24 04:45:56 -06:00
|
|
|
// render renders Markdown to HTML without handling special links.
|
|
|
|
func render(body []byte, urlPrefix string, metas map[string]string, wikiMarkdown bool) []byte {
|
2019-12-30 18:53:28 -07:00
|
|
|
once.Do(func() {
|
|
|
|
converter = goldmark.New(
|
|
|
|
goldmark.WithExtensions(extension.Table,
|
|
|
|
extension.Strikethrough,
|
|
|
|
extension.TaskList,
|
|
|
|
extension.DefinitionList,
|
|
|
|
common.FootnoteExtension,
|
|
|
|
extension.NewTypographer(
|
|
|
|
extension.WithTypographicSubstitutions(extension.TypographicSubstitutions{
|
2020-01-22 08:30:20 -07:00
|
|
|
extension.EnDash: nil,
|
|
|
|
extension.EmDash: nil,
|
|
|
|
extension.Ellipsis: nil,
|
2019-12-30 18:53:28 -07:00
|
|
|
}),
|
|
|
|
),
|
2020-04-24 07:22:36 -06:00
|
|
|
meta.Meta,
|
2019-12-30 18:53:28 -07:00
|
|
|
),
|
|
|
|
goldmark.WithParserOptions(
|
|
|
|
parser.WithAttribute(),
|
|
|
|
parser.WithAutoHeadingID(),
|
|
|
|
parser.WithASTTransformers(
|
2020-04-24 07:22:36 -06:00
|
|
|
util.Prioritized(&ASTTransformer{}, 10000),
|
2019-12-30 18:53:28 -07:00
|
|
|
),
|
|
|
|
),
|
|
|
|
goldmark.WithRendererOptions(
|
|
|
|
html.WithUnsafe(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Override the original Tasklist renderer!
|
|
|
|
converter.Renderer().AddOptions(
|
|
|
|
renderer.WithNodeRenderers(
|
2020-04-24 07:22:36 -06:00
|
|
|
util.Prioritized(NewHTMLRenderer(), 10),
|
2019-12-30 18:53:28 -07:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
})
|
2014-04-10 12:20:58 -06:00
|
|
|
|
2020-05-24 04:45:56 -06:00
|
|
|
pc := NewGiteaParseContext(urlPrefix, metas, wikiMarkdown)
|
2019-12-30 18:53:28 -07:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := converter.Convert(giteautil.NormalizeEOL(body), &buf, parser.WithContext(pc)); err != nil {
|
|
|
|
log.Error("Unable to render: %v", err)
|
2015-09-01 06:32:02 -06:00
|
|
|
}
|
2019-12-30 18:53:28 -07:00
|
|
|
return markup.SanitizeReader(&buf).Bytes()
|
2014-05-05 11:08:01 -06:00
|
|
|
}
|
|
|
|
|
2017-04-21 01:01:08 -06:00
|
|
|
var (
|
|
|
|
// MarkupName describes markup's name
|
|
|
|
MarkupName = "markdown"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
markup.RegisterParser(Parser{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser implements markup.Parser
|
2019-12-30 18:53:28 -07:00
|
|
|
type Parser struct{}
|
2017-04-21 01:01:08 -06: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 04:45:56 -06:00
|
|
|
return render(rawBytes, urlPrefix, metas, isWiki)
|
2017-09-16 11:17:57 -06: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 04:45:56 -06: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 11:17:57 -06: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 01:01:08 -06:00
|
|
|
}
|