2019-12-31 07:23:28 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-12-31 07:23:28 +05:30
|
|
|
|
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-24 18:52:36 +05:30
|
|
|
"regexp"
|
2019-12-31 07:23:28 +05:30
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2020-04-24 18:52:36 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-12-31 07:23:28 +05:30
|
|
|
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var byteMailto = []byte("mailto:")
|
|
|
|
|
2020-04-24 18:52:36 +05:30
|
|
|
// ASTTransformer is a default transformer of the goldmark tree.
|
|
|
|
type ASTTransformer struct{}
|
2019-12-31 07:23:28 +05:30
|
|
|
|
2024-03-28 07:56:13 +05:30
|
|
|
func (g *ASTTransformer) applyElementDir(n ast.Node) {
|
|
|
|
if markup.DefaultProcessorHelper.ElementDir != "" {
|
|
|
|
n.SetAttributeString("dir", []byte(markup.DefaultProcessorHelper.ElementDir))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:23:28 +05:30
|
|
|
// Transform transforms the given AST tree.
|
2020-04-24 18:52:36 +05:30
|
|
|
func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
|
|
|
|
firstChild := node.FirstChild()
|
2023-04-18 00:35:19 +05:30
|
|
|
tocMode := ""
|
2022-06-08 14:29:16 +05:30
|
|
|
ctx := pc.Get(renderContextKey).(*markup.RenderContext)
|
2022-09-13 22:03:37 +05:30
|
|
|
rc := pc.Get(renderConfigKey).(*RenderConfig)
|
2023-04-18 00:35:19 +05:30
|
|
|
|
|
|
|
tocList := make([]markup.Header, 0, 20)
|
2022-09-13 22:03:37 +05:30
|
|
|
if rc.yamlNode != nil {
|
|
|
|
metaNode := rc.toMetaNode()
|
2020-04-24 18:52:36 +05:30
|
|
|
if metaNode != nil {
|
|
|
|
node.InsertBefore(node, firstChild, metaNode)
|
|
|
|
}
|
2023-04-18 00:35:19 +05:30
|
|
|
tocMode = rc.TOC
|
2020-04-24 18:52:36 +05:30
|
|
|
}
|
|
|
|
|
2019-12-31 07:23:28 +05:30
|
|
|
_ = 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 18:52:36 +05:30
|
|
|
case *ast.Heading:
|
2024-03-28 07:56:13 +05:30
|
|
|
g.transformHeading(ctx, v, reader, &tocList)
|
2023-05-21 02:32:52 +05:30
|
|
|
case *ast.Paragraph:
|
2024-03-28 07:56:13 +05:30
|
|
|
g.applyElementDir(v)
|
2019-12-31 07:23:28 +05:30
|
|
|
case *ast.Image:
|
2024-03-28 07:56:13 +05:30
|
|
|
g.transformImage(ctx, v, reader)
|
2019-12-31 07:23:28 +05:30
|
|
|
case *ast.Link:
|
2024-03-28 07:56:13 +05:30
|
|
|
g.transformLink(ctx, v, reader)
|
2020-03-23 03:55:38 +05:30
|
|
|
case *ast.List:
|
2024-03-28 07:56:13 +05:30
|
|
|
g.transformList(ctx, v, reader, rc)
|
2020-05-24 13:44:26 +05:30
|
|
|
case *ast.Text:
|
|
|
|
if v.SoftLineBreak() && !v.HardLineBreak() {
|
2024-01-15 14:19:24 +05:30
|
|
|
if ctx.Metas["mode"] != "document" {
|
2020-05-24 13:44:26 +05:30
|
|
|
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInComments)
|
|
|
|
} else {
|
|
|
|
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInDocuments)
|
|
|
|
}
|
|
|
|
}
|
2022-10-21 17:30:53 +05:30
|
|
|
case *ast.CodeSpan:
|
2024-03-28 07:56:13 +05:30
|
|
|
g.transformCodeSpan(ctx, v, reader)
|
2019-12-31 07:23:28 +05:30
|
|
|
}
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
})
|
2020-04-24 18:52:36 +05:30
|
|
|
|
2023-04-18 00:35:19 +05:30
|
|
|
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 18:52:36 +05:30
|
|
|
node.InsertBefore(node, firstChild, tocNode)
|
2023-04-18 00:35:19 +05:30
|
|
|
} else {
|
|
|
|
tocNode := createTOCNode(tocList, rc.Lang, map[string]string{"open": "open"})
|
|
|
|
ctx.SidebarTocNode = tocNode
|
2020-04-24 18:52:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rc.Lang) > 0 {
|
|
|
|
node.SetAttributeString("lang", []byte(rc.Lang))
|
|
|
|
}
|
2019-12-31 07:23:28 +05:30
|
|
|
}
|
|
|
|
|
2020-04-24 18:52:36 +05:30
|
|
|
// NewHTMLRenderer creates a HTMLRenderer to render
|
2019-12-31 07:23:28 +05:30
|
|
|
// in the gitea form.
|
2020-04-24 18:52:36 +05:30
|
|
|
func NewHTMLRenderer(opts ...html.Option) renderer.NodeRenderer {
|
|
|
|
r := &HTMLRenderer{
|
2019-12-31 07:23:28 +05:30
|
|
|
Config: html.NewConfig(),
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.SetHTMLOption(&r.Config)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-04-24 18:52:36 +05:30
|
|
|
// HTMLRenderer is a renderer.NodeRenderer implementation that
|
|
|
|
// renders gitea specific features.
|
|
|
|
type HTMLRenderer struct {
|
2019-12-31 07:23:28 +05:30
|
|
|
html.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs.
|
2020-04-24 18:52:36 +05:30
|
|
|
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 17:30:53 +05:30
|
|
|
reg.Register(ast.KindCodeSpan, r.renderCodeSpan)
|
2020-04-26 10:39:08 +05:30
|
|
|
reg.Register(KindTaskCheckBoxListItem, r.renderTaskCheckBoxListItem)
|
2019-12-31 07:23:28 +05:30
|
|
|
reg.Register(east.KindTaskCheckBox, r.renderTaskCheckBox)
|
|
|
|
}
|
|
|
|
|
2020-04-24 18:52:36 +05:30
|
|
|
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 00:35:19 +05:30
|
|
|
if _, err = w.WriteString("<details"); err != nil {
|
|
|
|
return ast.WalkStop, err
|
|
|
|
}
|
|
|
|
html.RenderAttributes(w, node, nil)
|
|
|
|
_, err = w.WriteString(">")
|
2020-04-24 18:52:36 +05:30
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
2024-02-17 22:06:07 +05:30
|
|
|
var validNameRE = regexp.MustCompile("^[a-z ]+$")
|
2020-04-24 18:52:36 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validNameRE.MatchString(name) {
|
|
|
|
// skip this
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
_, err = w.WriteString(fmt.Sprintf(`<i class="icon %s"></i>`, name))
|
|
|
|
if err != nil {
|
|
|
|
return ast.WalkStop, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|