2022-09-13 10:33:37 -06:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-09-13 10:33:37 -06:00
|
|
|
|
|
|
|
package math
|
|
|
|
|
|
|
|
import (
|
2024-11-17 22:25:42 -07:00
|
|
|
"code.gitea.io/gitea/modules/markup/internal"
|
2024-12-13 22:43:05 -07:00
|
|
|
giteaUtil "code.gitea.io/gitea/modules/util"
|
2024-11-17 22:25:42 -07:00
|
|
|
|
2022-09-13 10:33:37 -06:00
|
|
|
"github.com/yuin/goldmark"
|
|
|
|
"github.com/yuin/goldmark/parser"
|
|
|
|
"github.com/yuin/goldmark/renderer"
|
|
|
|
"github.com/yuin/goldmark/util"
|
|
|
|
)
|
|
|
|
|
2024-12-13 22:43:05 -07:00
|
|
|
type Options struct {
|
|
|
|
Enabled bool
|
|
|
|
ParseDollarInline bool
|
|
|
|
ParseDollarBlock bool
|
|
|
|
ParseSquareBlock bool
|
2022-09-13 10:33:37 -06:00
|
|
|
}
|
|
|
|
|
2024-12-13 22:43:05 -07:00
|
|
|
// Extension is a math extension
|
|
|
|
type Extension struct {
|
|
|
|
renderInternal *internal.RenderInternal
|
|
|
|
options Options
|
2022-09-13 10:33:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewExtension creates a new math extension with the provided options
|
2024-12-13 22:43:05 -07:00
|
|
|
func NewExtension(renderInternal *internal.RenderInternal, opts ...Options) *Extension {
|
|
|
|
opt := giteaUtil.OptionalArg(opts)
|
2022-09-13 10:33:37 -06:00
|
|
|
r := &Extension{
|
2024-12-13 22:43:05 -07:00
|
|
|
renderInternal: renderInternal,
|
|
|
|
options: opt,
|
2022-09-13 10:33:37 -06:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extend extends goldmark with our parsers and renderers
|
|
|
|
func (e *Extension) Extend(m goldmark.Markdown) {
|
2024-12-13 22:43:05 -07:00
|
|
|
if !e.options.Enabled {
|
2022-09-13 10:33:37 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-12-13 22:43:05 -07:00
|
|
|
inlines := []util.PrioritizedValue{util.Prioritized(NewInlineBracketParser(), 501)}
|
|
|
|
if e.options.ParseDollarInline {
|
|
|
|
inlines = append(inlines, util.Prioritized(NewInlineDollarParser(), 502))
|
2022-09-13 10:33:37 -06:00
|
|
|
}
|
|
|
|
m.Parser().AddOptions(parser.WithInlineParsers(inlines...))
|
|
|
|
|
2024-12-13 22:43:05 -07:00
|
|
|
m.Parser().AddOptions(parser.WithBlockParsers(
|
|
|
|
util.Prioritized(NewBlockParser(e.options.ParseDollarBlock, e.options.ParseSquareBlock), 701),
|
|
|
|
))
|
|
|
|
|
2022-09-13 10:33:37 -06:00
|
|
|
m.Renderer().AddOptions(renderer.WithNodeRenderers(
|
2024-11-17 22:25:42 -07:00
|
|
|
util.Prioritized(NewBlockRenderer(e.renderInternal), 501),
|
|
|
|
util.Prioritized(NewInlineRenderer(e.renderInternal), 502),
|
2022-09-13 10:33:37 -06:00
|
|
|
))
|
|
|
|
}
|