2019-12-31 09:53:28 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-12-31 09:53:28 +08:00
|
|
|
|
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-24 21:22:36 +08:00
|
|
|
"regexp"
|
2019-12-31 09:53:28 +08:00
|
|
|
"strings"
|
2024-11-18 13:25:42 +08:00
|
|
|
"sync"
|
2019-12-31 09:53:28 +08:00
|
|
|
|
2022-10-12 13:18:26 +08:00
|
|
|
"code.gitea.io/gitea/modules/container"
|
2019-12-31 09:53:28 +08:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2024-11-18 13:25:42 +08:00
|
|
|
"code.gitea.io/gitea/modules/markup/internal"
|
2020-04-24 21:22:36 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-12-31 09:53:28 +08:00
|
|
|
|
|
|
|
"github.com/yuin/goldmark/ast"
|
|
|
|
east "github.com/yuin/goldmark/extension/ast"
|
|
|
|
"github.com/yuin/goldmark/parser"
|
|
|
|
"github.com/yuin/goldmark/renderer"
|
|
|
|
"github.com/yuin/goldmark/renderer/html"
|
|
|
|
"github.com/yuin/goldmark/text"
|
|
|
|
"github.com/yuin/goldmark/util"
|
|
|
|
)
|
|
|
|
|
2020-04-24 21:22:36 +08:00
|
|
|
// ASTTransformer is a default transformer of the goldmark tree.
|
2024-03-22 20:16:23 +08:00
|
|
|
type ASTTransformer struct {
|
2024-11-18 13:25:42 +08:00
|
|
|
renderInternal *internal.RenderInternal
|
2024-03-28 10:26:13 +08:00
|
|
|
attentionTypes container.Set[string]
|
2024-03-22 20:16:23 +08:00
|
|
|
}
|
|
|
|
|
2024-11-18 13:25:42 +08:00
|
|
|
func NewASTTransformer(renderInternal *internal.RenderInternal) *ASTTransformer {
|
2024-03-22 20:16:23 +08:00
|
|
|
return &ASTTransformer{
|
2024-11-18 13:25:42 +08:00
|
|
|
renderInternal: renderInternal,
|
2024-03-28 10:26:13 +08:00
|
|
|
attentionTypes: container.SetOf("note", "tip", "important", "warning", "caution"),
|
2024-03-22 20:16:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ASTTransformer) applyElementDir(n ast.Node) {
|
|
|
|
if markup.DefaultProcessorHelper.ElementDir != "" {
|
|
|
|
n.SetAttributeString("dir", []byte(markup.DefaultProcessorHelper.ElementDir))
|
|
|
|
}
|
|
|
|
}
|
2019-12-31 09:53:28 +08:00
|
|
|
|
|
|
|
// Transform transforms the given AST tree.
|
2020-04-24 21:22:36 +08:00
|
|
|
func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
|
|
|
|
firstChild := node.FirstChild()
|
2023-04-18 03:05:19 +08:00
|
|
|
tocMode := ""
|
2022-06-08 16:59:16 +08:00
|
|
|
ctx := pc.Get(renderContextKey).(*markup.RenderContext)
|
2022-09-14 00:33:37 +08:00
|
|
|
rc := pc.Get(renderConfigKey).(*RenderConfig)
|
2023-04-18 03:05:19 +08:00
|
|
|
|
2024-11-04 18:59:50 +08:00
|
|
|
tocList := make([]Header, 0, 20)
|
2022-09-14 00:33:37 +08:00
|
|
|
if rc.yamlNode != nil {
|
|
|
|
metaNode := rc.toMetaNode()
|
2020-04-24 21:22:36 +08:00
|
|
|
if metaNode != nil {
|
|
|
|
node.InsertBefore(node, firstChild, metaNode)
|
|
|
|
}
|
2023-04-18 03:05:19 +08:00
|
|
|
tocMode = rc.TOC
|
2020-04-24 21:22:36 +08:00
|
|
|
}
|
|
|
|
|
2019-12-31 09:53:28 +08:00
|
|
|
_ = ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
if !entering {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := n.(type) {
|
2020-04-24 21:22:36 +08:00
|
|
|
case *ast.Heading:
|
2024-03-28 10:26:13 +08:00
|
|
|
g.transformHeading(ctx, v, reader, &tocList)
|
2023-05-21 05:02:52 +08:00
|
|
|
case *ast.Paragraph:
|
2024-03-22 20:16:23 +08:00
|
|
|
g.applyElementDir(v)
|
2019-12-31 09:53:28 +08:00
|
|
|
case *ast.Image:
|
2024-04-29 16:47:56 +08:00
|
|
|
g.transformImage(ctx, v)
|
2019-12-31 09:53:28 +08:00
|
|
|
case *ast.Link:
|
2024-04-29 16:47:56 +08:00
|
|
|
g.transformLink(ctx, v)
|
2020-03-23 06:25:38 +08:00
|
|
|
case *ast.List:
|
2024-04-29 16:47:56 +08:00
|
|
|
g.transformList(ctx, v, rc)
|
2020-05-24 16:14:26 +08:00
|
|
|
case *ast.Text:
|
|
|
|
if v.SoftLineBreak() && !v.HardLineBreak() {
|
2024-11-14 13:02:11 +08:00
|
|
|
// TODO: this was a quite unclear part, old code: `if metas["mode"] != "document" { use comment link break setting }`
|
|
|
|
// many places render non-comment contents with no mode=document, then these contents also use comment's hard line break setting
|
|
|
|
// especially in many tests.
|
2024-11-22 13:48:09 +08:00
|
|
|
markdownLineBreakStyle := ctx.RenderOptions.Metas["markdownLineBreakStyle"]
|
2024-11-14 13:02:11 +08:00
|
|
|
if markup.RenderBehaviorForTesting.ForceHardLineBreak {
|
|
|
|
v.SetHardLineBreak(true)
|
2024-11-16 16:41:44 +08:00
|
|
|
} else if markdownLineBreakStyle == "comment" {
|
2020-05-24 16:14:26 +08:00
|
|
|
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInComments)
|
2024-11-16 16:41:44 +08:00
|
|
|
} else if markdownLineBreakStyle == "document" {
|
2020-05-24 16:14:26 +08:00
|
|
|
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInDocuments)
|
|
|
|
}
|
|
|
|
}
|
2022-10-21 20:00:53 +08:00
|
|
|
case *ast.CodeSpan:
|
2024-03-28 10:26:13 +08:00
|
|
|
g.transformCodeSpan(ctx, v, reader)
|
2024-02-11 02:43:09 +08:00
|
|
|
case *ast.Blockquote:
|
2024-03-22 20:16:23 +08:00
|
|
|
return g.transformBlockquote(v, reader)
|
2019-12-31 09:53:28 +08:00
|
|
|
}
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
})
|
2020-04-24 21:22:36 +08:00
|
|
|
|
2023-04-18 03:05:19 +08:00
|
|
|
showTocInMain := tocMode == "true" /* old behavior, in main view */ || tocMode == "main"
|
|
|
|
showTocInSidebar := !showTocInMain && tocMode != "false" // not hidden, not main, then show it in sidebar
|
|
|
|
if len(tocList) > 0 && (showTocInMain || showTocInSidebar) {
|
|
|
|
if showTocInMain {
|
|
|
|
tocNode := createTOCNode(tocList, rc.Lang, nil)
|
2020-04-24 21:22:36 +08:00
|
|
|
node.InsertBefore(node, firstChild, tocNode)
|
2023-04-18 03:05:19 +08:00
|
|
|
} else {
|
|
|
|
tocNode := createTOCNode(tocList, rc.Lang, map[string]string{"open": "open"})
|
|
|
|
ctx.SidebarTocNode = tocNode
|
2020-04-24 21:22:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rc.Lang) > 0 {
|
|
|
|
node.SetAttributeString("lang", []byte(rc.Lang))
|
|
|
|
}
|
2019-12-31 09:53:28 +08:00
|
|
|
}
|
|
|
|
|
2024-11-18 13:25:42 +08:00
|
|
|
// it is copied from old code, which is quite doubtful whether it is correct
|
|
|
|
var reValidIconName = sync.OnceValue[*regexp.Regexp](func() *regexp.Regexp {
|
|
|
|
return regexp.MustCompile(`^[-\w]+$`) // old: regexp.MustCompile("^[a-z ]+$")
|
|
|
|
})
|
|
|
|
|
|
|
|
// NewHTMLRenderer creates a HTMLRenderer to render in the gitea form.
|
|
|
|
func NewHTMLRenderer(renderInternal *internal.RenderInternal, opts ...html.Option) renderer.NodeRenderer {
|
2020-04-24 21:22:36 +08:00
|
|
|
r := &HTMLRenderer{
|
2024-11-18 13:25:42 +08:00
|
|
|
renderInternal: renderInternal,
|
|
|
|
Config: html.NewConfig(),
|
2019-12-31 09:53:28 +08:00
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.SetHTMLOption(&r.Config)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-04-24 21:22:36 +08:00
|
|
|
// HTMLRenderer is a renderer.NodeRenderer implementation that
|
|
|
|
// renders gitea specific features.
|
|
|
|
type HTMLRenderer struct {
|
2019-12-31 09:53:28 +08:00
|
|
|
html.Config
|
2024-11-18 13:25:42 +08:00
|
|
|
renderInternal *internal.RenderInternal
|
2019-12-31 09:53:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs.
|
2020-04-24 21:22:36 +08:00
|
|
|
func (r *HTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
|
|
|
|
reg.Register(ast.KindDocument, r.renderDocument)
|
|
|
|
reg.Register(KindDetails, r.renderDetails)
|
|
|
|
reg.Register(KindSummary, r.renderSummary)
|
|
|
|
reg.Register(KindIcon, r.renderIcon)
|
2022-10-21 20:00:53 +08:00
|
|
|
reg.Register(ast.KindCodeSpan, r.renderCodeSpan)
|
2022-11-09 08:11:26 +08:00
|
|
|
reg.Register(KindAttention, r.renderAttention)
|
2020-04-26 13:09:08 +08:00
|
|
|
reg.Register(KindTaskCheckBoxListItem, r.renderTaskCheckBoxListItem)
|
2019-12-31 09:53:28 +08:00
|
|
|
reg.Register(east.KindTaskCheckBox, r.renderTaskCheckBox)
|
|
|
|
}
|
|
|
|
|
2020-04-24 21:22:36 +08:00
|
|
|
func (r *HTMLRenderer) renderDocument(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
n := node.(*ast.Document)
|
|
|
|
|
|
|
|
if val, has := n.AttributeString("lang"); has {
|
|
|
|
var err error
|
|
|
|
if entering {
|
|
|
|
_, err = w.WriteString("<div")
|
|
|
|
if err == nil {
|
|
|
|
_, err = w.WriteString(fmt.Sprintf(` lang=%q`, val))
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
_, err = w.WriteRune('>')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, err = w.WriteString("</div>")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ast.WalkStop, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *HTMLRenderer) renderDetails(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
var err error
|
|
|
|
if entering {
|
2023-04-18 03:05:19 +08:00
|
|
|
if _, err = w.WriteString("<details"); err != nil {
|
|
|
|
return ast.WalkStop, err
|
|
|
|
}
|
|
|
|
html.RenderAttributes(w, node, nil)
|
|
|
|
_, err = w.WriteString(">")
|
2020-04-24 21:22:36 +08:00
|
|
|
} else {
|
|
|
|
_, err = w.WriteString("</details>")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ast.WalkStop, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *HTMLRenderer) renderSummary(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
var err error
|
|
|
|
if entering {
|
|
|
|
_, err = w.WriteString("<summary>")
|
|
|
|
} else {
|
|
|
|
_, err = w.WriteString("</summary>")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ast.WalkStop, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *HTMLRenderer) renderIcon(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
if !entering {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n := node.(*Icon)
|
|
|
|
|
|
|
|
name := strings.TrimSpace(strings.ToLower(string(n.Name)))
|
|
|
|
|
|
|
|
if len(name) == 0 {
|
|
|
|
// skip this
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
2024-11-18 13:25:42 +08:00
|
|
|
if !reValidIconName().MatchString(name) {
|
2020-04-24 21:22:36 +08:00
|
|
|
// skip this
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
2024-11-18 13:25:42 +08:00
|
|
|
// FIXME: the "icon xxx" is from Fomantic UI, it's really questionable whether it still works correctly
|
|
|
|
err := r.renderInternal.FormatWithSafeAttrs(w, `<i class="icon %s"></i>`, name)
|
2020-04-24 21:22:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return ast.WalkStop, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|