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