mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-28 10:33:20 +00:00
f92591b825
close #31481 currently `$$A + B$$ test` will ignore text after $$ block test text ![圖片](https://github.com/go-gitea/gitea/assets/30816317/39b2974b-c0b6-48a0-87d0-5f4a13615eed) before fix ![圖片](https://github.com/go-gitea/gitea/assets/30816317/15469e4c-474d-4128-b46f-d6cadaafbd68) after fix ![圖片](https://github.com/go-gitea/gitea/assets/30816317/c1025eef-177f-4ade-988f-510e7039f3f9) github display ![圖片](https://github.com/go-gitea/gitea/assets/30816317/97cd1e10-ac94-4899-86d8-8e359ef1d694) (cherry picked from commit f0033051d5ce07f5a18d2aacef6bfdca5fc69014)
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package math
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"github.com/yuin/goldmark/ast"
|
|
"github.com/yuin/goldmark/renderer"
|
|
"github.com/yuin/goldmark/util"
|
|
)
|
|
|
|
// InlineRenderer is an inline renderer
|
|
type InlineRenderer struct{}
|
|
|
|
// NewInlineRenderer returns a new renderer for inline math
|
|
func NewInlineRenderer() renderer.NodeRenderer {
|
|
return &InlineRenderer{}
|
|
}
|
|
|
|
func (r *InlineRenderer) renderInline(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
if entering {
|
|
extraClass := ""
|
|
if _, ok := n.(*InlineBlock); ok {
|
|
extraClass = "display "
|
|
}
|
|
_, _ = w.WriteString(`<code class="language-math ` + extraClass + `is-loading">`)
|
|
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
|
|
segment := c.(*ast.Text).Segment
|
|
value := util.EscapeHTML(segment.Value(source))
|
|
if bytes.HasSuffix(value, []byte("\n")) {
|
|
_, _ = w.Write(value[:len(value)-1])
|
|
if c != n.LastChild() {
|
|
_, _ = w.Write([]byte(" "))
|
|
}
|
|
} else {
|
|
_, _ = w.Write(value)
|
|
}
|
|
}
|
|
return ast.WalkSkipChildren, nil
|
|
}
|
|
_, _ = w.WriteString(`</code>`)
|
|
return ast.WalkContinue, nil
|
|
}
|
|
|
|
// RegisterFuncs registers the renderer for inline math nodes
|
|
func (r *InlineRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
|
|
reg.Register(KindInline, r.renderInline)
|
|
reg.Register(KindInlineBlock, r.renderInline)
|
|
}
|