mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 10:57:44 +09:00 
			
		
		
		
	Refactor markup render system (#32533)
Remove unmaintainable sanitizer rules. No need to add special "class" regexp rules anymore, use RenderInternal.SafeAttr instead, more details (and examples) are in the tests
This commit is contained in:
		| @@ -4,17 +4,21 @@ | ||||
| package math | ||||
|  | ||||
| import ( | ||||
| 	"code.gitea.io/gitea/modules/markup/internal" | ||||
|  | ||||
| 	gast "github.com/yuin/goldmark/ast" | ||||
| 	"github.com/yuin/goldmark/renderer" | ||||
| 	"github.com/yuin/goldmark/util" | ||||
| ) | ||||
|  | ||||
| // BlockRenderer represents a renderer for math Blocks | ||||
| type BlockRenderer struct{} | ||||
| type BlockRenderer struct { | ||||
| 	renderInternal *internal.RenderInternal | ||||
| } | ||||
|  | ||||
| // NewBlockRenderer creates a new renderer for math Blocks | ||||
| func NewBlockRenderer() renderer.NodeRenderer { | ||||
| 	return &BlockRenderer{} | ||||
| func NewBlockRenderer(renderInternal *internal.RenderInternal) renderer.NodeRenderer { | ||||
| 	return &BlockRenderer{renderInternal: renderInternal} | ||||
| } | ||||
|  | ||||
| // RegisterFuncs registers the renderer for math Blocks | ||||
| @@ -33,7 +37,7 @@ func (r *BlockRenderer) writeLines(w util.BufWriter, source []byte, n gast.Node) | ||||
| func (r *BlockRenderer) renderBlock(w util.BufWriter, source []byte, node gast.Node, entering bool) (gast.WalkStatus, error) { | ||||
| 	n := node.(*Block) | ||||
| 	if entering { | ||||
| 		_, _ = w.WriteString(`<pre class="code-block is-loading"><code class="chroma language-math display">`) | ||||
| 		_ = r.renderInternal.FormatWithSafeAttrs(w, `<pre class="code-block is-loading"><code class="chroma language-math display">`) | ||||
| 		r.writeLines(w, source, n) | ||||
| 	} else { | ||||
| 		_, _ = w.WriteString(`</code></pre>` + "\n") | ||||
|   | ||||
| @@ -6,17 +6,21 @@ package math | ||||
| import ( | ||||
| 	"bytes" | ||||
|  | ||||
| 	"code.gitea.io/gitea/modules/markup/internal" | ||||
|  | ||||
| 	"github.com/yuin/goldmark/ast" | ||||
| 	"github.com/yuin/goldmark/renderer" | ||||
| 	"github.com/yuin/goldmark/util" | ||||
| ) | ||||
|  | ||||
| // InlineRenderer is an inline renderer | ||||
| type InlineRenderer struct{} | ||||
| type InlineRenderer struct { | ||||
| 	renderInternal *internal.RenderInternal | ||||
| } | ||||
|  | ||||
| // NewInlineRenderer returns a new renderer for inline math | ||||
| func NewInlineRenderer() renderer.NodeRenderer { | ||||
| 	return &InlineRenderer{} | ||||
| func NewInlineRenderer(renderInternal *internal.RenderInternal) renderer.NodeRenderer { | ||||
| 	return &InlineRenderer{renderInternal: renderInternal} | ||||
| } | ||||
|  | ||||
| func (r *InlineRenderer) renderInline(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) { | ||||
| @@ -25,7 +29,7 @@ func (r *InlineRenderer) renderInline(w util.BufWriter, source []byte, n ast.Nod | ||||
| 		if _, ok := n.(*InlineBlock); ok { | ||||
| 			extraClass = "display " | ||||
| 		} | ||||
| 		_, _ = w.WriteString(`<code class="language-math ` + extraClass + `is-loading">`) | ||||
| 		_ = r.renderInternal.FormatWithSafeAttrs(w, `<code class="language-math %sis-loading">`, extraClass) | ||||
| 		for c := n.FirstChild(); c != nil; c = c.NextSibling() { | ||||
| 			segment := c.(*ast.Text).Segment | ||||
| 			value := util.EscapeHTML(segment.Value(source)) | ||||
|   | ||||
| @@ -4,6 +4,8 @@ | ||||
| package math | ||||
|  | ||||
| import ( | ||||
| 	"code.gitea.io/gitea/modules/markup/internal" | ||||
|  | ||||
| 	"github.com/yuin/goldmark" | ||||
| 	"github.com/yuin/goldmark/parser" | ||||
| 	"github.com/yuin/goldmark/renderer" | ||||
| @@ -12,6 +14,7 @@ import ( | ||||
|  | ||||
| // Extension is a math extension | ||||
| type Extension struct { | ||||
| 	renderInternal    *internal.RenderInternal | ||||
| 	enabled           bool | ||||
| 	parseDollarInline bool | ||||
| 	parseDollarBlock  bool | ||||
| @@ -39,38 +42,10 @@ func Enabled(enable ...bool) Option { | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // WithInlineDollarParser enables or disables the parsing of $...$ | ||||
| func WithInlineDollarParser(enable ...bool) Option { | ||||
| 	value := true | ||||
| 	if len(enable) > 0 { | ||||
| 		value = enable[0] | ||||
| 	} | ||||
| 	return extensionFunc(func(e *Extension) { | ||||
| 		e.parseDollarInline = value | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // WithBlockDollarParser enables or disables the parsing of $$...$$ | ||||
| func WithBlockDollarParser(enable ...bool) Option { | ||||
| 	value := true | ||||
| 	if len(enable) > 0 { | ||||
| 		value = enable[0] | ||||
| 	} | ||||
| 	return extensionFunc(func(e *Extension) { | ||||
| 		e.parseDollarBlock = value | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // Math represents a math extension with default rendered delimiters | ||||
| var Math = &Extension{ | ||||
| 	enabled:           true, | ||||
| 	parseDollarBlock:  true, | ||||
| 	parseDollarInline: true, | ||||
| } | ||||
|  | ||||
| // NewExtension creates a new math extension with the provided options | ||||
| func NewExtension(opts ...Option) *Extension { | ||||
| func NewExtension(renderInternal *internal.RenderInternal, opts ...Option) *Extension { | ||||
| 	r := &Extension{ | ||||
| 		renderInternal:    renderInternal, | ||||
| 		enabled:           true, | ||||
| 		parseDollarBlock:  true, | ||||
| 		parseDollarInline: true, | ||||
| @@ -102,7 +77,7 @@ func (e *Extension) Extend(m goldmark.Markdown) { | ||||
| 	m.Parser().AddOptions(parser.WithInlineParsers(inlines...)) | ||||
|  | ||||
| 	m.Renderer().AddOptions(renderer.WithNodeRenderers( | ||||
| 		util.Prioritized(NewBlockRenderer(), 501), | ||||
| 		util.Prioritized(NewInlineRenderer(), 502), | ||||
| 		util.Prioritized(NewBlockRenderer(e.renderInternal), 501), | ||||
| 		util.Prioritized(NewInlineRenderer(e.renderInternal), 502), | ||||
| 	)) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user