Files
Atay-Makhzan/modules/markup/markdown/math/math.go
T

61 lines
1.8 KiB
Go
Raw Normal View History

2022-09-13 17:33:37 +01:00
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2022-09-13 17:33:37 +01:00
package math
import (
2024-11-18 13:25:42 +08:00
"code.gitea.io/gitea/modules/markup/internal"
2024-12-14 13:43:05 +08:00
giteaUtil "code.gitea.io/gitea/modules/util"
2024-11-18 13:25:42 +08:00
2022-09-13 17:33:37 +01:00
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
)
2024-12-14 13:43:05 +08:00
type Options struct {
2025-04-05 11:56:48 +08:00
Enabled bool
ParseInlineDollar bool // inline $$ xxx $$ text
ParseInlineParentheses bool // inline \( xxx \) text
ParseBlockDollar bool // block $$ multiple-line $$ text
ParseBlockSquareBrackets bool // block \[ multiple-line \] text
2022-09-13 17:33:37 +01:00
}
2024-12-14 13:43:05 +08:00
// Extension is a math extension
type Extension struct {
renderInternal *internal.RenderInternal
options Options
2022-09-13 17:33:37 +01:00
}
// NewExtension creates a new math extension with the provided options
2024-12-14 13:43:05 +08:00
func NewExtension(renderInternal *internal.RenderInternal, opts ...Options) *Extension {
opt := giteaUtil.OptionalArg(opts)
2022-09-13 17:33:37 +01:00
r := &Extension{
2024-12-14 13:43:05 +08:00
renderInternal: renderInternal,
options: opt,
2022-09-13 17:33:37 +01:00
}
return r
}
// Extend extends goldmark with our parsers and renderers
func (e *Extension) Extend(m goldmark.Markdown) {
2024-12-14 13:43:05 +08:00
if !e.options.Enabled {
2022-09-13 17:33:37 +01:00
return
}
2025-04-05 11:56:48 +08:00
var inlines []util.PrioritizedValue
if e.options.ParseInlineParentheses {
inlines = append(inlines, util.Prioritized(NewInlineParenthesesParser(), 501))
2022-09-13 17:33:37 +01:00
}
2025-04-05 11:56:48 +08:00
inlines = append(inlines, util.Prioritized(NewInlineDollarParser(e.options.ParseInlineDollar), 502))
2022-09-13 17:33:37 +01:00
2025-04-05 11:56:48 +08:00
m.Parser().AddOptions(parser.WithInlineParsers(inlines...))
2024-12-14 13:43:05 +08:00
m.Parser().AddOptions(parser.WithBlockParsers(
2025-04-05 11:56:48 +08:00
util.Prioritized(NewBlockParser(e.options.ParseBlockDollar, e.options.ParseBlockSquareBrackets), 701),
2024-12-14 13:43:05 +08:00
))
2022-09-13 17:33:37 +01:00
m.Renderer().AddOptions(renderer.WithNodeRenderers(
2024-11-18 13:25:42 +08:00
util.Prioritized(NewBlockRenderer(e.renderInternal), 501),
util.Prioritized(NewInlineRenderer(e.renderInternal), 502),
2022-09-13 17:33:37 +01:00
))
}