2014-04-10 23:50:58 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2018-10-31 03:56:28 +05:30
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2014-04-10 23:50:58 +05:30
|
|
|
// 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"
|
2019-12-31 07:23:28 +05:30
|
|
|
"sync"
|
2014-04-10 23:50:58 +05:30
|
|
|
|
2019-12-31 07:23:28 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2017-04-21 12:31:08 +05:30
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2019-12-31 07:23:28 +05:30
|
|
|
"code.gitea.io/gitea/modules/markup/common"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-12-31 07:23:28 +05:30
|
|
|
giteautil "code.gitea.io/gitea/modules/util"
|
|
|
|
|
|
|
|
"github.com/yuin/goldmark"
|
2020-04-09 16:24:50 +05:30
|
|
|
meta "github.com/yuin/goldmark-meta"
|
2019-12-31 07:23:28 +05:30
|
|
|
"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 23:50:58 +05:30
|
|
|
)
|
|
|
|
|
2019-12-31 07:23:28 +05:30
|
|
|
var converter goldmark.Markdown
|
|
|
|
var once = sync.Once{}
|
2014-04-10 23:50:58 +05:30
|
|
|
|
2019-12-31 07:23:28 +05:30
|
|
|
var urlPrefixKey = parser.NewContextKey()
|
|
|
|
var isWikiKey = parser.NewContextKey()
|
2018-02-27 12:39:18 +05:30
|
|
|
|
2019-12-31 07:23:28 +05:30
|
|
|
// NewGiteaParseContext creates a parser.Context with the gitea context set
|
|
|
|
func NewGiteaParseContext(urlPrefix string, isWiki bool) parser.Context {
|
|
|
|
pc := parser.NewContext(parser.WithIDs(newPrefixedIDs()))
|
|
|
|
pc.Set(urlPrefixKey, urlPrefix)
|
|
|
|
pc.Set(isWikiKey, isWiki)
|
|
|
|
return pc
|
2016-01-09 08:29:04 +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 {
|
2019-12-31 07:23:28 +05:30
|
|
|
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 21:00:20 +05:30
|
|
|
extension.EnDash: nil,
|
|
|
|
extension.EmDash: nil,
|
|
|
|
extension.Ellipsis: nil,
|
2019-12-31 07:23:28 +05:30
|
|
|
}),
|
|
|
|
),
|
2020-04-24 18:52:36 +05:30
|
|
|
meta.Meta,
|
2019-12-31 07:23:28 +05:30
|
|
|
),
|
|
|
|
goldmark.WithParserOptions(
|
|
|
|
parser.WithAttribute(),
|
|
|
|
parser.WithAutoHeadingID(),
|
|
|
|
parser.WithASTTransformers(
|
2020-04-24 18:52:36 +05:30
|
|
|
util.Prioritized(&ASTTransformer{}, 10000),
|
2019-12-31 07:23:28 +05:30
|
|
|
),
|
|
|
|
),
|
|
|
|
goldmark.WithRendererOptions(
|
|
|
|
html.WithUnsafe(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Override the original Tasklist renderer!
|
|
|
|
converter.Renderer().AddOptions(
|
|
|
|
renderer.WithNodeRenderers(
|
2020-04-24 18:52:36 +05:30
|
|
|
util.Prioritized(NewHTMLRenderer(), 10),
|
2019-12-31 07:23:28 +05:30
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
if setting.Markdown.EnableHardLineBreak {
|
|
|
|
converter.Renderer().AddOptions(html.WithHardWraps())
|
|
|
|
}
|
|
|
|
})
|
2014-04-10 23:50:58 +05:30
|
|
|
|
2019-12-31 07:23:28 +05:30
|
|
|
pc := NewGiteaParseContext(urlPrefix, wikiMarkdown)
|
|
|
|
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 18:02:02 +05:30
|
|
|
}
|
2019-12-31 07:23:28 +05:30
|
|
|
return markup.SanitizeReader(&buf).Bytes()
|
2014-05-05 22:38:01 +05:30
|
|
|
}
|
|
|
|
|
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
|
2019-12-31 07:23:28 +05:30
|
|
|
type Parser struct{}
|
2017-04-21 12:31:08 +05:30
|
|
|
|
|
|
|
// 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
|
|
|
}
|