2024-02-17 16:36:07 +00:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package callout
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/svg"
|
|
|
|
|
|
|
|
"github.com/yuin/goldmark/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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GitHubCalloutTransformer struct{}
|
|
|
|
|
|
|
|
// Transform transforms the given AST tree.
|
|
|
|
func (g *GitHubCalloutTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
|
|
|
|
supportedAttentionTypes := map[string]bool{
|
|
|
|
"note": true,
|
|
|
|
"tip": true,
|
|
|
|
"important": true,
|
|
|
|
"warning": true,
|
|
|
|
"caution": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
if !entering {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
2024-07-17 17:19:36 +00:00
|
|
|
if v, ok := n.(*ast.Blockquote); ok {
|
2024-07-20 23:02:31 +00:00
|
|
|
if v.ChildCount() == 0 {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
2024-02-17 16:36:07 +00:00
|
|
|
// We only want attention blockquotes when the AST looks like:
|
|
|
|
// Text: "["
|
|
|
|
// Text: "!TYPE"
|
|
|
|
// Text(SoftLineBreak): "]"
|
|
|
|
|
|
|
|
// grab these nodes and make sure we adhere to the attention blockquote structure
|
|
|
|
firstParagraph := v.FirstChild()
|
|
|
|
if firstParagraph.ChildCount() < 3 {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
firstTextNode, ok := firstParagraph.FirstChild().(*ast.Text)
|
|
|
|
if !ok || string(firstTextNode.Text(reader.Source())) != "[" {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
secondTextNode, ok := firstTextNode.NextSibling().(*ast.Text)
|
|
|
|
if !ok {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
// If the second node's text isn't one of the supported attention
|
|
|
|
// types, continue walking.
|
|
|
|
secondTextNodeText := secondTextNode.Text(reader.Source())
|
|
|
|
attentionType := strings.ToLower(strings.TrimPrefix(string(secondTextNodeText), "!"))
|
|
|
|
if _, has := supportedAttentionTypes[attentionType]; !has {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
thirdTextNode, ok := secondTextNode.NextSibling().(*ast.Text)
|
|
|
|
if !ok || string(thirdTextNode.Text(reader.Source())) != "]" {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// color the blockquote
|
2024-03-16 11:34:38 +00:00
|
|
|
v.SetAttributeString("class", []byte("attention-header attention-"+attentionType))
|
2024-02-17 16:36:07 +00:00
|
|
|
|
|
|
|
// create an emphasis to make it bold
|
2024-03-08 09:30:41 +00:00
|
|
|
attentionParagraph := ast.NewParagraph()
|
2024-04-03 19:52:30 +00:00
|
|
|
attentionParagraph.SetAttributeString("class", []byte("attention-title"))
|
2024-02-17 16:36:07 +00:00
|
|
|
emphasis := ast.NewEmphasis(2)
|
|
|
|
emphasis.SetAttributeString("class", []byte("attention-"+attentionType))
|
|
|
|
firstParagraph.InsertBefore(firstParagraph, firstTextNode, emphasis)
|
|
|
|
|
|
|
|
// capitalize first letter
|
|
|
|
attentionText := ast.NewString([]byte(strings.ToUpper(string(attentionType[0])) + attentionType[1:]))
|
|
|
|
|
2024-03-08 09:30:41 +00:00
|
|
|
// replace the ![TYPE] with a dedicated paragraph of icon+Type
|
2024-02-17 16:36:07 +00:00
|
|
|
emphasis.AppendChild(emphasis, attentionText)
|
2024-03-08 09:30:41 +00:00
|
|
|
attentionParagraph.AppendChild(attentionParagraph, NewAttention(attentionType))
|
|
|
|
attentionParagraph.AppendChild(attentionParagraph, emphasis)
|
|
|
|
firstParagraph.Parent().InsertBefore(firstParagraph.Parent(), firstParagraph, attentionParagraph)
|
2024-02-17 16:36:07 +00:00
|
|
|
firstParagraph.RemoveChild(firstParagraph, firstTextNode)
|
|
|
|
firstParagraph.RemoveChild(firstParagraph, secondTextNode)
|
2024-08-18 14:36:06 +00:00
|
|
|
firstParagraph.RemoveChild(firstParagraph, thirdTextNode)
|
2024-02-17 16:36:07 +00:00
|
|
|
}
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type GitHubCalloutHTMLRenderer struct {
|
|
|
|
html.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs.
|
|
|
|
func (r *GitHubCalloutHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
|
|
|
|
reg.Register(KindAttention, r.renderAttention)
|
|
|
|
}
|
|
|
|
|
|
|
|
// renderAttention renders a quote marked with i.e. "> **Note**" or "> **Warning**" with a corresponding svg
|
|
|
|
func (r *GitHubCalloutHTMLRenderer) renderAttention(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
if entering {
|
|
|
|
n := node.(*Attention)
|
|
|
|
|
2024-03-16 11:34:38 +00:00
|
|
|
var octiconName string
|
2024-02-17 16:36:07 +00:00
|
|
|
switch n.AttentionType {
|
|
|
|
case "note":
|
2024-03-16 11:34:38 +00:00
|
|
|
octiconName = "info"
|
2024-02-17 16:36:07 +00:00
|
|
|
case "tip":
|
2024-03-16 11:34:38 +00:00
|
|
|
octiconName = "light-bulb"
|
2024-02-17 16:36:07 +00:00
|
|
|
case "important":
|
2024-03-16 11:34:38 +00:00
|
|
|
octiconName = "report"
|
2024-02-17 16:36:07 +00:00
|
|
|
case "warning":
|
2024-03-16 11:34:38 +00:00
|
|
|
octiconName = "alert"
|
2024-02-17 16:36:07 +00:00
|
|
|
case "caution":
|
2024-03-16 11:34:38 +00:00
|
|
|
octiconName = "stop"
|
|
|
|
default:
|
|
|
|
octiconName = "info"
|
2024-02-17 16:36:07 +00:00
|
|
|
}
|
2024-03-16 11:34:38 +00:00
|
|
|
_, _ = w.WriteString(string(svg.RenderHTML("octicon-"+octiconName, 16, "attention-icon attention-"+n.AttentionType)))
|
2024-02-17 16:36:07 +00:00
|
|
|
}
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGitHubCalloutHTMLRenderer(opts ...html.Option) renderer.NodeRenderer {
|
|
|
|
r := &GitHubCalloutHTMLRenderer{
|
|
|
|
Config: html.NewConfig(),
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.SetHTMLOption(&r.Config)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|